避免"Physics Space Locked"错误

在一些cocos2d中使用物理引擎的代码中,往往会出现如下错误:

Aborting due to Chipmunk error: You cannot manually reindex objects while the space is locked. Wait until the current query or step is complete.Failed condition: !space->locked

该错误是一个著名的典型问题在大多数物理引擎中出现:在碰撞事件中,你不能修改物理世界(在Chipmunk中是space)中的特定部分.在这个例子中,播放动画将会修改节点的旋转因此旋转节点的物理对象.Chipmunk并不喜欢这样做,因为物理世界(space)被锁住了!意思是当前处理的物体需要连贯的状态,只有物理引擎自身允许修改它们的状态.(而不是在SpriteBuilder中用动画修改).

幸运的是我们可以快速简单的修正这个错误…见如下代码:

-(void)didTriggerWithNode:(CCNode *)activator{ [self scheduleBlock:^(CCTimer *timer){ [self.animationManager runAnimationsForSequenceNamed:_timelineName];
    } delay:0.0];
}

用0延时推迟调度一个block,直到Cocos2D调度器运行所有调度blocks的下一时刻中才会运行,至少它将会延时到Cocos2D调度器的下一帧才会运行.

你可能感兴趣的:(cocos2d,locked,physics,chipmunk,shace)