Game Development Reference
In-Depth Information
[self setDisplayFrame:frame];
[self unscheduleUpdate];
[self scheduleUpdate];
}
You also need to replace the method declaration in the Bullet.h interface. No sur-
prises there:
-(void) shootBulletFrom:(CGPoint)startPosition
velocity:(CGPoint)vel
frameName:(NSString*)frameName;
Both velocity and position are now directly assigned to the bullet. This means
the code calling the shootBulletFrom method has to determine the position, direc-
tion, and speed of the bullet. This is exactly what you want: full flexibility for shooting
bullets, including changing the bullet's sprite frame by using the setDisplayFrame
method. Because the bullets are all in the same texture atlas and thus use the same tex-
ture, all it needs to do to change which bullet is displayed is set the desired sprite
frame. In effect, this simply renders a different part of the texture and comes at no extra
cost.
While in the Bullet class, I also fixed the boundary issues the bullets would have
had—that only bullets moving outside the right-hand side of the screen would have
been set invisible and put back on the waiting list. By using the CGRectInter-
sectsRect check with the bullet's boundingBox and the screenRect in the
update method, any bullet having moved completely outside the screen area will be
marked for reuse. Shown in Listing 8-5 are the improved init and update methods
of the Bullet class.
Listing 8-5. The Bullet Class Code Is Changed to Test if a Bullet Left the Screen
static CGRect screenRect;
-(id) initWithBulletImage
{
if ((self = [super initWithSpriteFrameName:@"bullet.png"]))
{
// make sure to initialize the screen rect only once
 
 
Search Nedrilad ::




Custom Search