简体   繁体   English

在cocos2d上制作CCSprite动画?

[英]Animating a CCSprite on cocos2d?

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) 大家好,我是cocos2d和iPhone开发的新手,我目前正尝试创建类似“ PapiJump”的游戏,目前遇到的问题是使游戏中的角色CCSprite动画化,我已经为自己的游戏制作了3张图片字符,字符指向右,左和中间(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). 在我的代码中,我使用TouchesEnded方法,在此方法中,我设置了一个名为“ touchState”的变量,因此,如果用户触摸了屏幕的右侧,则会将“ touchState”设置为1,否则将其设置为2(即左侧)。

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); 然后在“ init”方法中,我创建了一个“日程表”,该日程表每0.5秒运行另一个方法,该方法名为:“ updateGame”,“ updateGame”方法更新玩家的位置,其工作方式如下: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! 我已经阅读过Ray的有关CCSprite动画的教程 ,但是我不知道如何实现所需的结果。.预先感谢,并感谢我英语不好!

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) (或缓存它们并切换)

Can you clarify your question? 你能澄清你的问题吗? If you want to set a different sprite frame, you can use setDisplayFrame : 如果要设置其他精灵帧,可以使用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. 但是我觉得这个问题有点模糊。

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. 首先,您可以旋转对象直到其上下颠倒,然后使用FlipX(或FlipY)标志标记继续您以前的动画为true,或者可以添加仅动画设置角色旋转的动画,并在完成后更改FLipX(或FlipY)状态。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM