Making the Ship Glide

As our game is now, we have a mechanical problem with our ship. It only moves when a touch begins on the screen, and we want it to move toward where the finger drags around on the screen. Because we get all the standard touch events from iOS, we could copy the same code into ‑touchesMoved:withEvent: and update the ship’s position property there, but there’s a simpler way with Sprite Kit.

Let’s start with a property that keeps track of the touch that we received until the touch ends. Add this class extension to the top of the RCWMyScene.m file above the @implementation definition:

01-SpriteIntro/step04/SpaceRun/RCWMyScene.m
 
@interface​ RCWMyScene ()
 
@property​ (nonatomic, weak) UITouch *shipTouch;
 
@end

We’re declaring ...

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.