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 starling.core.Starling;
 
    [SWF(width="400", height="300", frameRate="60", backgroundColor="#000000")]
    public class Startup extends Sprite
    {
        private var _starling:Starling;
 
        public function Startup()
        {
            // Create a Starling instance that will run the "Game" class
            _starling = new Starling(Game, stage);
            _starling.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.

First Launch

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.

AIR

If we're talking about an AIR project, you will need to edit the AIR application descriptor, which is probably called “Project-app.xml” or similar, depending on your project name. Find and update the “renderMode” XML tag (which might be commented out) to the value “direct”.

Find this:
<!-- <renderMode></renderMode> -->
 
Replace with this:
<renderMode>direct</renderMode>

Flash Player

If you are creating a project targeting the Flash Player, I recommend to let it start in the Debug Flash Player during development. That's very convenient, since you don't need to modify any browser plugin. You can get the last debug Player (called “Flash Player Projector Content Debugger”) here. Disable any “HTML Wrapper” that your IDE might offer in project or compilation settings.

Some day, though, you'll want to embed your SWF file into an HTML page. That's when you'll see the error message I showed above – which can be fixed by changing the “wmode” Flash parameter to the value “direct” in the HTML file.

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

Fixed Launch

Congratulations! You have successfully compiled and run your first Starling based project.

Seriously, the most difficult part lies behind you; I always find setting up the development environment until everything is ready to compile and run the most daunting part. We're here to write some code, right?

So, finally, let's get to that!

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: 2016/04/22 08:17 by daniel
 
Powered by DokuWiki