【问题标题】:How do I call a particular function at the end of a runAction?如何在 runAction 结束时调用特定函数?
【发布时间】:2011-10-31 18:38:14
【问题描述】:

我正在对精灵执行 runAction 以移动到某个位置。

[[crBalls[cb.count] getball] runAction:[CCSequence actions:[CCMoveTo actionWithDuration:timeToTravel position:ccp(xIntercept,yIntercept)],[CCMoveTo actionWithDuration:0.05 position:FD],nil]];

一旦精灵移动到所需的位置(FD),那么我想在那个时候调用一个函数。现在我正在安排一个选择器在“timeToTravel”延迟后调用,这是上述操作完成执行所花费的时间。(我使用调度器而不是 performSelector,因为执行选择器更容易出现问题)

[self schedule:@selector(placeThatDamnBall) interval:timeToTravel+0.05];

-(void) placeThatDamnBall
{
[self unschedule:@selector(placeThatDammBall)];
[self ballPlacedIn:FD.x :FD.y :cb.type : cb.count];
}

但这并不完全可靠,并且在极少数情况下可能会导致问题,即函数可能在精灵到达目的地之前被调用。有什么方法可以避免调用选择器并能够在精灵真正到达目的地后调用函数?

谢谢

【问题讨论】:

    标签: function cocos2d-iphone selector scheduler ccsprite


    【解决方案1】:

    在序列末尾添加CCCallFunc

    [[crBalls[cb.count] getball] runAction:
        [CCSequence actions:
            [CCMoveTo actionWithDuration:timeToTravel position:ccp(xIntercept,yIntercept)],
            [CCMoveTo actionWithDuration:0.05 position:FD],
            [CCCallFunc actionWithTarget:self selector:@selector(placeThatDamnBall)],
            nil]];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-09-12
      • 2023-03-27
      • 2012-02-08
      • 1970-01-01
      • 1970-01-01
      • 2013-12-24
      • 2016-12-19
      相关资源
      最近更新 更多