user75444
user75444

Reputation: 11

How do you animate in Cocos2d?

I am trying to animate a sprite. The cocos2d website makes it look so easy but everytime I try it I get errors. I managed to download some code to get it working but one sprite animation takes 6 files. I have a character that needs to walk right and left, jump, climb, and fall. That means I am looking at 35 files. can't it be streamlined a bit? It just seems way harder than it should be.

Thanks, Josh

Upvotes: 1

Views: 5086

Answers (4)

NeverHopeless
NeverHopeless

Reputation: 11233

I know this thread is much older, but for future reference this is the article which seems best fit for this question.

It involves TexturePacker tool which generates a PLIST and a PNG that fulfill our needs.

Upvotes: 0

John Riselvato
John Riselvato

Reputation: 12924

    UIImageView* headBandAnimation = [[UIImageView alloc] initWithFrame:CGRectMake(25, 205, 100, 50)];
    headBandAnimation.animationImages = [NSArray arrayWithObjects:    
                                    [UIImage imageNamed:@"band1.png"],
                                    [UIImage imageNamed:@"band2.png"],
                                    [UIImage imageNamed:@"band3.png"],
                                    [UIImage imageNamed:@"band4.png"], nil];
    headBandAnimation.animationDuration = 0.5;
    headBandAnimation.animationRepeatCount = 0;
    [headBandAnimation startAnimating];
    [self.view addSubview:headBandAnimation];

is how to do animation without cocos2d if you wish to go this route.

Upvotes: 1

dbr
dbr

Reputation: 169673

Cocoa With Love have a short-series on writing a came using CoreAnimation, perhaps it might be of use?

Cocoa does take a lot of getting used to, and does seem really overly convoluted, but when you compared it to.. well, most other GUI toolkits I've used, it suddenly seems very elegant and simple. The problem with the example code, and most tutorials (including the one I linked to, albeit to a slightly lesser degree) is they only show you the finished application - it doesn't show the increments. There's no "I have a empty canvas", then "I worked out how to draw a circle", then "I've animated the circle".

Try making a new application, look through the example code/IB project/tutorials/documentation for the bit that initialises the canvas-thing. Then look for the code that adds a simple shape. Then look for the code to animate the code (Genericrich answer, for example)

Upvotes: 0

Genericrich
Genericrich

Reputation: 4651

Cocos is great. You just need to spend time with the demo project, hang out on the message board, and keep at it.

You animate a sprite like this:

id action = [Sequence actions:
             [ScaleTo actionWithDuration:.3 scale:0.7f],
             [ScaleTo actionWithDuration:.3 scale:1.0f],
             nil];

[[self getByTag:FOO] do:action];

This causes the sprite with the tag FOO to scale down to 70 percent in .3 seconds, then back up to 100 percent in .3 seconds.

Much more complex animations are possible, just get the basics down and the world will be ya oyster, at least as far as making stuff fly around on the screen, that is.

Upvotes: 3

Related Questions