Following the Finger Around

To move the ship around, we have to update its position property every time a finger comes in contact with the screen. Thankfully, handling touch events in Sprite Kit scenes is the same as elsewhere in iOS. We have all the standard low-level touch event methods.

We’ll add this method after the ‑initWithSize: method to move the ship when a touch begins:

01-SpriteIntro/step03/SpaceRun/RCWMyScene.m
 
- (​void​)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
 
{
 
UITouch *touch = [touches anyObject];
 
CGPoint touchPoint = [touch locationInNode:self];
 
SKNode *ship = [self childNodeWithName:@​"ship"​];
 
ship.position = touchPoint;
 
}

In the ‑touchesBegan:withEvent: method, we grab one of the touches out of ...

Get Build iOS Games with Sprite Kit now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.