Game Development Reference
In-Depth Information
Listing 4-8 . For Easier Access, Spider Sprites Are Initialized and Added to a CCArray
-(void) initSpiders
{
CGSize screenSize = [CCDirector sharedDirector].winSize;
// using a temporary spider sprite is the easiest way to get the image's size
CCSprite* tempSpider = [CCSprite spriteWithFile:@"spider.png"];
float imageWidth = tempSpider.texture.contentSize.width;
// Use as many spiders as can fit next to each other over the whole screen width.
int numSpiders = screenSize.width / imageWidth;
// Initialize the spiders array using alloc.
spiders = [NSMutableArray arrayWithCapacity:numSpiders];
for (int i = 0; i < numSpiders; i++)
{
CCSprite* spider = [CCSprite spriteWithFile:@"spider.png"];
[self addChild:spider z:0 tag:2];
// Also add the spider to the spiders array.
[spiders addObject:spider];
}
// call the method to reposition all spiders
[self resetSpiders];
}
There are a few things to note. You create a tempSpider CCSprite only to find
out the sprite's image width, which you then use to decide how many spider sprites can
fit next to each other. The easiest way to get an image's dimensions is by simply creat-
ing a temporary CCSprite . Note that you didn't add the tempSpider as child to
any other node, nor assign it to an instance variable. This means that once execution
leaves the initSpiders method, ARC will recognize that the tempSpider object
is no longer in use and will automatically release its memory.
 
Search Nedrilad ::




Custom Search