COSC 4355/6355: Ubiquitous Computing -‐ Fall 2014
Exercise 7 In the seventh exercise, you will create a game using Cocos2D. Teaching Moments You will learn how to create a sprite, perform animation and make a simple game with Cocos2D. You will also learn how to use NSTimer. Instructions Ø Download the base project and the assets from Blackboard. Ø Modify the code so that the bear can move only in one line at the bottom of the screen. The bear should not have any vertical motion. Ø Add a monkey sprite to the game. The monkey should move randomly on the top of the screen. It can only move left and write and will not have any vertical motion like the bear. It should not go out of the screen. Programming Tip 1 and Programming Tip 2 Ø The monkey should randomly drop bananas. Make sure that the monkey and banana are in proper proportion / scale with respect to the bear. Ø When the bear catches the banana, the particular banana sprite should be removed. If the bear does not catch the banana, the banana sprite should be removed even when it reached the bottom of the screen. Programming Tip 3. Ø A score should be displayed on the screen. Whenever the bear catches a banana, the score increments by 1 but whenever it misses, the score reduces by 1. Ø The game should last for 2 minutes. The timer should also be displayed on the screen. After 2 minutes the game should end and an alert view should be displayed indicating the player’s score. The game should be restarted if the user wants to. A screenshot of the game is shown in Figure 1. Use NSTimer with 1-‐second interval. Ø If the user does not want to play the game again, the application should close. FIGURE 1 Extra credit 1. The bear should move based on the accelerometer. For example, if the iPad is tilted to the left, the bear should move to the left. [+5] 2. When game ends, display your current score and your highest score so far [+1] 3. Add the ability to pause game [+1] Programming Tips of the Day Programming Tip 1 – Add sprite in Cocos2D To add a sprite, <CCSprite Object> = [CCSprite spriteWithFile:<filename>];
[self addChild: <CCSprite Object>];
Programming Tip 2 – Continuous animation of CCSprite -(void) startAnimation:(CCSprite *) sprite {
id actionMove = [CCMoveTo actionWithDuration:1.0
position:ccp(x,y)];
// set the new the x and y co-ordinates
id actionMoveDone = [CCCallFuncN actionWithTarget:self
selector:@selector(finishedMoving:)];
[<Sprite Object> runAction:[CCSequence actions:actionMove,
actionMoveDone, nil]];
}
-(void) finishedMoving:(id) sender {
[self startAnimation:<Sprite Object>];
}
Programming Tip 3 – Collision detection Use CGRectIntersectsRect method to detect collisions between sprites.
© Copyright 2026 Paperzz