Game Development Reference
In-Depth Information
viper = [Viper01 new];
CGRect frame = [self.view frame];
viper.center = CGPointMake(frame.size.width/2.0, frame.size.height/2.0);
[self.view addSubview:viper];
[viper setMoveToPoint:viper.center];
displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(updateScene)];
[displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
}
In Listing 5-3, after setting the title, we create a
UITapGestureRecognizer
and add it to the root
associated with this
UIViewController
. Adding the
UITapGestureRecognizer
to the
UIView
will
viewTapped:
to be called when the user taps the screen, giving us a chance to update
Viper01
and place it in the center of the root
UIView
.
moveToPoint
property of viper to the center of the screen, so the viper starts out
CADisplayLink
and
updateScene
. Once we call
addToRunLoop
,
updateScene
will be called every time
CADisplayLink
and
NSRunLoop
and
updateScene
, as shown in Listing 5-4.
Listing 5-4. Example01Controller.m (updateScene)
-(void)updateScene{
[viper updateLocation];
}
In Listing 5-4, in the task
updateScene
, we simply call
updateLocation
on the object
viper
. In future
examples we will do more in the task
updateScene
, but for now let's just look at how we implement
moving the one ship.
Moving the Spaceship
Each time
updateScene
is called, we have to update the location of the spaceship. This is
accomplished with a little geometry to figure out the new location of the ship for each frame of the
animation. Let's just look at
updateLocation
and understand how that task moves the spaceship.
See Listing 5-5.
Listing 5-5. Viper01.m (updateLocation)
-(void)updateLocation{
CGPoint c = [self center];