Game Development Reference
In-Depth Information
Each method gets an NSSet class of a UITouch object (which holds the coordinates
of our touch), and the event holds information about the window and view in which
the touch was triggered as well as information about the touch. Let's see what each
of them does:
touchesBegan: : This method gets triggered each time the user touches the
screen, unrelated to whether this touch continues as a long press or not.
touchesMoved: : This method provides you with events that occurred if the
user moved a finger while on screen, and provides new touches.
touchesEnded: : This method gets called when the user takes their finger off
the screen. This can be useful, for example, if you want some animation to
play after the user removes their finger, or to re-enable physics calculations
on the node.
touchesCancelled: : This method is rarely needed, but you should
implement it anyway. It is called when some event cancels the touch,
for example, an on-screen alert, phone call, notifications (such as a push
notification or calendar notification), and others. You will likely want to
trigger the same code as in touchesEnded: .
Sprite Kit offers a few helper methods to assist us with touches and detecting nodes
in which touches happened. We will list some of them here. These are all methods
of SKScene:
[touch locationInNode:(SKNode*)node : This is an example of one of
the methods to convert location from UIView coordinates to coordinates in
SKNode. This is useful since touch objects in those methods have UIView
coordinates and we operate with Sprite Kit coordinates.
[self nodeAtPoint:(CGPoint)p] : This method returns the node in the
scene's hierarchy that intersects the provided point. This can be useful to
detect touches on certain nodes without tedious calculations.
Now that we have learned the basics, let's implement the basic functionality of touch
handling. We will move the character sprite by dragging it around. We will need to
perform a few steps to accomplish this, which are as follows:
1.
Add @property (assign) BOOL selected; to your player class in
ERGPlayer.h —we will use this to handle the state of the player sprite;
whether we should drag it when the user drags a finger on the screen
or disregard such touches.
 
Search WWH ::




Custom Search