Game Development Reference
In-Depth Information
Moving the Player Through Physics
Because a physics-enabled node with a dynamic physicsBody should not run any move,
rotate, scale, or skew actions, you need to replace the move action with proper physics
movement.
The touch events need to change from executing a move action as soon as a touch begins
to merely changing a flag. If this flag is set, it will cause a function called from the up-
date: method to accelerate the player in a given direction.
In GameScene.m , add the following four ivars within the @implementation brack-
ets, as seen in Listing 4-1 .
Listing 4-1 . Additional ivars needed for physics movement
@implementation GameScene
{
__weak CCNode* _levelNode;
__weak CCPhysicsNode* _physicsNode;
__weak CCNode* _playerNode;
__weak CCNode* _backgroundNode;
CGFloat _playerNudgeRightVelocity;
CGFloat _playerNudgeUpVelocity;
CGFloat _playerMaxVelocity;
BOOL _acceleratePlayer;
}
Now locate the touchBegan:withEvent: method and replace its body with the fol-
lowing:
-(void) touchBegan:(UITouch *)touch withEvent:(UIEvent
*)event
{
_acceleratePlayer = YES;
}
This enables the “user is currently touching the screen” mode.
Of course, you also need to end this mode. To do so, you have to add the touchesEn-
ded: and touchesCancelled: methods as seen in Listing 4-2 .
Search WWH ::




Custom Search