
cocos2D iOS tutorial 5
November 4, 2011Tutorial 5A: State Machine
Let’s create a StateMachine class that controls the behaviour (BaseState) of GameObject. Each GameObject should have at least one state machine. You can use 3 buttons to change the behaviour of a game object. For now, just create one object to test your StateMachine class.
Note that there are multiple files here.
Sample code 1
@property(nonatomic, readonly) StateMachine *mySM;
…
state = [[RoundAboutState alloc] initWithName:@”roundabout” stateMachine:sm];
cocos2D iOS tutorial 4
November 4, 2011Tutorial 4A: Shared Data Singleton
Create a class call GameData (inherit from NSObject) that is to be shared across the game, between multiple scenes. GameData should contain member variables such as player’s life, score, level, experience points, gold, state and so on. These data are persistent throughout the game and not just in a scene (those local data only used in a scene should be under the scene’s member variable). You may want to store the pointer to your Object Manager in GameData, so that it can be used in multiple scenes.
Sample code 1
GameData.h
@interface GameData : NSObject {
int hp;
int score;
int level;
int xp;
int gold;
int state;
};
@property(nonatomic) int hp;
…
+ (GameData *)sharedData;
@end
GameData.m
static GameData *data = nil;
@implementation GameData
+ (GameData *)sharedData {
if(data == nil) {
data = [[GameData alloc] init];
gold = 100;
hp = 20;
score = 0;
…
}
return data;
}
…
@end
cocos2D iOS tutorial 3
November 3, 2011Tutorial 3A: Game Object
GameObject are the screen objects found in our game. They can be player, enemies, gold coins, obstacles, exits and so on.
At this stage, your game object will probably be a CCSprite with no other information. Nevertheless, you should put the CCSprite class into a GameObject class and add some other variables like float speed, int type, bool active, etc.
Tutorial 3B: Game Object Manager
Multiple instances (e.g. 30x) of GameObject class (inherit from CCNode) should be pre-allocated and store in a Game Object Manager (ObjectManager) class, using an NSMutableArray. Every time you need a GameObject, simply request one from ObjectManager, i.e. [om getObjectOfType:type]. This is to move the object allocation task from runtime to load time.
In the scene, let’s assume you have 3 buttons on the screen. Touching each button will get the object of a corresponding type from the object manager, add it to the scene and move it. The object will drop from the top to the bottom of the screen at a random speed. When it falls out of the screen, set the object.active to NO and remove from the scene such that it is released back to the ObjectManager.
cocos2D iOS tutorial 2
November 3, 2011[iOS device required]
Tutorial 2A(i): Accelerometer
Make use of the iOS devices’ accelerometer. Using a CCSprite and the update:dt method, update the sprite’s position with the accelerometer readings, i.e. when you tilt left/right, the sprite moves left/right; when you tilt up/down, the sprite moves up/down. Both axes should be updated simultaneously. You should turn off the auto screen rotation when you use the accelerometer. Allow your sprite to pass through the side of the screen and appear at the opposite side when it touches the edge.
Tutorial 2A(ii): Direction of sprite
Now that you have a moving sprite, now use a top-view (aero)plane image as your sprite. When you tilt, the plane should also turn towards to the direction of travel.
Tutorial 2A(iii): Velocity and acceleration (Pre-requisite: Vector math)
Note that you might want to store the previous velocity of your plane. To add realism, the plane should not react sharply to the tilt, it should gradually change its velocity. Your plane also should have acceleration. When you tilt the device on one side long enough, it should gradually pick up speed. But remember to cap the speed, otherwise it may go to infinity.
cocos2D iOS tutorial 1
November 3, 2011Tutorial 1A: Path-finding (how to use update:)
The landscape screen is 480×320 pixels. Sub-divide the screen into grids of 40×40 pixels, and there should be 12 by 8 grids. Find a suitable image for the background.
Using update or nextFrame method, move a sprite from grid ( 1, 1 ) at bottom-left to grid ( 12, 8 ) at top-right, at a constant speed. See below for the path.
O O O O O O G G G O G G
O O G G G O G O G O G O
O O G O G O G O G O G O
O O G O G G G O G O G G
O O G O O O O O G O O G
O O G G G G G O G O O G
O O O O O O G O G G G G
G G G G G G G O O O O O
Tutorial 1B: Touch Input (how to capture touch)
Subdivide the screen into 3×3 grid for a tic-tac-toe game. Use a suitable image for the background. The scene should register 9 touch area. Whenever any of them is touched, ‘O’ or ‘X’ should be shown (alternately, if touched again).
Mahjong HD for iPad is here!
September 10, 2010Mahjong HD for iPad is here.
The game is based on SG Mahjong, with HD graphics and a new mode – 4 players can play on the iPad simultaneously.
Download on iPad AppStore now
Posted by clouette 











