我正在使用此代码根据UIDevice的加速度移动“下降”精灵。 它工作得很好,但是非常迟缓,这意味着精灵的位置之间会有跳跃。
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
if(paused)
return;
directionPoint.x += acceleration.x * 100;
}
- (void)update:(ccTime)dt {
if(paused)
return;
directionPoint.y = -500*dt;
CGPoint playerDestinationPoint = player.position;
playerDestinationPoint.x += directionPoint.x;
playerDestinationPoint.y += directionPoint.y;
if(playerDestinationPoint.x < player.contentSize.width/2) {
playerDestinationPoint.x = player.contentSize.width/2;
}
else if(playerDestinationPoint.x > ([[CCDirector sharedDirector] winSize].width - play er.contentSize.width/2)) {
playerDestinationPoint.x = [[CCDirector sharedDirector] winSize].width - player.contentSize.width/2;
}
[player setPosition:playerDestinationPoint];
directionPoint = CGPointMake(0.0, 0.0);
}
我尝试以这种方式使用CCMoveTo:
- (void)update:(ccTime)dt {
if(paused)
return;
[player stopAllActions];
directionPoint.y = -500*dt;
CGPoint playerDestinationPoint = player.position;
playerDestinationPoint.x += directionPoint.x;
playerDestinationPoint.y += directionPoint.y;
if(playerDestinationPoint.x < player.contentSize.width/2) {
playerDestinationPoint.x = player.contentSize.width/2;
}
else if(playerDestinationPoint.x > ([[CCDirector sharedDirector] winSize].width - player.contentSize.width/2)) {
playerDestinationPoint.x = [[CCDirector sharedDirector] winSize].width - player.contentSize.width/2;
}
id actionMoveTo = [CCMoveTo actionWithDuration:dt position:playerDestinationPoint];
[player runAction:actionMoveTo];
directionPoint = CGPointMake(0.0, 0.0);
}
但是精灵根本不动。任何提示?
MFG,
侧击
答案 0 :(得分:0)
在您的第一次更新方法中尝试:
playerDestinationPoint.x += directionPoint.x*dt;
由于dt间隔可能会根据性能而改变。