按下暂停按钮时如何停止位置更新?

时间:2013-06-12 12:02:14

标签: ios objective-c cocos2d-iphone

我有这个代码使粒子跟随我的b2body对象:

-(void)updateMagicFruitParticlePosition:(ccTime)dt{

    if (!gameManager.isPaused) {

         magicFruitParticle.position = especialObject.ccPosition;
    }
    else
       [self unschedule:_cmd];
}

代码有效,但是当按下暂停按钮并且播放器重新开始游戏时,它会崩溃。理论上它应该在按下按钮后停止,但即使方法已经未安排,它也恰好在更新过程中。

下面的代码是按下暂停按钮时发生的情况:

-(void)pauseGame:(id)sender{

    if (!gameManager.isPaused ) {

        gameManager.isPaused = YES;

        [gameManager pauseAllSoundEffects];
        [gameManager pauseBackgroundMusic];
        [[CCDirector sharedDirector] pause];

        pauseLayer = [CCLayerColor layerWithColor:ccc4(0, 0, 0, 125) width:screenSize.width height:screenSize.height];
        pauseLayer.position = CGPointZero;
        [self addChild:pauseLayer z:kSecondLayer];

        backgroundPauseA = [CCSprite spriteWithSpriteFrameName:@"BackgroundPauseA"];
        backgroundPauseA.position = ccp(screenSize.width /2, screenSize.height /2);
        [self addChild:backgroundPauseA z:kSuperiorLayer];

        backgroundPauseB = [CCSprite spriteWithSpriteFrameName:@"BackgroundPauseB"];
        backgroundPauseB.position = ccp(screenSize.width /2, screenSize.height /2);
        [self addChild:backgroundPauseB z:kSuperiorLayer];

        [self pauseMenu]; 
    }
}

还有其他方法可以使粒子效果跟随对象或精灵吗?或者我该如何解决它?

任何帮助将不胜感激!

谢谢!

0 个答案:

没有答案