Game Development Reference
In-Depth Information
Or, you may just follow us. Change the didMoveToView method to the following:
- (void) didMoveToView:(SKView *)view
{
UILongPressGestureRecognizer *tapper =
[[UILongPressGestureRecognizer alloc] initWithTarget:self action:@
selector(tappedScreen:)];
tapper.minimumPressDuration = 0.1;
[view addGestureRecognizer:tapper];
}
Here, we initialize and use the new gesture recognizer that will set the player's state
to accelerating , meaning that the player is moving up now. To handle this, add a
new property to ERGPlayer.h @property (assign) BOOL accelerating; .
After this, add the following code to ERGMyScene.m :
- (void) tappedScreen:(UITapGestureRecognizer *)recognizer
{
ERGPlayer *player = (ERGPlayer *)[self
childNodeWithName:@"player"];
if (recognizer.state == UIGestureRecognizerStateBegan) {
player.accelerating = YES;
}
if (recognizer.state == UIGestureRecognizerStateEnded) {
player.accelerating = NO;
}
}
As said before, we need this method to set an accelerated property on the
player—if it is active, we will apply a certain force on each frame of the player.
To do this, add the following to your update method:
[self enumerateChildNodesWithName:@"player" usingBlock:^(SKNode
*node, BOOL *stop) {
ERGPlayer *player = (ERGPlayer *)node;
if (player.accelerating) {
[player.physicsBody applyForce:CGVectorMake(0,
playerJumpForce * timeSinceLast)];
}
}];
 
Search WWH ::




Custom Search