在 iPhone 上制作无限循环的弹跳球动画的最佳方法是什么?

发布于 2024-07-25 13:29:33 字数 670 浏览 3 评论 0原文

我正在开发一款 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 技术交流群。

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

发布评论

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

评论(3

吝吻 2024-08-01 13:29:34

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

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

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

希望有点帮助。

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

Hope that helps a bit.

断爱 2024-08-01 13:29:34

从你的问题来看,你似乎使用了多个计时器。 只需使用一个即可为所有对象添加动画效果。 在某个地方,当您的应用程序启动时,请执行以下操作:

[NSTimer scheduledTimerWithTimeInterval:0.03 target:self selector:@selector(mainLoop:) userInfo:nil repeats:YES];

然后,在主循环中,执行如下操作:

- (void)mainLoop:(NSTimer *)timer
{
    // compute new position for imgBirds
    for (int i=0; i<kBirdCount; i++)
    {
        imgBirds[i].center = CGPointMake(somex, somey);
    }

    // compute new position for ship
    ship.center = CGPointMake(somex, somey);
}

另外,您应该将 somexsomey 计算为计时器间隔的函数。 这样,如果你改变它,你的动画看起来仍然是一样的。

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:

[NSTimer scheduledTimerWithTimeInterval:0.03 target:self selector:@selector(mainLoop:) userInfo:nil repeats:YES];

Then, in main loop, something like this:

- (void)mainLoop:(NSTimer *)timer
{
    // compute new position for imgBirds
    for (int i=0; i<kBirdCount; i++)
    {
        imgBirds[i].center = CGPointMake(somex, somey);
    }

    // compute new position for ship
    ship.center = CGPointMake(somex, somey);
}

Also, you should compute somex and somey as a function of your timer interval. In that way, if you change it, your animation will still look the same.

神回复 2024-08-01 13:29:34

您需要使用核心动画而不是手动移动小鸟。 查看文档中的 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.

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