Game Development Reference
In-Depth Information
Listing 6-13. Powerup.m (powerup: (partial))
+(id)powerup:(GameController*)aController{
CGSize gameSize = [aController gameAreaSize];
CGPoint gameCenter = CGPointMake(gameSize.width/2.0, gameSize.height/2.0);
float distanceFromCenter = sqrtf(gameCenter.x*gameCenter.x + gameCenter.y*gameCenter.y);
CGPoint center = [Actor randomPointAround:gameCenter At:distanceFromCenter];
ImageRepresentation* rep = [ImageRepresentation imageRep];
[rep setBackwards:arc4random()%2 == 0];
[rep setStepsPerFrame:1 + arc4random()%3];
Powerup* powerup = [[Powerup alloc] initAt:center WithRadius:32 AndRepresentation:rep];
[rep setDelegate:powerup];
float rotation = arc4random()%100/100.0 * M_PI*2;
[powerup setRotation:rotation];
[powerup setVariant:arc4random()%PWR_VARIATION_COUNT];
//Section Omitted , see Listing 6-21
return powerup;
The first thing we have to do is figure out values that will be passed to Actor 's initAt:WithRadius:
AndRepresentation task. After we have those values figured out, we create a new power-up and then
set a few starting values, such as the rotation and variation of the power-up. In an omitted section of
Listing 6-13, we set up the behavior of the power-up. This code can be found in Listing 6-21 and is
discussed later in this section.
Let's take a quick look at how we calculate the center of the power-up before diving into the details
of ImageRepresentation . The CGPoint center is created by picking a random point on a circle just
beyond the bounds of the screen, as shown in Figure 6-3 .
 
Search WWH ::




Custom Search