09.12.2016 Views

starling-handbook-preview

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

public function advanceTime(passedTime:Number):void<br />

{<br />

_bird.advanceTime(passedTime);<br />

}<br />

The advanceTime method moves the playhead forward by the given time. Thus, we want this method<br />

to be called once per frame, and the time parameter needs to contain the time passed since the<br />

previous frame. Sounds like a job for our Game class, right? Open it up and add the following code:<br />

public function start(assets:AssetManager):void<br />

{<br />

/* ... */<br />

}<br />

addEventListener(Event.ENTER_FRAME, onEnterFrame);<br />

private function onEnterFrame(event:Event, passedTime:Number):void<br />

{<br />

_world.advanceTime(passedTime);<br />

}<br />

This code introduces a concept that hasn’t been mentioned before: events. We will look into that<br />

topic in detail a little later, but that code should be rather intuitive. We listen to an ENTER_FRAME<br />

-event, which is a standard event that’s dispatched once per frame to every display object. As a<br />

result, the method onEnterFrame (the event handler) will be called repeatedly.<br />

The event handler also contains an argument called passedTime, which is just what we need in the<br />

advanceTime method we just defined on World.<br />

Which means that everything is in place to get that bird flapping! Compile and run the code now to<br />

see it in action.<br />

Scrolling<br />

Right now, the bird is flying in empty space. We will now add some grassland below to give the<br />

player some sense of the bird’s height and movement. Let’s start with the following additions to the<br />

World class:<br />

15

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!