Cocos2d - 生涩的滚动运动(遵循CCNode)

时间:2015-03-14 12:10:56

标签: ios objective-c iphone cocos2d-iphone box2d

我有一个关于平滑滚动实现的问题。在我的游戏中我有一个水平滚动。当前的实现如下所示:

//Update method
float offset = _hero.position.x;
float offsetY = _hero.position.y;
float scaleToCalculateWith = _follower.scale;

float posX = (_winSize.width/4-offset*scaleToCalculateWith);
float posY = (_winSize.height/2-offsetY*scaleToCalculateWith);

//16px is the general value between the follower and the 
//hero position at a velocity of 30
if(_lastX - _follower.position.x > 16){
    NSLog(@"%f", _lastX - _follower.position.x);
}
_lastX = _follower.position.x;

//_follower is just a CCNode
_follower.position = ccp(posX, posY);

我使用Box2D作为物理学。在创建模拟以重现抖动。在模拟中,我有一个没有任何重力的世界和一个速度为b2Vec2(0, 30)的圆形。

如您所见,我记录了关注节点的最后和当前位置之间的差异。日志看起来像这样:

2015-03-14 13:00:23.921 jitterysimulation[1567:60b] 53.312500
2015-03-14 13:00:26.554 jitterysimulation[1567:60b] 21.250000
2015-03-14 13:01:00.142 jitterysimulation[1567:60b] 64.000000
2015-03-14 13:01:04.360 jitterysimulation[1567:60b] 22.406250
2015-03-14 13:01:04.613 jitterysimulation[1567:60b] 42.750000
2015-03-14 13:01:06.321 jitterysimulation[1567:60b] 21.343750
2015-03-14 13:01:08.087 jitterysimulation[1567:60b] 21.406250
2015-03-14 13:01:09.626 jitterysimulation[1567:60b] 21.343750
2015-03-14 13:01:16.722 jitterysimulation[1567:60b] 21.343750
2015-03-14 13:01:17.654 jitterysimulation[1567:60b] 21.250000
2015-03-14 13:01:21.808 jitterysimulation[1567:60b] 21.250000
2015-03-14 13:01:25.090 jitterysimulation[1567:60b] 53.000000
2015-03-14 13:01:45.654 jitterysimulation[1567:60b] 42.625000
2015-03-14 13:02:00.126 jitterysimulation[1567:60b] 31.968750
2015-03-14 13:02:00.144 jitterysimulation[1567:60b] 53.312500
2015-03-14 13:02:06.204 jitterysimulation[1567:60b] 42.656250
2015-03-14 13:02:09.654 jitterysimulation[1567:60b] 21.343750

跟随者有时会跳跃。在iphone 4(设备)上,模拟运行稳定,恒定FPS为60,CPU使用率低于30%。 Box2d更新采用与跟随者位置更新相同的更新方法。

我怎样才能防止这些口吃?这是游戏的一般问题吗?

如果有人有兴趣,我会在Dropbox上传模拟。

提前谢谢。

0 个答案:

没有答案