Feel free to fix/add documentation to the wiki
Hello Actions
Valid for cocos2d v0.99.0 or newer
Important: This example ONLY shows the basic steps to execute some actions. Some advanced options regarding the initialization were removed to simplify the sample. Once you understand this example, you can read the hello_events sample.
Implementation File
File: HelloActions.m
In objective-c the implementation is defined in the .m file, while the interfaces are defined in the .h file.
Import Headers
Like in c / c++ / java / etc, you need to import the needed headers.
// Needed for UIWindow, NSAutoReleasePool, and other objects #import <UIKit/UIKit.h> // Import the interfaces #import "HelloActions.h"
Layer
The magic occurs in the class. We create a CCLayer that will have 2 objects: a CCLabel and a CCSprite.
- The CCLabel will run 1 action: scale.
- The CCSprite will run multiple actions: jump, rotate, spawn, sequence and repeat.
// HelloActions implementation @implementation HelloActions // on "init" you need to initialize your instance -(id) init { // always call "super" init // Apple recommends to re-assign "self" with the "super" return value if( (self=[super init] )) { // // Label // // create and initialize a Label CCLabel* label = [CCLabel labelWithString:@"Hello Actions" fontName:@"Marker Felt" fontSize:64]; // ask director the the window size CGSize size = [[CCDirector sharedDirector] winSize]; // position the label on the center of the screen // "ccp" is a helper macro that creates a point. It means: "CoCos Point" label.position = ccp( size.width /2 , size.height/2 ); // add the label as a child to this Layer [self addChild: label]; // objective-c can be an static or dynamic language. // "id" is a reserved word that means "this is an object, but I don't care about its type" // scales the label 2.5x in 3 seconds. id action = [CCScaleBy actionWithDuration:3.0f scale:2.5f]; // tell the "label" to run the action // The action will be execute once this Layer appears on the screen (not before). [label runAction:action]; // // Sprite // CCSprite *sprite = [CCSprite spriteWithFile:@"grossini.png"]; sprite.position = ccp( 0, 50); // z is the z-order. Greater values means on top of lower values. // default z value is 0. // So the sprite will be on top of the label. [self addChild:sprite z:1]; // create a RotateBy action. // "By" means relative. "To" means absolute. id rotateAction = [CCRotateBy actionWithDuration:4 angle:180*4]; // create a JumpBy action. id jumpAction = [CCJumpBy actionWithDuration:4 position:ccp(size.width,0) height:100 jumps:4]; // spawn is an action that executes 2 or more actions at the same time id fordward = [CCSpawn actions:rotateAction, jumpAction, nil]; // almost all actions supports the "reverse" method. // It will create a new actions that is the reversed action. id backwards = [fordward reverse]; // Sequence is an action that executes one action after another one id sequence = [CCSequence actions: fordward, backwards, nil]; // Finally, you can repeat an action as many times as you want. // You can repeat an action forever using the "RepeatForEver" action. id repeat = [CCRepeat actionWithAction:sequence times:2]; // Tell the sprite to execute the actions. // The action will be execute once this Layer appears on the screen (not before). [sprite runAction:repeat]; } return self; } // on "dealloc" you need to release all your retained objects - (void) dealloc { // in case you have something to dealloc, do it in this method // in this particular example nothing needs to be released. // cocos2d will automatically release all the children (Label) // don't forget to call "super dealloc" [super dealloc]; } @end
Application Delegate
This class handles the initialization. Probably all your games will have a similar Application Delegate. For the moment it's not that important if you don't understand the following code.
@implementation AppController // window is a property. @synthesize will create the accesors methods @synthesize window; // Application entry point - (void) applicationDidFinishLaunching:(UIApplication*)application { // Try to use CADisplayLink director // if it fails (SDK < 3.1) use Threaded director if( ! [CCDirector setDirectorType:CCDirectorTypeDisplayLink] ) [CCDirector setDirectorType:CCDirectorTypeDefault]; // create an initilize the main UIWindow window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // Attach cocos2d to the window [[CCDirector sharedDirector] attachInWindow:window]; // before creating any layer, set the landscape mode [[CCDirector sharedDirector] setDeviceOrientation:CCDeviceOrientationLandscapeLeft]; // Make the window visible [window makeKeyAndVisible]; // Create and initialize parent and empty Scene CCScene *scene = [CCScene node]; // Create and initialize our HelloActions Layer CCLayer *layer = [HelloActions node]; // add our HelloWorld Layer as a child of the main scene [scene addChild:layer]; // Run! [[CCDirector sharedDirector] runWithScene: scene]; } - (void) dealloc { [window release]; [super dealloc]; } @end
Main entry point
Main entry point. Like any c or c++ program, the main function is the entry point.
int main(int argc, char *argv[]) { // it is safe to leave these lines as they are. NSAutoreleasePool *pool = [NSAutoreleasePool new]; UIApplicationMain(argc, argv, nil, @"AppController"); [pool release]; return 0; }
Header file
File: HelloActions.h
// When you import this file, you import all the cocos2d classes #import "cocos2d.h" // Application Delegate class @interface AppController : NSObject <UIAccelerometerDelegate, UIAlertViewDelegate, UITextFieldDelegate, UIApplicationDelegate> { // main UIWindow // The OpenGL view will be a attached to this UIWindow UIWindow *window; } // Make the main UIWindow a property @property (nonatomic, retain) UIWindow *window; @end // HelloActions Layer @interface HelloActions : CCLayer { } @end
Trace: » hello_actions » hello_world » cookbook » labels » actions » hello_events » effects » actions_ease » basic_concepts » hello_actions
