Game Development Reference
In-Depth Information
LearnSpriteBuilder[54443:70b] _levelNode = <CCNode
= 0x9f77b30 | Name = >
Moving the Player with CCActionMoveTo
To move the player smoothly to the desired location, you can replace the contents of the
touchBegan:withEvent: method with the action-based movementcode in Listing
3-5 .
Listing 3-5 . Moving the player smoothly with a move action
-(void) touchBegan:(UITouch *)touch withEvent:(UIEvent
*)event
{
CGPoint pos = [touch locationInNode:_levelNode];
CCAction* move = [CCActionMoveTo actionWithDuration:0.2
position:pos];
[_playerNode runAction:move];
}
First, the touch location is converted relative to the _levelNode . This is important to
allow the player to move about the full extents of the _levelNode (4000x500 points)
rather than being confined to the screen space (568x320 points on 4-inch iPhones or
1024x768 points on iPads). However, this isn't reflected by the game yet because there is
no scrolling; hence, the player can't move further than the initial screen position.
Next, a CCActionMoveTo is created that moves a node to a given coordinate over time.
This action is then run on the _playerNode . Note that if you increase the duration you
will notice that the move actions don't stack and the player won't end up where you last
tapped. To fix that, you'd have to assign a tag—an arbitrary integer number of your
choice—to the action. Using this tag, you can stop the existing action with the same tag
before running the new one. Listing 3-6 shows the updated code with this improvement.
Listing 3-6 . Stop an action by tag to avoid having multiple move actions running simul-
taneously
-(void) touchBegan:(UITouch *)touch withEvent:(UIEvent
*)event
Search WWH ::




Custom Search