Game Development Reference
In-Depth Information
Figure 3-1 . Adding an exception breakpoint to every project is an essential debugging aid
Now that touch events are enabled and the _playerNode reference is assigned, you can
move the player to a touch location easily. For the moment, the code in Listing 3-4 will
do. Add the Listing 3-4 code to the GameScene.m implementation file below the
loadLevelNamed: method.
Listing 3-4 . Teleporting the player to the touch location
-(void) touchBegan:(UITouch *)touch withEvent:(UIEvent
*)event
{
_playerNode.position = [touch locationInNode:self];
}
This takes the location of the touch and transforms it from the touch's Cocoa coordinate
system (origin in the upper-left corner) to the OpenGL coordinate system used by Co-
cos2D (origin in the lower-left corner). It also converts the position to the local coordinate
space of the supplied node, in this instance self. The result is directly applied to the
_playerNode , which will now, when you build and run, instantly zap to where you tap
on the screen.
Assigning the Level-Node ivar
For the next steps, you need to have a reference to the level node. Instead of getting it by
name like the player, you can also have SpriteBuilder assign it to the GameScene class
directly, provided that the class already declares an ivar or property of the same name.
Which it already does since Listing 3-1 .
Search WWH ::




Custom Search