Game Development Reference
In-Depth Information
if (CGRectIsEmpty(screenRect))
CGSize screenSize = [CCDirector sharedDirector].winSize;
screenRect = CGRectMake(0, 0, screenSize.width, screenSize.height);
return self;
. . .
-(void) update:(ccTime)delta
self.position = ccpAdd(self.position, ccpMult(velocity, delta));
// When the bullet leaves the screen, make it invisible
CGSize screenSize = [CCDirector sharedDirector].winSize;
CGRect screenRect = CGRectMake(0, 0, screenSize.width, screenSize.height);
if (CGRectIntersectsRect(self.boundingBox, screenRect) == NO)
self.visible = NO;
[self unscheduleUpdate];
The screenRect variable itself is now stored for convenience and performance reas-
ons as a static variable, so it can be accessed by other instances of the Bullet class
and doesn't need to be re-created for each use. Static variables like screenRect are
available in the class implementation file where they're declared. They're like global
variables to the class; any class instance can read and modify the variable, as opposed
to class member variables, which are local to every class instance. Because the screen
size never changes during game play, and all Bullet instances need to use this vari-
able, it makes sense to store it in a static variable for all class instances. The first bullet
to be initialized sets the screenRect variable. The CGRectIsEmpty method
checks whether the screenRect variable is still uninitialized; because the variable is
static, it needs to be initialized only once.
Search Nedrilad ::

Custom Search