【问题标题】:Replacing Scene and Stop All Animations and Sounds替换场景并停止所有动画和声音
【发布时间】:2011-07-05 21:16:58
【问题描述】:

我的场景中有一个主页按钮,按下它会进入主页菜单。我使用 replaceScene 将当前场景(游戏场景)替换为 HomeMenu 场景。由于某种原因,当我更换场景时,游戏场景中发生的动作和声音并没有停止。我已经尝试了以下代码,但是当我在主菜单中时,我仍然可以听到游戏场景的动作和声音。

// 主菜单时触发 点击! -(void) homeMenuClicked:(CCMenuItem *) item { NSLog(@"home menu clicked!");

CCScene *scene = [[CCDirector sharedDirector] runningScene]; [场景 stopAllActions];

[self.layer stopAllActions];

[自我卸载声音效果];

[[CCDirector sharedDirector] 替换场景:[CCSlideInLTransition 过渡持续时间:1.0 场景:[HomeScene 场景]] ];

}

我还必须补充一点,游戏层还有一个计时器 (NSTimer) 对象,它在 2 秒内启动。

更新 2:

让我发布一些代码!我认为问题在于,当玩家猜出正确答案时,会调用以下方法:

[self updateForCorrectAnswer];

在 updateForCorrectAnswer 内部,我有一个 performSelector,它计划在 6-7 秒内触发。我相信 performSelector 是罪魁祸首。如果我能以某种方式阻止它被解雇,那么我想我会没事的。

    [self performSelector:@selector(refreshScore) withObject:nil afterDelay:7.0]; 

【问题讨论】:

  • 您确定您的旧场景(游戏层)正在发布吗?您是否在游戏层 dealloc 方法中停止计时器?
  • 你能发布更多的代码片段吗?目前的代码并不能说明太多
  • 更新了我认为是问题的帖子!

标签: iphone cocos2d-iphone scheduler


【解决方案1】:

您不应该将 NSTimer 用作 cocos2d 文档。

/* call scheduleUpdate in initializing */
[self scheduleUpdate];

它调度更新:当该节点在舞台上时每帧调用的方法。

- (void)update:(ccTime)dt
{
    /* This method is automatically called every frame. */
}

scheduleUpdateWithPriority:, schedule:interval: 也可用。

你为什么不这样使用,而不是 performSelector:withObject:afterDelay。

[self runAction:[CCSequence actions:[CCDelayTime actionWithDuration:7], [CCCallFunc actionWithTarget:self selector:@selector(refreshScore)], nil]];

【讨论】:

    【解决方案2】:

    如果使用“[self performSelector:withObject:afterDelay:]”这个方法,cocos2d不会在这个run loop中管理它。您应该使用它而不是以前的:

    [self schedule:interval:];
    

    然后在你的“homeMenuClicked:”方法中,调用这个:

    [self.layer unschedule:@selector(refreshScore)];
    

    我没有尝试过,但我认为它会更好。

    有关更多信息,您可以查看文档here

    【讨论】:

      猜你喜欢
      • 2012-11-07
      • 2013-06-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-06
      相关资源
      最近更新 更多