停止 SKSpriteKit 点击的冲动 [英] stop impulse on SKSpriteKit click

查看:7
本文介绍了停止 SKSpriteKit 点击的冲动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的游戏中创建一个暂停按钮.在你点击屏幕的那一刻,它会在移动精灵节点上施加一个脉冲.问题是当我点击暂停按钮时它仍然会施加冲动.如何在不产生冲动的情况下创建暂停按钮.

i'm trying to create an pause button in my game. at the moment when you click on the screen it will apply an impulse on the mover spriteNode. The problem is that it still applying the impulse when i click the pause button. How can i create an pause button without creating the impulse also.

我尝试将 if 语句更改为 else if,然后将最后一个脉冲部分作为 else 部分,但这不起作用.

i've tried changing the if statements to an else if and then making the last impulse part the else part, but this wont work.

touchBegan 方法:

touchBegan method:

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

    //if fire button touched, bring the rain
    if ([node.name isEqualToString:@"repeat"]) {
        SKTransition *reveal = [SKTransition fadeWithDuration:2.0 ];
        MyScene *newScene = [[MyScene 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:@"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 (noImpulse == 1) {
        if (started == 1) {
            mover.physicsBody.restitution = 0.0;
            mover.physicsBody.velocity = CGVectorMake(0, 0);
            [mover.physicsBody applyImpulse:CGVectorMake(0, 15)];
        }
    }

}

暂停菜单方法:

-(void)pausedMenu
{

self.scene.paused = YES;
mover.scene.paused = YES;
[repeatButton removeFromParent];
[homeButton removeFromParent];
[menuBackground removeFromParent];
menuBackground = [SKSpriteNode spriteNodeWithColor:[SKColor colorWithWhite:0.0 alpha:0.3] size:CGSizeMake(self.frame.size.width*2, self.frame.size.height*2)];


[self addChild:menuBackground];

[pauseButton removeFromParent];

startButton = [SKSpriteNode spriteNodeWithImageNamed:@"staart"];
startButton.name = @"start";
startButton.position = CGPointMake(20, self.size.height-20);
startButton.size = CGSizeMake(40, 40);
[self addChild:startButton];



repeatButton = [SKSpriteNode spriteNodeWithImageNamed:@"repeat"];
repeatButton.name = @"repeat";
repeatButton.position = CGPointMake(self.size.width/2+30, self.size.height/2);
repeatButton.size = CGSizeMake(repeatButton.size.width/2, repeatButton.size.height/2);
[self addChild:repeatButton];

homeButton = [SKSpriteNode spriteNodeWithImageNamed:@"home"];
homeButton.name = @"home";
homeButton.position = CGPointMake(self.size.width/2-30, self.size.height/2);
homeButton.size = CGSizeMake(homeButton.size.width/2, homeButton.size.height/2);
[self addChild:homeButton];

}

推荐答案

查看https://developer.apple.com/library/ios/documentation/SpriteKit/Reference/SKView/Reference/Reference.html#//apple_ref/occ/instp/SKView/paused如果您暂停包含游戏的 SKView,则所有状态都将真正暂停,您将能够在暂停菜单被关闭后恢复它.您必须在另一个 SKView 中显示暂停菜单,然后是您的游戏所在的那个.

Check out https://developer.apple.com/library/ios/documentation/SpriteKit/Reference/SKView/Reference/Reference.html#//apple_ref/occ/instp/SKView/paused If you pause your SKView that contains the game, all the state will be truly paused and you will be able to resume it after the pause menu has been dismissed. You'll have to present the pause menu in another SKView then the one that your game is contained in.

这篇关于停止 SKSpriteKit 点击的冲动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆