Game Development Reference
In-Depth Information
Caution I know this singleton and the additional accessor method bulletS-
priteBatch may not be to everyone's liking. Why don't you simply pass the
CCSpriteBatchNode as a pointer to the Ship class, either in the initializer
or via a property?
One reason is that Ship doesn't own the bullet sprite batch, and therefore it
shouldn't keep a reference to it. Moreover, if the Ship class also holds a refer-
ence to the sprite batch, it could cause your whole scene to not be deallocated if
you're not careful. Nodes should never hold references to nodes that are not
children or grandchildren of itself.
A node that holds a reference to its parent or any of its grandparents would
cause the parent to not get deallocated. And the child nodes won't be dealloc-
ated unless its parent gets deallocated. That means a node holding a strong ref-
erence to one of its parents will create a so-called retain cycle. This vicious
circle causes a memory leak and potentially weird side effects because some
nodes may remain in memory. Therefore it's good practice to monitor the exe-
cution of a scene's dealloc method. Whenever it doesn't get called when chan-
ging scenes, you should investigate the cause.,
Now the Ship class can add the bullets to the sprite batch directly using the
sharedGameLayer and bulletSpriteBatch accessors, as shown in Listing
6-7 .
Listing 6-7. GameScene Gets a CCSpriteBatchNode for Bullets and Accessors for the
Ship Class
-(void) update:(ccTime)delta
{
Bullet* bullet = [Bullet bulletWithShip:self];
[[[GameLayer sharedGameLayer] bulletSpriteBatch] addChild:bullet
z:0
tag:GameSceneNodeTagBullet];
}
Optimizations
 
 
Search Nedrilad ::




Custom Search