I want to test for when the player's body moves past a certain point on the screen, and call the gameOver node when they do.
I tried putting it in the touches began method but then every time the user touched after that it would continue to add gameOver nodes.
I also tried putting it in the -(void)update:(CFTimeInterval)currentTime
method but it added a gameOver node every frame.
This is the code that tests when the user is out of moves/crosses a certain point on the screen:
//Check to see if moves are at 0.
if (movesRemaining < 0) {
movesRemaining = 0;
}
if (_fish.position.y < CGRectGetMidY(self.frame)-160) {
movesRemaining = 0;
}
if (movesRemaining == 0) {
[self runAction:[SKAction sequence:@[
[SKAction waitForDuration:0.5],
[SKAction runBlock:^{
[self outOfMoves];
}],
]]];
}
Which calls this:
-(void)outOfMoves {
SKShapeNode *gameOverNode = [SKShapeNode node];
[gameOverNode setPath:CGPathCreateWithRoundedRect(CGRectMake(-140, -125, 280, 250), 10, 10, nil)];
gameOverNode.strokeColor = gameOverNode.fillColor = [UIColor colorWithRed:192/255. green:47/255. blue:47/255. alpha:1.0];
gameOverNode.position = CGPointMake(CGRectGetMidX(self.frame), -150);
SKLabelNode *outOfMoves = [SKLabelNode labelNodeWithFontNamed:@"DIN Condensed"];
outOfMoves.position = CGPointMake(0,80);
outOfMoves.fontSize = 40;
outOfMoves.text = @"OUT OF MOVES";
SKLabelNode *reTry = [SKLabelNode labelNodeWithFontNamed:@"DIN Condensed"];
reTry.position = CGPointMake(0, 30);
reTry.fontSize = 40;
reTry.fontColor = [UIColor colorWithRed:255/255. green:156/255. blue:0/255. alpha:1.0];
reTry.name = @"reTry";
reTry.text = @"TRY AGAIN";
[gameOverNode addChild:outOfMoves];
[gameOverNode addChild:reTry];
[self addChild:gameOverNode];
[gameOverNode runAction:[SKAction moveToY:CGRectGetMidY(self.frame)+40 duration:0.5]];
}
Where is the best place to put the condition of checking where on the screen the user is / when they are at 0 lives in a sprite-kit code?
It accurately tested when it was updated with frames, but added too many.
I also tried using a "performSelector" call inside the didMoveToView method but that didn't work either..
(code:
-(void)didMoveToView:(SKView *)view {
[self performSelector:@selector(checkNow) withObject:nil afterDelay:1.0];
}
)
So how can I do this?