Game Development Reference
In-Depth Information
} else if (self.state == STATE_TURNING){
if (self.clockwise){
return @"viper_clockwise";
} else {
return @"viper_counterclockwise";
}
} else {//STATE_TRAVELING
return @"viper_traveling";
}
}
In Listing 5-24, the task viper name simply returns the image name based on the state of the
object. If the state is STATE_TURNING , we check the property clockwise to decide which image
should be used. In order to implement the change in behavior we have to take a look at the new
implementation of the task step: , as shown in Listing 5-25.
Listing 5-25. Viper03.m (step:)
-(void)step:(Example03Controller*)controller{
CGPoint c = [self center];
if (self.state == STATE_STOPPED){
if (abs(moveToPoint.x - c.x) < self.speed && abs(moveToPoint.y - c.y) < self.speed){
c.x = moveToPoint.x;
c.y = moveToPoint.y;
[self setCenter:c];
} else {
self.state = STATE_TURNING;
self.needsImageUpdated = YES;
}
} else if (self.state == STATE_TURNING){
float dx = (moveToPoint.x - c.x);
float dy = (moveToPoint.y - c.y);
float theta = −atan(dx/dy);
float targetRotation;
if (dy > 0){
targetRotation = theta + M_PI;
} else {
targetRotation = theta;
}
if ( fabsf(self.rotation - targetRotation) < .1){
self.rotation = targetRotation;
self.state = STATE_TRAVELING;
self.needsImageUpdated = YES;
return;
}
if (self.rotation - targetRotation < 0){
self.rotation += .1;
Search WWH ::




Custom Search