Game Development Reference
In-Depth Information
#import < Foundation/Foundation.h>
#import "cocos2d.h"
@interface BulletCache : CCNode
{
CCSpriteBatchNode* batch;
NSUInteger nextInactiveBullet;
}
-(void) shootBulletFrom:(CGPoint)startPosition
velocity:(CGPoint)velocity
frameName:(NSString*)frameName;
@end
To refactor the bullet-shooting code out of the GameLayer class, you need to move
both the initialization and the method to shoot bullets to the new BulletCache class.
Therefore remove the declaration and implementation of the shootBul-
letFromShip:(Ship*)ship method from the GameLayer class. Also remove
the nextInactiveBullet instance variable from the interface and remove the bul-
let initialization code from the init method, as seen in Listing 8-2 . All this code will
be moved over to the new BulletCache class, the implementation of which is in
Listing 8-4 .
Listing 8-2. Remove This Code from the GameLayer class's init Method
// Now uses the image from the Texture Atlas.
CCSpriteFrame* bulletFrame = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"bullet.png"];
CCSpriteBatchNode* batch = [CCSpriteBatchNode batchNodeWithTexture:bulletFrame.texture];
[self addChild:batch z:1 tag:GameSceneNodeTagBulletSpriteBatch];
// Create a number of bullets up front and re-use them whenever necessary.
for (int i = 0; i < 400; i++)
{
Bullet* bullet = [Bullet bullet];
bullet.visible = NO;
[batch addChild:bullet];
}
 
 
 
Search Nedrilad ::




Custom Search