iOS 版 Cocos2d 中的流畅动画

发布于 2024-12-17 07:56:55 字数 603 浏览 2 评论 0原文

我使用以下代码在 iOS 设备的屏幕上移动一个简单的 CCSprite

[self schedule:@selector(update:) interval:0.0167];

- (void) update:(ccTime) delta {
    CGPoint currPos = self.position;
    currPos.x += xVelocity;
    currPos.y += yVelocity;

    self.position = currPos;
}

这可以工作,但动画不流畅。 如何提高动画的流畅度?

我的场景非常简单(只有一个带有背景图像的全屏 CCSprite 和一个相对较小的 CCSprite< /code> 移动缓慢)。

我已经记录了 ccTime 增量,但它并不一致(它几乎总是大于我指定的间隔 0.0167...有时高达 4 倍)。

我考虑过根据增量时间调整更新方法中的运动(较大的增量 => 较大的运动等)。然而,考虑到我的场景的简单性,似乎有更好的方法(以及我可能缺少的基本方法)。

I move a simple CCSprite around the screen of an iOS device using this code:

[self schedule:@selector(update:) interval:0.0167];

- (void) update:(ccTime) delta {
    CGPoint currPos = self.position;
    currPos.x += xVelocity;
    currPos.y += yVelocity;

    self.position = currPos;
}

This works however the animation is not smooth. How can I improve the smoothness of my animation?

My scene is exceedingly simple (just has one full-screen CCSprite with a background image and a relatively small CCSprite that moves slowly).

I've logged the ccTime delta and it's not consistent (it's almost always greater than my specified interval of 0.0167... sometimes up to a factor of 4x).

I've considered tailoring the motion in the update method to the delta time (larger delta => larger movement etc). However given the simplicity of my scene it's seems there's a better way (and something basic that I'm probably missing).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

最偏执的依靠 2024-12-24 07:56:55

调度程序将尝试根据您的时间间隔来适应和调用您的选择器,但如果有其他进程正在运行,它可能会更早或更晚(因此会出现不一致)。

相反,将 xVelocity 和 yVelocity 乘以 delta - 这应该将速度缩放为更平滑的运动。

例如:

- (void) update:(ccTime) delta {
    CGPoint currPos = self.position;
    currPos.x += (xVelocity * delta);
    currPos.y += (yVelocity * delta);

    self.position = currPos;
}

The scheduler will try to accommodate and call your selector as per your interval but if there are other processes running, it can be earlier or later (hence why the inconsistency).

Instead, multiply your xVelocity and yVelocity by delta - this should scale the velocities into a far smoother motion.

For example:

- (void) update:(ccTime) delta {
    CGPoint currPos = self.position;
    currPos.x += (xVelocity * delta);
    currPos.y += (yVelocity * delta);

    self.position = currPos;
}
鲜血染红嫁衣 2024-12-24 07:56:55

尝试使用默认的 [self ScheduleUpdate] 方法,而不是像您正在做的那样直接调用它,看看这是否有区别。此方法是针对您正在做的事情而设计的,并且可能会更顺利。

Try using the default [self scheduleUpdate] method rather than calling it directly as you are doing, see if that makes a difference. This method is designed for what you are doing and may be smoother.

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