减少 CCSprite 速度时的奇怪行为

发布于 2024-11-08 20:34:37 字数 1607 浏览 4 评论 0原文

我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

木有鱼丸 2024-11-15 20:34:37

我相信这个块是一个语法拼写错误:

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;

应该是这样:

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;

你需要在比较运算符之前关闭 fabs 括号

p/s:没关系,每个人都经常犯这种错误: )

I believe this block is a syntax typo:

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;

It should be this instead:

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;

You need to close the fabs parentheses before the comparison operator

p/s: it's okay, everyone does this kind of mistake all the time :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文