Starling handbook Preview
Starling handbook Preview - Gamua, Starling Framework, Actionscript 3, AS3, Adobe AIR
Starling handbook Preview - Gamua, Starling Framework, Actionscript 3, AS3, Adobe AIR
Erfolgreiche ePaper selbst erstellen
Machen Sie aus Ihren PDF Publikationen ein blätterbares Flipbook mit unserer einzigartigen Google optimierten e-Paper Software.
Don’t forget to assign birdVelocity a value (zero, in this case). AS3 has a very<br />
peculiar default value for variables of type Number: NaN (not a number). So much<br />
for language designers not having a sense of humor!<br />
The next step is to update birdVelocity each frame, and to move the bird by that velocity.<br />
public function advanceTime(passedTime:Number):void<br />
{<br />
/* ... */<br />
}<br />
advancePhysics(passedTime); 1<br />
private function advancePhysics(passedTime:Number):void<br />
{<br />
_bird.y += _birdVelocity * passedTime; 2<br />
_birdVelocity += GRAVITY * passedTime; 3<br />
}<br />
public function flapBird():void 4<br />
{<br />
_birdVelocity = FLAP_VELOCITY;<br />
}<br />
1 The new method advancePhysics will contain our tiny physics engine.<br />
2 Move the bird along the y-axis, according to its current velocity.<br />
3 Add the GRAVITY to the bird’s current velocity.<br />
4 This method will be called when the player touches the screen. It instantly sets the bird’s<br />
velocity to a fixed value.<br />
That’s actually all our physics code. You probably agree that adding a full-fledged physics library<br />
would have been an overkill in this situation!<br />
There’s one more step to do: allowing the player to control the bird, i.e. to call that new flapBird<br />
method. Please open up the Game class and make the following modifications:<br />
20