Question

i'm creating an game where there is a pause button. For checking wether its touched i'm checking the location of the touch and if its equal to the paused button name.

When the pausedButton is clicked its call a method which pause the scene.

The problem is that in the touchBegan method whenever you touch the screen it apply an impulse, so when i press the pauseButton and unpause it the applyforce will come after. This is not ideal for the game. I've tried with a bool like shouldImpulse, but havent got it to work. here is my touchedBegan method:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInNode:self];
    SKNode *node = [self nodeAtPoint:location];

    if ([node.name isEqualToString:@"home"]) {

        SKTransition *reveal = [SKTransition fadeWithDuration:2.0 ];
        Menu *newScene = [[Menu alloc] initWithSize: CGSizeMake(self.size.width,self.size.height)];
        //  Optionally, insert code to configure the new scene.
        [self.scene.view presentScene: newScene transition: reveal];
    }

    if ([node.name isEqualToString:@"pause"]) {
        [self pausedMenu];


    }

    if ([node.name isEqualToString:@"start"]) {
        [self startMenu];


    }

    showpipes = showpipes + 1;

    if (showpipes == 1) {
    self.physicsWorld.gravity = CGVectorMake( 0.0, -5.0 );

        SKAction* spawn = [SKAction performSelector:@selector(spawnPipes) onTarget:self];
        SKAction* delay = [SKAction waitForDuration:2.0];
        SKAction* spawnThenDelay = [SKAction sequence:@[spawn, delay]];
        SKAction* spawnThenDelayForever = [SKAction repeatActionForever:spawnThenDelay];
        [self runAction:spawnThenDelayForever];

    }

    started = 1;


        if (started == 1) {

            mover.physicsBody.restitution = 0.0;
            mover.physicsBody.velocity = CGVectorMake(0, 0);
            [mover.physicsBody applyImpulse:CGVectorMake(0, 15)];


        }




}
Was it helpful?

Solution

You can do that with a simple IF, ELSE IF, ELSE:

if([node.name isEqualToString:@"home"])
{
    // do stuff...
} else if ([node.name isEqualToString:@"pause"])
{
    // do stuff...
} else if ([node.name isEqualToString:@"start"])
{
    // do stuff...
} else
{
    // do whatever else here...
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top