How do you animate in Cocos2d? - cocos2d-iphone

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

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.

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.

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)

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.

Related

Using setAliasTexParameters and cc.ResolutionPolicy to render a pixel perfect sprite in cocos2d-js

I'm making my first game with cocos2d-js. I've made a nice title png for my game but when I load it in cocos2-js it has anti-aliasing that ruins the effect. I read on the internet that using setAliasTexParameters can turn off the anti aliasing. I can't get it to work though, I've tried many different ways. I'm sure it's a basic problem with my syntax but I can't work it out and it's driving me insane. Chrome gives me the error Uncaught TypeError: Cannot read property 'setAliasTexParameters' of null.
Here's the layer:
var game = cc.Layer.extend({
init:function() {
this._super();
backgroundLayer = cc.Layer.create();
this.addChild(backgroundLayer);
var title = new cc.Sprite.create("assets/ui/title.png");
backgroundLayer.addChild(title,0);
title.texture.setAliasTexParameters();
title.setPosition(160,240);
}
});
EDIT: I have realised the sprites are being made about 30% too big whether I use setAliasTexParameters or not, so pixel perfect anti aliasing ins't possible. I can't work out why this is happening.
EDIT2: If I remove the cc.ResolutionPolicy in my main js file the sprite is rendered perfectly without antialiasing, but my canvas is half the size it should be! I think this is a resolutionPolicy problem rather than a setAliasTexParameters problem.
I haven't been able to find a resolutionpolicy from the documentation that won't resize my sprite.

Best way to have animated background in cocos2d

I have seen some similar questions asked but no definitive answer.
I have a background image that I'm using for my main menu for a cocos2d game. I plan to have it animated but not sure what is the most efficient way to do this. One idea was to have multiple images to create the animation but I was thinking this may take up too much memory as each image would be quite big.
The other idea was having one background image as a sprite and then having child sprites of that image that are animated with ccaction. The only thing is I may not be able to create such an elaborate animation if I do this.
I just wanted to get some feedback on this to see what would be the best approach.
Thank you,
Making a frame-by-frame animation of the whole screen would make your app size litteraly explode.
You should definitely go with your 2nd idea, i.e have different sprites for each animated component and use actions to animate them.
Check out CocosBuilder: it provides a nice UI for designing such complex animations

realistic jump like all games in cocos2d iphone

i am new on game development so can any one guide me or help to make the jump in the game more realistic like monster inc run,mega run have in which user can jump or also make the long jump if it pressed long on screen. i tried the box2d code for making simple jump i am using this code
b2Vec2 impulse = b2Vec2([self body]->GetMass()*vert, [self body]->GetMass()*horz);
b2Vec2 impulsePoint = [self body]->GetWorldCenter();//GetWorldPoint(b2Vec2(5.0/100.0, -15.0/100.0));
[self body]->ApplyLinearImpulse(impulse, impulsePoint);
but it's not giving me the correct result i want the exact logic or technique which all the games have. Can any one suggest me the book,tutorial,or sample code so i can make my game more attractive with game play and also tell me how i can make the longer jump thing. i searched a lot but i didn't find any good tutorial or thing on the web so i decided to asked it here and sorry for my english i am not good with it.
I do not know Cocos2D, but I can suggest you how I've seen it done:
When the user starts pressing the button, you start applying a vertical force to the character, this force continues applying for a certain ammount of time (having the character achieve more height) or until the user stops pressing the button.
That way if its just a tap then the force applied is little, but if the button is pressed for longer the character jumps higher

Understanding cocos2d scenes, how they works really?

I'm stuck on something about the scenes creation and replacement in cocos2d, so I'm going to ask precisely what seems to be misunderstood by me. I have a game (fully working except for scene swapping, sadly) with some little-games, now, if I had to do this starting with a cocos2d scene as menu I wouldn't have any problem, but since I did it starting with UIKit I truly need to know better how the scenes are working to fix it.
Firstly, is it required to start a scene in the appDelegate? since I'm starting with UIkit and the scene must be shown after you choice the game (say, out of 3 choices), which scene should I put in the appDelegate? and where exactly? I'm putting the scene in this method:
-(void) directorDidReshapeProjection:(CCDirector*)director
{
if(director.runningScene == nil)
//start scene
}
If I put the FIRST scene, the UIKit part works good and when I start the "game number TWO" as first choice (say we play this game for first) I got the Open GL 0x0506 error, then the scene start.
If I put the first scene, I choice the first game, and then quit and choice the second game, the scene is replaced properly without that error.
If I put the first scene, and I start the "game number 1" it works (obviously) because he has the scene loaded, but I cannot know which game will start as first the user.
I tried with an "intro scene" loaded at the appDelegate but I got the same problem. the problem basically is "how to start scene if you have more than one scene and don't know which will be called as first"...
The 'getting started with iOS' documentation will really clear up a lot of these questions. You can find it at developer.apple.com - https://developer.apple.com/library/ios/#documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/Introduction/Introduction.html#//apple_ref/doc/uid/TP40007072
It explains just what an AppDelegate actually is, as well as how to use one properly. It is not immediately clear how to mix UIKit and cocos2d, but the above link cleared a lot up for me. Another very helpful resource is a tutorial by Ray Wenderlich -
http://www.raywenderlich.com/4817/how-to-integrate-cocos2d-and-uikit
From a bird's eye view, the CCDirector inherits from a UIWindow. Mixing UIKit and cocos2d is as simple as building your interface with UIKit, then at some point opening a UIWindow and allowing the CCDirector to start cocos2d. In a sense, the components act as almost two entirely separate entities.

What is the best way to set up menus with cocos2d?

I'm new to development and I have played around with a few tutorials. I wonder what the best way to set up a menu for a game with cocos2d?
I want a MainMenu with a Startbutton, SettingsButton, HighScoresButton and a little info/creditsButton in the corner.
How should I set this up?
Should I have the MainMenu as a Scene and the others as layers or just make all of them as separate Scenes?
The buttons that I add, should they be a plain button and then I add the textLabel on top or should I make them complete with textLabel?
I would like the buttons to "wiggle" like it is made of jello when i tap them, how do I do that?
As I said, I am new to this but I wanna learn as much as I can before school starts. I'm currently taking a summer class in iphone development so I get a head start for the next semester. I would like to see experienced game developers to help me out with this since i want to work with that when I get older, if they could also show me how to structure a game and the design.
Thank you so much.
David H
You've got several questions buried in there, so I'll address them in order...
First, to save on memory, you should break sections of your game, including menus, into separate scenes and switch between them using:
[[CCDirector sharedDirector] replaceScene: yourScene];
Alternatively you can use pushScene: and popScene, but these hold pushed scenes in memory and can be very costly depending on what you've got in the scene.
As for how to design your buttons, that's completely up to you and what fits best with the game. Some games look fine with text buttons. Others need a more stylized button that is best created with images. Remember, a CCMenuItem (button) is just a CCNode, so you can layer images and text in almost any way you wish.
Animating buttons is going to be a manual thing. I suggest subclassing the CCMenuItem, or CCMenuItemImage and overriding the selected and unselected methods to animate the underlying images.
We used cocos2d for our game The Selfish Birdbreeder. You can find the game and source here and dig around. I'm pretty certain we have a main menu.
http://pyweek.org/e/BirdBreeder/