I have a game where circular objects shoot up from the bottom of the screen and I would like to be able to swipe them to flick them in the direction of my swipe. My issue is, I don't know how to calculate the vector/direction of the swipe in order to get the circular object to get flicked in the proper direction with the proper velocity.
The static vector "(5,5)" I am using needs to be calculated by the swipe speed and direction of the swipe. Also, I need to make sure that once I make first contact with the object, it no longer happens, as to refrain from double hitting the object. Here's what I am doing currently:
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
for (UITouch *touch in touches) {
CGPoint location = [touch locationInNode:self];
SKNode* node = [self nodeAtPoint:location];
[node.physicsBody applyImpulse:CGVectorMake(5, 5) atPoint:location];
}
}
Handling touches and swipes is right at the core of most apps – after all, it’s how your users interact with the user interface! In Sprite Kit, there’s no UIButton equivalent to handle simple taps, and you need to implement touch handling yourself with callbacks or with gesture recognizers.
In Sprite Kit, there’s no UIButton equivalent to handle simple taps, and you need to implement touch handling yourself with callbacks or with gesture recognizers. How to use gesture recognizers with Sprite Kit for even more cool effects!
SpriteKit. The SpriteKit framework makes it easy to create high-performance, battery-efficient 2D games. With support for custom OpenGL ES shaders and lighting, integration with SceneKit, and advanced new physics effects and animations, you can add force fields, detect collisions, and generate new lighting effects in your games.
The anchor point in Sprite Kit is at the center of the node by default, which means you're usually setting the center point when you set a node's position. By setting the anchor point to the lower left corner, when you set the position of the node, you are now setting where the lower left corner is.
Here's an example of how to detect a swipe gesture:
First, define instance variables to store the starting location and time .
CGPoint start;
NSTimeInterval startTime;
In touchesBegan, save the location/time of a touch event.
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
/* Avoid multi-touch gestures (optional) */
if ([touches count] > 1) {
return;
}
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInNode:self];
// Save start location and time
start = location;
startTime = touch.timestamp;
}
Define parameters of the swipe gesture. Adjust these accordingly.
#define kMinDistance 25
#define kMinDuration 0.1
#define kMinSpeed 100
#define kMaxSpeed 500
In touchesEnded, determine if the user's gesture was a swipe by comparing the differences between starting and ending locations and time stamps.
- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInNode:self];
// Determine distance from the starting point
CGFloat dx = location.x - start.x;
CGFloat dy = location.y - start.y;
CGFloat magnitude = sqrt(dx*dx+dy*dy);
if (magnitude >= kMinDistance) {
// Determine time difference from start of the gesture
CGFloat dt = touch.timestamp - startTime;
if (dt > kMinDuration) {
// Determine gesture speed in points/sec
CGFloat speed = magnitude / dt;
if (speed >= kMinSpeed && speed <= kMaxSpeed) {
// Calculate normalized direction of the swipe
dx = dx / magnitude;
dy = dy / magnitude;
NSLog(@"Swipe detected with speed = %g and direction (%g, %g)",speed, dx, dy);
}
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With