Game Development Reference
In-Depth Information
This is in contrast to the spiders array you're using to hold references to the spider
sprites. The array is assigned to the instance variable spiders ; therefore, ARC won't
release the object until the GameLayer object itself is released. Under ARC, you don't
have to release the spiders array in any way or form.
At the end of Listing 4-8 , the method [self resetSpiders] is called; this meth-
od is shown in Listing 4-9 . The reason for separating the initialization of the sprites and
positioning them is that eventually there will be a game over, after which the game will
need to be reset. The most efficient way to do so is to simply move all game objects to
their initial positions. However, that may stop being feasible once your game scene
gets to a certain complexity. Eventually, it may be easier to simply reload the whole
scene, at the cost of having the player wait for the scene to reload.
Caution Speaking of reloading a scene, you may be tempted to write
[[CCDirector sharedDirector] replaceScene:self]; , to re-
load the same scene. This will cause a crash because self is the currently run-
ning scene, and trying to replace a running scene with itself is something
cocos2d doesn't like and causes the app to crash. Instead, you must create a
new instance of the GameLayer class: [[CCDirector sharedDirect-
or] replaceScene:[GameLayer scene]]; .
Listing 4-9 . Resetting Spider Sprite Positions
-(void) resetSpiders
{
CGSize screenSize = [CCDirector sharedDirector].winSize;
// Get any spider to get its image width
CCSprite* tempSpider = [spiders lastObject];
CGSize size = tempSpider.texture.contentSize;
int numSpiders = [spiders count];
for (int i = 0; i < numSpiders; i++)
{
// Put each spider at its designated position outside the screen
CCSprite* spider = [spiders objectAtIndex:i];
spider.position = CGPointMake(size.width * i + size.width * 0.5f,←
 
 
Search Nedrilad ::




Custom Search