我发现了一些有用的东西。
tl;dr
基本思想是自己跟踪精灵的位置,然后定期检查它们,看看它们中的任何一个自上次检查以来是否移动过。
加长版
我创建了一个 CCNode 的子类,其类名为 Piece。
这些是我添加到物理世界的对象。
@implementation Piece {
float _previousX;
float _previousY;
}
-(void)updatePreviousScreenXandY{
_previousX = self.position.x;
_previousY = self.position.y;
}
-(BOOL)hasntMoved{
float currentX = self.position.x;
float currentY = self.position.y;
if ( currentX == _previousX && currentY == _previousY ) {
return TRUE;
}else{
return FALSE;
}
}
这是在我的 CCNode 中充当游戏场景
-(void)doStuffAfterPiecesStopMoving:(NSTimer*)timer{
BOOL noPiecesHaveMoved = TRUE;
for (int i = 0; i < [[_physicsWorld children] count]; i++) {
if ( [[_physicsWorld children][i] hasntMoved] == FALSE ) {
noPiecesHaveMoved = FALSE;
break;
}
}
if ( noPiecesHaveMoved ) {
[timer invalidate];
NSLog(“Pieces have stopped moving”);
}else{
NSLog(“Pieces are still moving”);
[self updateAllPreviousPiecePositions];
}
}
-(void)updateAllPreviousPiecePositions{
for (int i=0; i < [[_physicsWorld children] count]; i++) {
Piece *piece = (Piece*)[_physicsWorld children][i];
[piece updatePreviousScreenXandY];
}
}
我要做的就是
[NSTimer scheduledTimerWithTimeInterval:TIME_BETWEEN_CHECKS
target:_gamePlay
selector:@selector(doStuffAfterPiecesStopMoving:)
userInfo:nil
repeats:YES];
它会在所有 Piece 节点停止移动后运行我想要的任何代码。
让它正常工作的关键是让 Chipmunk 空间的 sleepTimeThreshold 和上面的计时器的时间值尽可能低。
我的实验表明以下设置可以正常工作,但任何较低的设置都会导致问题(即碰撞无法正常发生):
sleepTimeThreshold = 0.15
我的计时器 = 0.05
如果有人对上述代码有不同/更好的解决方案或改进,请发布。