在 iPhone 上制作无限循环的弹跳球动画的最佳方法是什么?
我正在开发一款 iPhone 游戏,其中鸟类会弹跳。
我已经设置了用于为飞鸟的翅膀设置动画的图像,如下所示:
[imgBird[i] setAnimationImages:birdArrayConstant];
[imgBird[i] setAnimationDuration:1.0];
[imgBird[i] startAnimating];
现在,我如何让鸟移动是使用 NSTimer 每 0.03 秒触发一次,这会从 imgBird[i] 的 x 或 y 坐标中加/减 1 。中心。
我从这里学会了这样做。 http://icodeblog .com/2008/10/28/iphone-programming-tutorial-animating-a-ball-using-an-nstimer/
但问题是,一旦另一个计时器(用于移动我的船当我停止移动船时,以同样的方式射击并返回到原始速度。
除了 NSTimer 之外,还有更好的方法让小鸟保持移动吗?
鸟的运动是无限循环的。
I am developing an iPhone game in which birds bounce.
I have set up the images for animating the wings of the flying bird like this:
[imgBird[i] setAnimationImages:birdArrayConstant];
[imgBird[i] setAnimationDuration:1.0];
[imgBird[i] startAnimating];
Now how I make the bird move is to use an NSTimer to fire every 0.03 seconds which adds/subtracts 1 from the x or y coordinate from imgBird[i].center.
I learnt about doing it like this from here. http://icodeblog.com/2008/10/28/iphone-programming-tutorial-animating-a-ball-using-an-nstimer/
But the issue is the birds slow down as soon as another timer (for moving my ship the same way) fires and returns back to original speed as i stop moving the ship.
Is there a better way to keep the bird moving except NSTimer?
The movement of bird is an infinite loop.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要将 CoreGraphics 和 QuartzCore 框架导入到您的项目中。
将这些行添加到文件的顶部。
...
希望有点帮助。
You will need to import CoreGraphics and the QuartzCore frameworks into you project.
Add these lines to the top of your file.
...
Hope that helps a bit.
从你的问题来看,你似乎使用了多个计时器。 只需使用一个即可为所有对象添加动画效果。 在某个地方,当您的应用程序启动时,请执行以下操作:
然后,在主循环中,执行如下操作:
另外,您应该将
somex
和somey
计算为计时器间隔的函数。 这样,如果你改变它,你的动画看起来仍然是一样的。From your question it seems you are using more than one timer. Use just one to animate all your objects. Somewhere, when your app starts, have this:
Then, in main loop, something like this:
Also, you should compute
somex
andsomey
as a function of your timer interval. In that way, if you change it, your animation will still look the same.您需要使用核心动画而不是手动移动小鸟。 查看文档中的 CAAnimation 。 您基本上设置了一个动画,您希望它移动的路径,然后告诉它运行。 它会处理剩下的事情。 它还支持宽松政策,这将使步伐放缓和加速,因此看起来更自然。
You'll want to use Core Animation instead of manually moving the birds. Take a look at CAAnimation in the docs. You basically set up an animation, the path you want it to move along, and just tell it to run. It'll take care of the rest. It also has support for easing which will make the pace slow down and accelerate so it looks more natural.