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.

Relative Positioning<br />

Right now, we are creating a Flash game with a fixed size of 320x480 pixels.<br />

However, I’d like to port the game to mobile, later.<br />

<br />

Mobile devices have all kinds of different screen sizes, so it’s a good idea to take<br />

that into account right away. I’m doing that by working with relative instead of<br />

absolute positions.<br />

// bad: fixed position<br />

_cloud1.x = 160;<br />

// better: relative position<br />

_cloud1.x = _width * 0.5;<br />

What’s left to do, of course, is to reference this new class from Game.as. Here’s how it should look<br />

like now:<br />

Game.as<br />

public class Game extends Sprite<br />

{<br />

private static var sAssets:AssetManager;<br />

private var _world:World; 1<br />

public function Game()<br />

{ }<br />

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

{<br />

sAssets = assets;<br />

}<br />

_world = new World(stage.stageWidth, stage.stageHeight); 2<br />

addChild(_world);<br />

}<br />

public static function get assets():AssetManager<br />

{<br />

return sAssets;<br />

}<br />

1 Add a new member variable.<br />

2 Instantiate the new class (passing the stage dimensions along) and add it to the stage.<br />

Compile and run to check if everything worked out. Except for the two inconspicuous new clouds, it<br />

should look just like before.<br />

11

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

Saved successfully!

Ooh no, something went wrong!