Game Development Reference
In-Depth Information
screenSize.height + size.height);
[spider stopAllActions];
// Schedule the spider update logic to run at the given interval.
[self schedule:@selector(spidersUpdate:) interval:0.7f];
// reset the moved spiders counter and spider move duration (affects speed)
numSpidersMoved = 0;
spiderMoveDuration = 4.0f;
Once again you obtain a reference to one of the existing spiders temporarily to get its
image size via the texture's contentSize property. You don't create a new sprite
here because there are already existing sprites of the same kind, and because all spiders
use the same image with the same size, you shouldn't even care which sprite you're get-
ting. So, simply get the last spider from the array.
You then modify each spider's position so that together they span the entire width of
the screen. You add half the image size's width—once again, this is because of the
sprite's texture being centered on the node's position. As for the height, also set each
sprite to be one image size above the upper screen border. This is an arbitrary distance,
as long as the image isn't visible, which is all you want to achieve. Because the spider
might still be moving when the reset occurs, you also stop all of its actions at this point.
Tip To save a few CPU cycles, it's good practice not to use method calls in the
conditional block of for or other loops if it's not strictly necessary. In this
case, I created a variable numSpiders to hold the result of [spiders
count] and I use that in the conditional check of the for loop. The count of
the array remains the same during the for loop's iterations because the array
itself isn't modified in the loop. That's why I can cache this value and save the
repeated calls to [spiders count] during each iteration of the for loop.
I'm also scheduling the spidersUpdate : selector to run every 0.7 seconds, which
is how often another spider will drop down from the top of the screen. If the selector is
already scheduled, then cocos2d will acknowledge that with a log message you can ig-
Search Nedrilad ::

Custom Search