在iPhone上使用无限循环制作弹跳球动画的最佳方法是什么? [英] What is the best way to make a bouncing ball animation with infinite loop on iPhone?

查看:460
本文介绍了在iPhone上使用无限循环制作弹跳球动画的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一款让鸟儿反弹的iPhone游戏。

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];

现在我如何使鸟移动是使用NSTimer每隔0.03秒发射一次加/减1来自imgBird [i] .center的x或y坐标。

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.

我从这里学到了这样做。 http:// icodeblog .com / 2008/10/28 / iphone-programming-tutorial-animating-a-ball-using-an-nstimer /

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.

除了NSTimer之外,还有更好的方法让鸟儿继续移动吗?

Is there a better way to keep the bird moving except NSTimer?

鸟的运动是无限的环。

The movement of bird is an infinite loop.

推荐答案

您需要将CoreGraphics和QuartzCore框架导入到项目中。

You will need to import CoreGraphics and the QuartzCore frameworks into you project.

将这些行添加到文件的顶部。

Add these lines to the top of your file.

#import <QuartzCore/QuartzCore.h>
#import <CoreGraphics/CoreGraphics.h>

...

UIImageView *bird = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bird2.png"]];

CALayer *b = bird.layer;

// Create a keyframe animation to follow a path to the projected point

CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"scale"];
animation.removedOnCompletion = NO;


// Create the path for the bounces
CGMutablePathRef thePath = CGPathCreateMutable();

// Start the path at my current location
CGPathMoveToPoint(thePath, NULL, bird.center.x, bird.center.y);
CGPathAddLineToPoint(thePath, NULL,20, 500.0);

/*  // very cool path system.
CGMutablePathRef thePath = CGPathCreateMutable();
CGPathMoveToPoint(thePath,NULL,74.0,74.0);

CGPathAddCurveToPoint(thePath,NULL,74.0,500.0,
                      320.0,500.0,
                      320.0,74.0);
CGPathAddCurveToPoint(thePath,NULL,320.0,500.0,
                      566.0,500.0,
                      566.0,74.0);
*/

//animation.rotationMode = kCAAnimationRotateAuto;

animation.path = thePath;

animation.speed = 0.011;
//animation.delegate = self;
animation.repeatCount = 1000000;

// Add the animation to the layer
[b addAnimation:animation forKey:@"move"];

希望有所帮助。

这篇关于在iPhone上使用无限循环制作弹跳球动画的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆