在 cocos2d 上为 CCSprite 制作动画?

发布于 2024-10-30 17:13:00 字数 640 浏览 0 评论 0原文

大家好,我是 cocos2d 和 iPhone 开发的新手,我目前正在尝试创建一个像“PapiJump”这样的游戏, 我目前遇到的问题是在我的游戏中为角色 CCSprite 制作动画, 我创建了我的角色的 3 个图像,角色指向右、左和中(http://i53.tinypic.com/ngzoyh.png),

因此当角色改变方向(左或右)时,它不会直接“跳跃”到另一个方向,但会动画。

在我的代码中,我使用 TouchesEnded 方法,在此方法中,我设置一个名为“touchState”的变量, 因此,如果用户触摸屏幕的右侧,它会将“touchState”设置为 1,否则将其设置为 2(表示左侧)。

然后在“init”方法中,我创建了一个“时间表”,每 0.5 秒运行另一个方法,名为:“updateGame”, “updateGame”方法更新玩家的位置,其工作原理如下: 玩家位置 = ccp(玩家位置.x, 玩家位置.y - 玩家重力.y);

我已阅读 Ray 的教程关于动画CCSprites,但我不知道如何实现我需要的结果.. 预先感谢并抱歉我的英语不好!

Hey guys, I'm new to cocos2d and iPhone development, im currently trying to create a game like "PapiJump",
What I'm currently having problems with, is animating the character CCSprite in my game,
I have created 3 images of my character, character pointing right, left and middle (http://i53.tinypic.com/ngzoyh.png)

so when the character changes its direction (left or right), it wont "jump" directly to the other direction but will animate..

In my code im using the TouchesEnded method, in this method im setting a variable named "touchState",
so if the user touched the right side of the screen it will set "touchState" to 1, otherwise it sets it to 2 (means left).

then in the "init" method i created a "schedule" that runs another method every 0.5 sec that is named: "updateGame",
the "updateGame" method updates the player's position, it works like that:
player.position = ccp(player.position.x , player.position.y - playerGravity.y);

I've read Ray's tutorial on animating CCSprites but i have no idea how to achieve the result that i need..
Thanks in advance and sorry for my bad english!

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

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

发布评论

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

评论(3

只是我以为 2024-11-06 17:13:00
NSMutableArray *walkAnimFrames = [NSMutableArray array];
for(int i = 1; i <= 4; ++i)
{
    [walkAnimFrames addObject: [[CCSpriteFrameCache sharedSpriteFrameCache]     spriteFrameByName: [NSString stringWithFormat:@"spr_fillin_0%d.png", i]]];
}
CCAnimation *walkAnim = [CCAnimation animationWithFrames:walkAnimFrames delay:0.0333f];

_sprite = [CCSprite spriteWithSpriteFrameName:@"spr_fillin_01.png"];
[_sprite runAction:[CCRepeatForever actionWithAction: [CCAnimate actionWithAnimation:walkAnim restoreOriginalFrame:NO]]];

[<CCLAYER OR BATCH NODE> addChild:_sprite];

编辑:看起来你正在谈论只是翻转图像。 重新获取精灵

_sprite = [CCSprite spriteWithSpriteFrameName:@"<New Sprite File>"];

您可以使用(或缓存它们并切换)

NSMutableArray *walkAnimFrames = [NSMutableArray array];
for(int i = 1; i <= 4; ++i)
{
    [walkAnimFrames addObject: [[CCSpriteFrameCache sharedSpriteFrameCache]     spriteFrameByName: [NSString stringWithFormat:@"spr_fillin_0%d.png", i]]];
}
CCAnimation *walkAnim = [CCAnimation animationWithFrames:walkAnimFrames delay:0.0333f];

_sprite = [CCSprite spriteWithSpriteFrameName:@"spr_fillin_01.png"];
[_sprite runAction:[CCRepeatForever actionWithAction: [CCAnimate actionWithAnimation:walkAnim restoreOriginalFrame:NO]]];

[<CCLAYER OR BATCH NODE> addChild:_sprite];

Edit: Looks like you're talking about just flipping the image around. You can just refetch a sprite with

_sprite = [CCSprite spriteWithSpriteFrameName:@"<New Sprite File>"];

(Or cache them and switch)

醉生梦死 2024-11-06 17:13:00

你能澄清你的问题吗?如果你想设置不同的精灵框架,你可以使用 setDisplayFrame:

CCSpriteFrame *spriteFrame = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:frameName];
[sprite setDisplayFrame:frame];

或者如果你有动画:

[sprite runAction:[CCAnimate actionWithAnimation:runAnimation restoreOriginalFrame:NO]];

但我觉得这个问题有点模糊。

Can you clarify your question? If you want to set a different sprite frame, you can use setDisplayFrame:

CCSpriteFrame *spriteFrame = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:frameName];
[sprite setDisplayFrame:frame];

or if you have an animation:

[sprite runAction:[CCAnimate actionWithAnimation:runAnimation restoreOriginalFrame:NO]];

But I feel like the question is a little vague.

唱一曲作罢 2024-11-06 17:13:00

如果我正确地回答了您的问题,我可以建议两种方法来解决您的问题:
首先你可以旋转你的对象直到它颠倒,然后使用 FlipX(或 FlipY)标志标记为 true 继续之前的动画,或者你可以添加一个动画,仅对角色旋转进行动画处理,当它完成时更改 FLipX(或FlipY)状态。

if i got your question properly i can suggest two ways to solve your problem:
first you could rotate your object to till it is upside down, and the continue your previous animations using FlipX(or FlipY) flag marks as true or you can add an animation wich animates only characters rotation and when ever it's finnished change the FLipX(or FlipY) status.

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