Startup Code

If you have set up your project correctly, you need to add the code that will launch Starling.

Copy the following code to your Startup-class (in Flash Builder, that's the “Default Application” of your project). Normally, that class is named like your project, so exchange the class name below with the correct name.

package 
{
    import flash.display.Sprite;
    import flash.display.StageAlign;
    import flash.display.StageScaleMode;
 
    import starling.core.Starling;
 
    [SWF(width="400", height="300", frameRate="60", backgroundColor="#000000")]
    public class Startup extends Sprite
    {
        private var mStarling:Starling;
 
        public function Startup()
        {
            // These settings are recommended to avoid problems with touch handling
            stage.scaleMode = StageScaleMode.NO_SCALE;
            stage.align = StageAlign.TOP_LEFT;
 
            // Create a Starling instance that will run the "Game" class
            mStarling = new Starling(Game, stage);
            mStarling.start();
        }
    }
}

This code creates a Starling instance and starts it right away. Note that we pass a reference to the “Game” class into the Starling constructor. Starling will instantiate that class once it is ready. (It's done that way so you don't have to take care about doing stuff in the right order.)

You'll need that Game class too, of course. Here's a simple version of it.

package
{
    import starling.display.Quad;
    import starling.display.Sprite;
    import starling.utils.Color;
 
    public class Game extends Sprite
    {
        public function Game()
        {
            var quad:Quad = new Quad(200, 200, Color.RED);
            quad.x = 100;
            quad.y = 50;
            addChild(quad);
        }
    }
}

The class just displays a simple red quad to see if we've set up the project correctly.

Note that the Game class extends “starling.display.Sprite”, not “flash.display.Sprite”! This is crucial, because we're in the Starling world now. It's completely separate from the “flash.display” package.

Now start up the project. — Bummer! It displays an error message!

That's because content that is rendered through Stage3D requires a special Flash Parameter when it's embedded in the browser. That's easy to fix, though.

If you're using Flash Builder, it will have created an HTML template at this location:

html-template/index.template.html

Other IDEs will contain a similar file. In any case, add the parameter “wmode=direct” to the code that loads the Flash Plugin – like this:

// in Flash Builder, find the following line ...
var params = {};
 
// ... and add that one directly below:
params.wmode = "direct";

Alternatively, you can start up the project with the Standalone Projector of Flash.

  • In Flash Builder, enter the project properties and deactivate “Generate HTML wrapper file” in the “ActionScript Compiler” section.

Congratulations! You have successfully compiled and run your first Starling based project. Now let the fun begin!

I've received reports that the Internet Explorer may have problems with the default Startup code in some situations. If you run across such issues, this forum post presents a workaround.


Next Section: Display Objects

  manual/startup_code.txt · Last modified: 2012/07/14 16:28 by daniel
 
Powered by DokuWiki