Game Development Reference
In-Depth Information
Listing 7-24. Comet.m (comet:)
+(id)comet:(GameController*)controller{
CGSize gameSize = [controller gameAreaSize];
CGPoint gameCenter = CGPointMake(gameSize.width/2.0, gameSize.height/2.0);
float directionOffScreen = arc4random()%100/100.0 * M_PI*2;
float distanceFromCenter = MAX(gameCenter.x,gameCenter.y) * 1.2;
CGPoint center = CGPointMake(gameCenter.x + cosf(directionOffScreen)*distanceFromCenter,
gameCenter.y + sinf(directionOffScreen)*distanceFromCenter);
ImageRepresentation* rep = [ImageRepresentation imageRep];
Comet* comet = [[Comet alloc] initAt:center WithRadius:16 AndRepresentation:rep];
[rep setDelegate:comet];
[comet setVariant:arc4random()%VARIATION_COUNT];
float direction = arc4random()%100/100.0 * M_PI*2;
LinearMotion* motion = [LinearMotion linearMotionInDirection:direction AtSpeed:1];
[motion setWrap:YES];
[comet addBehavior: motion];
ExpireAfterTime* expire = [ExpireAfterTime expireAfter:60*15];
[comet addBehavior: expire];
return comet;
}
In Listing 7-24, we see that we again create each Comet offscreen by calculating CGPoint
called center, which is on a circle just off the edge of the game area. We also create a
VectorRepesentation and use the Comet as the delegate . We will be drawing the Comet and the
Comet 's tail particles with the same tasks. After specifying an ExpireAfterTime and a LinearMotion
as the Comet 's behavior, it is ready to be added to the scene. Listing 7-25 shows the task that draws
each Comet and Particle .
Listing 7-25. Comet.m (drawActor:WithContect:InRect:)
-(void)drawActor:(Actor*)anActor WithContext:(CGContextRef)context InRect:(CGRect)rect{
CGContextClearRect(context,rect);
CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB();
CGFloat locations[4];
locations[0] = 0.0;
locations[1] = 0.1;
locations[2] = 0.2;
locations[3] = 1.0;
UIColor* color1 = nil;
UIColor* color2 = nil;
UIColor* color3 = nil;
 
Search WWH ::




Custom Search