减少 CCSprite 速度时的奇怪行为
我的 CCSprite 子类有两个名为 velX 和 velY 的浮动属性,用于跟踪玩家的速度。当它在游戏层中滑动时,我会计算出滑动的矢量方向并开始玩家朝那个方向移动。在我的游戏层的 -update:(ccTime)dt
方法中,我调用 [player update:(ccTime)dt
(我知道,同名)并在CCSprite 类有这段代码。
// move
self.position = ccp(self.position.x + self.velX * dt, self.position.y + self.velY * dt);
// do edge checking
// decelerate
#define kDeceleration .95
NSLog(@">velX = %1.1f, velY = %1.1f, fabs(velX) = %1.1f, fabs(velY) = %1.1f", self.velX, self.velY, fabs(velX), fabs(velY));
self.velX *= kDeceleration;
if (fabs(self.velX < 1.0)) self.velX = 0.0;
self.velY *= kDeceleration;
if (fabs(self.velY < 1.0)) self.velY = 0.0;
每次游戏层调用 update
时都会调用此函数,并传递游戏循环使用的相同时间增量。
如果我朝正方向滑动,效果会很好,但如果任一滑动组件为负(向左或向下),则它们会直接归零。如果我去掉条件,一切正常,我只是认为乘以零应该比不断乘以非常小的小数更便宜。
我必须错误地使用 fabs() 函数...
编辑:两次滑动的一些示例输出,两个轴上一个为正,两个轴上一个为负。
>velX = 21.8, velY = 20.6, fabs(velX) = 21.8, fabs(velY) = 20.6
>velX = 20.7, velY = 19.6, fabs(velX) = 20.7, fabs(velY) = 19.6
>velX = 19.7, velY = 18.6, fabs(velX) = 19.7, fabs(velY) = 18.6
>velX = 18.7, velY = 17.7, fabs(velX) = 18.7, fabs(velY) = 17.7
>velX = 17.8, velY = 16.8, fabs(velX) = 17.8, fabs(velY) = 16.8
>velX = -23.0, velY = -19.3, fabs(velX) = 23.0, fabs(velY) = 19.3
>velX = 0.0, velY = 0.0, fabs(velX) = 0.0, fabs(velY) = 0.0
>velX = 0.0, velY = 0.0, fabs(velX) = 0.0, fabs(velY) = 0.0
>velX = 0.0, velY = 0.0, fabs(velX) = 0.0, fabs(velY) = 0.0
>velX = 0.0, velY = 0.0, fabs(velX) = 0.0, fabs(velY) = 0.0
My CCSprite subclass has two float properties called, velX and velY to keep track of the velocity of my player. When it gets swiped in the game layer, I figure out the vector direction of the swipe and start the player moving that way. In the -update:(ccTime)dt
method of my game layer, I call [player update:(ccTime)dt
(I know, same name) and that method in the CCSprite class has this code.
// move
self.position = ccp(self.position.x + self.velX * dt, self.position.y + self.velY * dt);
// do edge checking
// decelerate
#define kDeceleration .95
NSLog(@">velX = %1.1f, velY = %1.1f, fabs(velX) = %1.1f, fabs(velY) = %1.1f", self.velX, self.velY, fabs(velX), fabs(velY));
self.velX *= kDeceleration;
if (fabs(self.velX < 1.0)) self.velX = 0.0;
self.velY *= kDeceleration;
if (fabs(self.velY < 1.0)) self.velY = 0.0;
This gets called every time the game layer calls update
and passes in the same time increment that the game loop uses.
It works great if I swipe in a positive direction, but if either of the swipe components are negative (left or down) then they go straight to zero. If I take out the conditionals, everything works, I just figured that multiplying by zero should be cheaper than constantly multiplying by very small decimals.
I must be using the fabs()
function incorrectly...
EDIT: Some sample output from two swipes, one positive in both axes, and one negative in both.
>velX = 21.8, velY = 20.6, fabs(velX) = 21.8, fabs(velY) = 20.6
>velX = 20.7, velY = 19.6, fabs(velX) = 20.7, fabs(velY) = 19.6
>velX = 19.7, velY = 18.6, fabs(velX) = 19.7, fabs(velY) = 18.6
>velX = 18.7, velY = 17.7, fabs(velX) = 18.7, fabs(velY) = 17.7
>velX = 17.8, velY = 16.8, fabs(velX) = 17.8, fabs(velY) = 16.8
>velX = -23.0, velY = -19.3, fabs(velX) = 23.0, fabs(velY) = 19.3
>velX = 0.0, velY = 0.0, fabs(velX) = 0.0, fabs(velY) = 0.0
>velX = 0.0, velY = 0.0, fabs(velX) = 0.0, fabs(velY) = 0.0
>velX = 0.0, velY = 0.0, fabs(velX) = 0.0, fabs(velY) = 0.0
>velX = 0.0, velY = 0.0, fabs(velX) = 0.0, fabs(velY) = 0.0
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信这个块是一个语法拼写错误:
应该是这样:
你需要在比较运算符之前关闭 fabs 括号
p/s:没关系,每个人都经常犯这种错误: )
I believe this block is a syntax typo:
It should be this instead:
You need to close the
fabs
parentheses before the comparison operatorp/s: it's okay, everyone does this kind of mistake all the time :)