Question

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?

Était-ce utile?

La solution

IMO, -(void)update:(CFTimeInterval)currentTime is a good place to put that check. However, your game over nodes are being created multiple times because the check repeats multiple times while the outOfMoves method is still running.

I suggest you add a BOOL variable that is originally NO and changed to YES when the outOfMoves method is called. Then back in your update:, check that this variable is NO before calling outOfMoves again. That way you won't keep calling the method while it's running. Also, you can set this variable back to NO when your method is completed and you're ready again.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top