Cocos2D iOS tour: How to Write a hamster game (6): place a hamster
Place hamster
For this game, you will add three hamster in the scene-one in each hole. The hamster usually hides in a hole in the Grass-but occasionally pops up, so you can kill them.
First, let's add a hamster in each hole. we will temporarily make them appear on the grass so that we can put them in the correct position, once we set their position. we will put them in the hole.
Open HelloWorldLayer. h and add an array to keep the hamster in the level, as shown below:
// Inside @interface HelloWorldNSMutableArray *moles;
Storing the hamster in an array makes it easier for us to traverse them later.
Next, at the end of your init method (in "Add more here later ..." Place the following code:
// Load spritesCCSpriteBatchNode *spriteNode = [CCSpriteBatchNode batchNodeWithFile:sSheet];[self addChild:spriteNode z:999];[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:sPlist]; moles = [[NSMutableArray alloc] init];CCSprite *mole1 = [CCSprite spriteWithSpriteFrameName:@mole_1.png];mole1.position = [self convertPoint:ccp(85, 85)];[spriteNode addChild:mole1];[moles addObject:mole1];CCSprite *mole2 = [CCSprite spriteWithSpriteFrameName:@mole_1.png];mole2.position = [self convertPoint:ccp(240, 85)];[spriteNode addChild:mole2];[moles addObject:mole2];CCSprite *mole3 = [CCSprite spriteWithSpriteFrameName:@mole_1.png];mole3.position = [self convertPoint:ccp(395, 85)];[spriteNode addChild:mole3];[moles addObject:mole3];
The above code first creates a CCSpriteBatchNode for each Sprite, so we can plot three hamster more efficiently and then add this node as a child node to the layer. note that its z sequence is set to 999 temporarily. in this way, you can easily confirm the location of the hamster.
Then, the Code loads all the sprite frames from the attribute list to the memory for quick retrieval.
Next, create a hamster for each hole, place them in the scene, and add them to the moles list. note that the coordinate system of each hamster is in the playable area of (iPhone size ). for ipad. the positions of these points will be converted by the convertPoint method, as shown below:
- (CGPoint)convertPoint:(CGPoint)point { if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { return ccp(32 + point.x*2, 64 + point.y*2); } else { return point; } }
This method converts the point in the playable area to the appropriate position on the ipad screen. Remember:
We use high-definition images on the ipad, so all points are centered in the area of 960x640 on the screen of ipad1024x768, So 32 and 64 pixels are left at the top and bottom.
To put it bluntly, this method only converts a fixed point to an ipad using a simple algorithm.
One more thing-before we forget it, add the following code for memory cleanup to the dealloc method (you can ignore. Cat pig injection in the Code for enabling ARC .):
[moles release];moles = nil;
Compile and run your code. You should see that the three hamster appear happily in the correct position of the scenario! You should also try it on different devices to make sure their locations are correct.