I have subclassed Sprite and created a class called CharacterSprite. Within that class I would like to define various animations for the character (idle, running, death, etc.) I have managed to define a sequence of images in the init of my CharacterSprite and loop it forever with an action to show the idle animation. My question is how do I tell it to switch animations from my gamelayer? Is that the proper way to do this? I ask because I thought it would be better to define all of that here than in the init of my gamelayer. I am currently not doing this with one big texture but am instead using multiple images. The reason is I wanted to see it working this way without the complications of doing it with one file.
Multiple animations for a character sprite
(9 posts) (4 voices)-
Posted 2 years ago #
-
In my gamelayer I have this in my tick method:
-(void)step: (ccTime) dt {
if (heroMoving == YES) {
[hero showCrash];
}
}In my CharacterSprite i have the following methods
-(id) init
{
self = [super init];if (self)
{//Load the idleAnimation
idleAnimation = [Animation animationWithName:@"idle" delay:0.4];
for( int i=1;i<4;i++)
[idleAnimation addFrameWithFilename: [NSString stringWithFormat:@"char_land%04d.png", i]];//Load the crashAnimation
crashAnimation = [Animation animationWithName:@"crash" delay:0.1];
for( int i=1;i<10;i++)
[crashAnimation addFrameWithFilename: [NSString stringWithFormat:@"char_crash%04d.png", i]];[self showIdle];
}return self;
}-(void) showIdle
{
// Make the animation sequence repeat forever
id idleAction = [Animate actionWithAnimation: idleAnimation];
id idleRepeating = [RepeatForever actionWithAction:idleAction];
// Run the idle animation
[self runAction:idleRepeating];
}-(void) showCrash
{
id crashAction = [Animate actionWithAnimation: crashAnimation];
[self runAction:crashAction];
}When I run my app it all starts fine and when I initiate the touch to switch from idle to crash, it locks up. The only warning I have is under [hero showCrash] in the gamelayer which is "Warning: 'Sprite' may not respond to '-showCrash'"
Can anyone shed some light on how I tell my character to switch from his idle animation to another?
Posted 2 years ago # -
Not sure, but I think you probably need to 'retain' you animations.
Posted 2 years ago # -
And I just realized that I am creating the action in the wrong place. I have some cleanup to do.
Posted 2 years ago # -
you may find some tips in this thread:
Posted 2 years ago # -
Excellent reply. That was exactly my issue. Thank-you.
Posted 2 years ago # -
Actually a new problem has cropped up. The last cell visible in the idle animation shows after the crash shows it's animation. I thought stopping the first action would remove it. If not, how do I tell it to remove the first animation when I run the action for the second animation?
Posted 2 years ago # -
I believe Animate: has a 'restoreOriginalFrame' option that you can
set to false.Chris.
On Sun, Jun 21, 2009 at 10:41 PM,
<cocos2d-iphone-discuss@gamesforfood.cl> wrote:
> sonnyburnette says:
>
> Actually a new problem has cropped up. The last cell visible in the idle
> animation shows after the crash shows it's animation. I thought stopping the
> first action would remove it. If not, how do I tell it to remove the first
> animation when I run the action for the second animation?
>
> Read the whole thread.Posted 2 years ago # -
That was it. I also noticed that I needed an additional image in my explosion/crash that was blank so it didn't end with the last explosion cell frozen there.
Posted 2 years ago #
Reply
You must log in to post.