在精灵触摸上停止Impulse

时间:2014-05-08 15:37:58

标签: ios objective-c sprite-kit

我创建了一个有暂停按钮的游戏。为了检查触摸它是否检查触摸的位置以及它是否等于暂停的按钮名称。

单击pausedButton时,会调用一个暂停场景的方法。

问题是,在touchBegan方法中,无论何时触摸屏幕,它都会施加冲动,因此当我按下pauseButton并取消暂停时,applyforce将会跟进。这对于游戏来说并不理想。我曾尝试过像shouldImpulse这样的bool,但还没有让它发挥作用。这是我感动的开始方法:

-(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)];


        }




}

1 个答案:

答案 0 :(得分:1)

你可以用一个简单的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...
}