Skip to content

Instantly share code, notes, and snippets.

@Andrewjp97
Created November 25, 2013 20:46
Show Gist options
  • Select an option

  • Save Andrewjp97/7648589 to your computer and use it in GitHub Desktop.

Select an option

Save Andrewjp97/7648589 to your computer and use it in GitHub Desktop.
This Objective-C Class Category on SKScene returns a Boolean value for whether a touch occurred within a specified Boundary
//
// SKScene+DeterminingTouches.h
// Space Platypus
//
// Created by Andrew Paterson on 11/25/13.
// Copyright (c) 2013 Andrew Paterson. All rights reserved.
//
#import <SpriteKit/SpriteKit.h>
@interface SKScene (DeterminingTouches)
- (BOOL)touch:(UITouch *)touch occuredWithinBoundaryMinimum:(CGPoint)boundaryMin
andBoundaryMaximum:(CGPoint)boundaryMax
andIsInclusiveOfEdgeLeft:(BOOL)leftEdge
right:(BOOL)rightEdge
top:(BOOL)topEdge
bottom:(BOOL)bottomEdge;
@end
//
// SKScene+DeterminingTouches.m
// Space Platypus
//
// Created by Andrew Paterson on 11/25/13.
// Copyright (c) 2013 Andrew Paterson. All Rights Reserved.
//
#import "SKScene+DeterminingTouches.h"
/*
| | | | | | | | | X
-------------------------------------
| | | | | | | | | |
-------------------------------------
| | | | | | | | | |
-------------------------------------
| | | | | | | | | |
-------------------------------------
| | | | | | | | | |
-------------------------------------
| | | | | | | | | |
-------------------------------------
| | | | | | | | | |
-------------------------------------
| | | | | | | | | |
-------------------------------------
| | | | | | | | | |
-------------------------------------
| | | | | | | | | |
X------------------------------------
(0,0) and (10, 10) cover the entire grid, because the minX = 0 and the maxX = 10; the minY = 0 and the maxY = 10;
If any point is enter withing this grid, so long as all edges are included the result will be YES
You may exclude any edge from the boundary, in such a way to create consecutive areas with neighboring borders, and
should a touch land on the line between two button, it would be recognized by the one that has the edge included.
Otherwise it will be recognized by the method run on the thread first. Such action can lead to unpredictable behavior, and so
exclusion of the neighboring edge from one button and inclusion of it on the other is reccomended.
*/
@implementation SKScene (DeterminingTouches)
- (BOOL)touch:(UITouch *)touch occuredWithinBoundaryMinimum:(CGPoint)boundaryMin
andBoundaryMaximum:(CGPoint)boundaryMax
andIsInclusiveOfEdgeLeft:(BOOL)leftEdge
right:(BOOL)rightEdge
top:(BOOL)topEdge
bottom:(BOOL)bottomEdge{
CGFloat maxX = 0;
CGFloat minX = 0;
CGFloat maxY = 0;
CGFloat minY = 0;
if (rightEdge) {
maxX = boundaryMax.x;
}
else {
maxX = boundaryMax.x - 0.001;
}
if (leftEdge) {
minX = boundaryMin.x;
}
else {
minX = boundaryMin.x + 0.001;
}
if (topEdge) {
maxY = boundaryMax.y;
}
else {
maxY = boundaryMax.x - 0.001;
}
if (bottomEdge) {
minY = boundaryMin.y;
}
else {
minY = boundaryMin.y + 0.001;
}
CGPoint touchPoint = [touch locationInNode:self];
CGFloat xGrid = touchPoint.x;
CGFloat yGrid = touchPoint.y;
if (((xGrid >= minX) && (xGrid <= maxX)) && ((yGrid >= minY) && (yGrid <= maxY))) {
return YES;
}
else{
return NO;
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment