Packagestarling.core
Classpublic class Starling
InheritanceStarling Inheritance EventDispatcher Inheritance Object

The Starling class represents the core of the Starling framework.

The Starling framework makes it possible to create 2D applications and games that make use of the Stage3D architecture introduced in Flash Player 11. It implements a display tree system that is very similar to that of conventional Flash, while leveraging modern GPUs to speed up rendering.

The Starling class represents the link between the conventional Flash display tree and the Starling display tree. To create a Starling-powered application, you have to create an instance of the Starling class:

var starling:Starling = new Starling(Game, stage);

The first parameter has to be a Starling display object class, e.g. a subclass of starling.display.Sprite. In the sample above, the class "Game" is the application root. An instance of "Game" will be created as soon as Starling is initialized. The second parameter is the conventional (Flash) stage object. Per default, Starling will display its contents directly below the stage.

It is recommended to store the Starling instance as a member variable, to make sure that the Garbage Collector does not destroy it. After creating the Starling object, you have to start it up like this:

starling.start();

It will now render the contents of the "Game" class in the frame rate that is set up for the application (as defined in the Flash stage).

Context3D Profiles

Stage3D supports different rendering profiles, and Starling works with all of them. The last parameter of the Starling constructor allows you to choose which profile you want. The following profiles are available:

The recommendation is to deploy your app with the profile "auto" (which makes Starling pick the best available of those), but to test it in all available profiles.

Accessing the Starling object

From within your application, you can access the current Starling object anytime through the static method Starling.current. It will return the active Starling instance (most applications will only have one Starling object, anyway).

Viewport

The area the Starling content is rendered into is, per default, the complete size of the stage. You can, however, use the "viewPort" property to change it. This can be useful when you want to render only into a part of the screen, or if the player size changes. For the latter, you can listen to the RESIZE-event dispatched by the Starling stage.

Native overlay

Sometimes you will want to display native Flash content on top of Starling. That's what the nativeOverlay property is for. It returns a Flash Sprite lying directly on top of the Starling content. You can add conventional Flash objects to that overlay.

Beware, though, that conventional Flash content on top of 3D content can lead to performance penalties on some (mobile) platforms. For that reason, always remove all child objects from the overlay when you don't need them any longer.

Multitouch

Starling supports multitouch input on devices that provide it. During development, where most of us are working with a conventional mouse and keyboard, Starling can simulate multitouch events with the help of the "Shift" and "Ctrl" (Mac: "Cmd") keys. Activate this feature by enabling the simulateMultitouch property.

Skipping Unchanged Frames

It happens surprisingly often in an app or game that a scene stays completely static for several frames. So why redraw the stage at all in those situations? That's exactly the point of the skipUnchangedFrames-property. If enabled, static scenes are recognized as such and the back buffer is simply left as it is. On a mobile device, the impact of this feature can't be overestimated! There's simply no better way to enhance battery life. Make it a habit to always activate it; look at the documentation of the corresponding property for details.

Handling a lost render context

On some operating systems and under certain conditions (e.g. returning from system sleep), Starling's stage3D render context may be lost. Starling will try to recover from a lost context automatically; to be able to do this, it will cache textures in RAM. This will take up quite a bit of extra memory, though, which might be problematic especially on mobile platforms. To avoid the higher memory footprint, it's recommended to load your textures with Starling's "AssetManager"; it is smart enough to recreate a texture directly from its origin.

In case you want to react to a context loss manually, Starling dispatches an event with the type "Event.CONTEXT3D_CREATE" when the context is restored, and textures will execute their root.onRestore callback, to which you can attach your own logic. Refer to the "Texture" class for more information.

Sharing a 3D Context

Per default, Starling handles the Stage3D context itself. If you want to combine Starling with another Stage3D engine, however, this may not be what you want. In this case, you can make use of the shareContext property:

  1. Manually create and configure a context3D object that both frameworks can work with (ideally through RenderUtil.requestContext3D and context.configureBackBuffer).
  2. Initialize Starling with the stage3D instance that contains that configured context. This will automatically enable shareContext.
  3. Call start() on your Starling instance (as usual). This will make Starling queue input events (keyboard/mouse/touch).
  4. Create a game loop (e.g. using the native ENTER_FRAME event) and let it call Starling's nextFrame as well as the equivalent method of the other Stage3D engine. Surround those calls with context.clear() and context.present().

The Starling wiki contains a tutorial with more information about this topic.

See also

starling.utils.AssetManager
starling.textures.Texture


Public Properties
 PropertyDefined By
  all : Vector.<Starling>
[static] [read-only] All Starling instances.
Starling
  antiAliasing : int
The anti-aliasing level.
Starling
  contentScaleFactor : Number
[read-only] The ratio between viewPort width and stage width.
Starling
  context : Context3D
[read-only] The render context of this instance.
Starling
  contextValid : Boolean
[read-only] Indicates if the Context3D object is currently valid (i.e.
Starling
  current : Starling
[static] [read-only] The currently active Starling instance.
Starling
  discardSystemGestures : Boolean
When enabled, all touches that start very close to the screen edges are discarded.
Starling
  enableErrorChecking : Boolean
Indicates if Stage3D render methods will report errors.
Starling
  frameID : uint
[read-only] The number of frames that have been rendered since this instance was created.
Starling
  isStarted : Boolean
[read-only] Indicates if this Starling instance is started.
Starling
  juggler : Juggler
[read-only] The default juggler of this instance.
Starling
  multitouchEnabled : Boolean
[static] Indicates if multitouch input should be supported.
Starling
  nativeOverlay : Sprite
[read-only] A Flash Sprite placed directly on top of the Starling content.
Starling
  nativeOverlayBlocksTouches : Boolean
If enabled, touches or mouse events on the native overlay won't be propagated to Starling.
Starling
  nativeStage : Stage
[read-only] The Flash (2D) stage object Starling renders beneath.
Starling
  painter : Painter
[read-only] The painter, which is used for all rendering.
Starling
  profile : String
[read-only] The Context3D profile of the current render context, or null if the context has not been created yet.
Starling
  root : DisplayObject
[read-only] The instance of the root class provided in the constructor.
Starling
  rootClass : Class
The class that will be instantiated by Starling as the 'root' display object.
Starling
  shareContext : Boolean
Indicates if another Starling instance (or another Stage3D framework altogether) uses the same render context.
Starling
  showStats : Boolean
Indicates if a small statistics box (with FPS, memory usage and draw count) is displayed.
Starling
  simulateMultitouch : Boolean
Indicates if multitouch simulation with "Shift" and "Ctrl"/"Cmd"-keys is enabled.
Starling
  skipUnchangedFrames : Boolean
When enabled, Starling will skip rendering the stage if it hasn't changed since the last frame.
Starling
  stage : Stage
[read-only] The Starling stage object, which is the root of the display tree that is rendered.
Starling
  stage3D : Stage3D
[read-only] The Flash Stage3D object Starling renders into.
Starling
  supportBrowserZoom : Boolean
If enabled, the Stage3D back buffer will change its size according to the browser zoom value - similar to what's done when "supportHighResolutions" is enabled.
Starling
  supportHighResolutions : Boolean
Indicates that if the device supports HiDPI screens Starling will attempt to allocate a larger back buffer than indicated via the viewPort size.
Starling
  touchProcessor : TouchProcessor
The TouchProcessor is passed all mouse and touch input and is responsible for dispatching TouchEvents to the Starling display tree.
Starling
  viewPort : Rectangle
The viewport into which Starling contents will be rendered.
Starling
Public Methods
 MethodDefined By
  
Starling(rootClass:Class, stage:Stage, viewPort:Rectangle = null, stage3D:Stage3D = null, renderMode:String = auto, profile:Object = auto)
Creates a new Starling instance.
Starling
 Inherited
addEventListener(type:String, listener:Function):void
Registers an event listener at a certain object.
EventDispatcher
  
advanceTime(passedTime:Number):void
Dispatches ENTER_FRAME events on the display list, advances the Juggler and processes touches.
Starling
 Inherited
dispatchEvent(event:Event):void
Dispatches an event to all objects that have registered listeners for its type.
EventDispatcher
 Inherited
dispatchEventWith(type:String, bubbles:Boolean = false, data:Object = null):void
Dispatches an event with the given parameters to all objects that have registered listeners for the given type.
EventDispatcher
  
dispose():void
Disposes all children of the stage and the render context; removes all registered event listeners.
Starling
 Inherited
hasEventListener(type:String, listener:Function = null):Boolean
If called with one argument, figures out if there are any listeners registered for the given event type.
EventDispatcher
  
Make this Starling instance the current one.
Starling
  
nextFrame():void
Calls advanceTime() (with the time that has passed since the last frame) and render().
Starling
 Inherited
removeEventListener(type:String, listener:Function):void
Removes an event listener from the object.
EventDispatcher
 Inherited
removeEventListeners(type:String = null):void
Removes all event listeners with a certain type, or all of them if type is null.
EventDispatcher
  
render():void
Renders the complete display list.
Starling
  
Makes sure that the next frame is actually rendered.
Starling
  
showStatsAt(horizontalAlign:String = left, verticalAlign:String = top, scale:Number = 1):void
Displays the statistics box at a certain position.
Starling
  
start():void
As soon as Starling is started, it will queue input events (keyboard/mouse/touch); furthermore, the method nextFrame will be called once per Flash Player frame.
Starling
  
stop(suspendRendering:Boolean = false):void
Stops all logic and input processing, effectively freezing the app in its current state.
Starling
  
stopWithFatalError(message:String):void
Stops Starling right away and displays an error message on the native overlay.
Starling
Events
 Event Summary Defined By
  Dispatched when a new render context is created.Starling
  Dispatched when a fatal error is encountered.Starling
  Dispatched when the display list is about to be rendered.Starling
  Dispatched when the root class has been created.Starling
Public Constants
 ConstantDefined By
  VERSION : String = 2.7
[static] The version of the Starling framework.
Starling
Property Detail
allproperty
all:Vector.<Starling>  [read-only]

All Starling instances.

CAUTION: not a copy, but the actual object! Do not modify!


Implementation
    public static function get all():Vector.<Starling>
antiAliasingproperty 
antiAliasing:int

The anti-aliasing level. 0 - none, 16 - maximum.

The default value is 0.


Implementation
    public function get antiAliasing():int
    public function set antiAliasing(value:int):void
contentScaleFactorproperty 
contentScaleFactor:Number  [read-only]

The ratio between viewPort width and stage width. Useful for choosing a different set of textures depending on the display resolution.


Implementation
    public function get contentScaleFactor():Number
contextproperty 
context:Context3D  [read-only]

The render context of this instance.


Implementation
    public function get context():Context3D
contextValidproperty 
contextValid:Boolean  [read-only]

Indicates if the Context3D object is currently valid (i.e. it hasn't been lost or disposed).


Implementation
    public function get contextValid():Boolean
currentproperty 
current:Starling  [read-only]

The currently active Starling instance.


Implementation
    public static function get current():Starling
discardSystemGesturesproperty 
discardSystemGestures:Boolean

When enabled, all touches that start very close to the screen edges are discarded. On mobile, such touches often indicate swipes that are meant to use OS features. Per default, margins of 15 points at the top, bottom, and left side of the screen are checked. Call starling.touchProcessor.setSystemGestureMargins() to adapt the margins in each direction.

The default value is true on mobile, false on desktop.


Implementation
    public function get discardSystemGestures():Boolean
    public function set discardSystemGestures(value:Boolean):void
enableErrorCheckingproperty 
enableErrorChecking:Boolean

Indicates if Stage3D render methods will report errors. It's recommended to activate this when writing custom rendering code (shaders, etc.), since you'll get more detailed error messages. However, it has a very negative impact on performance, and it prevents ATF textures from being restored on a context loss. Never activate for release builds!

The default value is false.


Implementation
    public function get enableErrorChecking():Boolean
    public function set enableErrorChecking(value:Boolean):void
frameIDproperty 
frameID:uint  [read-only]

The number of frames that have been rendered since this instance was created.


Implementation
    public function get frameID():uint
isStartedproperty 
isStarted:Boolean  [read-only]

Indicates if this Starling instance is started.


Implementation
    public function get isStarted():Boolean
jugglerproperty 
juggler:Juggler  [read-only]

The default juggler of this instance. Will be advanced once per frame.


Implementation
    public function get juggler():Juggler
multitouchEnabledproperty 
multitouchEnabled:Boolean

Indicates if multitouch input should be supported. You can enable or disable multitouch at any time; just beware that any current touches will be cancelled.


Implementation
    public static function get multitouchEnabled():Boolean
    public static function set multitouchEnabled(value:Boolean):void
nativeOverlayproperty 
nativeOverlay:Sprite  [read-only]

A Flash Sprite placed directly on top of the Starling content. Use it to display native Flash components.


Implementation
    public function get nativeOverlay():Sprite
nativeOverlayBlocksTouchesproperty 
nativeOverlayBlocksTouches:Boolean

If enabled, touches or mouse events on the native overlay won't be propagated to Starling.

The default value is true.


Implementation
    public function get nativeOverlayBlocksTouches():Boolean
    public function set nativeOverlayBlocksTouches(value:Boolean):void
nativeStageproperty 
nativeStage:Stage  [read-only]

The Flash (2D) stage object Starling renders beneath.


Implementation
    public function get nativeStage():Stage
painterproperty 
painter:Painter  [read-only]

The painter, which is used for all rendering. The same instance is passed to all rendermethods each frame.


Implementation
    public function get painter():Painter
profileproperty 
profile:String  [read-only]

The Context3D profile of the current render context, or null if the context has not been created yet.


Implementation
    public function get profile():String
rootproperty 
root:DisplayObject  [read-only]

The instance of the root class provided in the constructor. Available as soon as the event 'ROOT_CREATED' has been dispatched.


Implementation
    public function get root():DisplayObject
rootClassproperty 
rootClass:Class

The class that will be instantiated by Starling as the 'root' display object. Must be a subclass of 'starling.display.DisplayObject'.

If you passed null as first parameter to the Starling constructor, you can use this property to set the root class at a later time. As soon as the class is instantiated, Starling will dispatch a ROOT_CREATED event.

Beware: you cannot change the root class once the root object has been instantiated.


Implementation
    public function get rootClass():Class
    public function set rootClass(value:Class):void
shareContextproperty 
shareContext:Boolean

Indicates if another Starling instance (or another Stage3D framework altogether) uses the same render context. If enabled, Starling will not execute any destructive context operations (e.g. not call 'configureBackBuffer', 'clear', 'present', etc. This has to be done manually, then.

The default value is false.


Implementation
    public function get shareContext():Boolean
    public function set shareContext(value:Boolean):void
showStatsproperty 
showStats:Boolean

Indicates if a small statistics box (with FPS, memory usage and draw count) is displayed.

When the box turns dark green, more than 50% of the frames since the box' last update could skip rendering altogether. This will only happen if the property skipUnchangedFrames is enabled.

Beware that the memory usage should be taken with a grain of salt. The value is determined via System.totalMemory and does not take texture memory into account. It is recommended to use Adobe Scout for reliable and comprehensive memory analysis.


Implementation
    public function get showStats():Boolean
    public function set showStats(value:Boolean):void
simulateMultitouchproperty 
simulateMultitouch:Boolean

Indicates if multitouch simulation with "Shift" and "Ctrl"/"Cmd"-keys is enabled.

The default value is false.


Implementation
    public function get simulateMultitouch():Boolean
    public function set simulateMultitouch(value:Boolean):void
skipUnchangedFramesproperty 
skipUnchangedFrames:Boolean

When enabled, Starling will skip rendering the stage if it hasn't changed since the last frame. This is great for apps that remain static from time to time, since it will greatly reduce power consumption. You should activate this whenever possible!

The reason why it's disabled by default is just that it causes problems with Render- and VideoTextures. When you use those, you either have to disable this property temporarily, or call setRequiresRedraw() (ideally on the stage) whenever those textures are changing. Otherwise, the changes won't show up.

The default value is false.


Implementation
    public function get skipUnchangedFrames():Boolean
    public function set skipUnchangedFrames(value:Boolean):void
stageproperty 
stage:Stage  [read-only]

The Starling stage object, which is the root of the display tree that is rendered.


Implementation
    public function get stage():Stage
stage3Dproperty 
stage3D:Stage3D  [read-only]

The Flash Stage3D object Starling renders into.


Implementation
    public function get stage3D():Stage3D
supportBrowserZoomproperty 
supportBrowserZoom:Boolean

If enabled, the Stage3D back buffer will change its size according to the browser zoom value - similar to what's done when "supportHighResolutions" is enabled. The resolution is updated on the fly when the zoom factor changes. Only relevant for the browser plugin.

The default value is false.


Implementation
    public function get supportBrowserZoom():Boolean
    public function set supportBrowserZoom(value:Boolean):void
supportHighResolutionsproperty 
supportHighResolutions:Boolean

Indicates that if the device supports HiDPI screens Starling will attempt to allocate a larger back buffer than indicated via the viewPort size. Note that this is used on Desktop only; mobile AIR apps still use the "requestedDisplayResolution" parameter the application descriptor XML.

The default value is false.


Implementation
    public function get supportHighResolutions():Boolean
    public function set supportHighResolutions(value:Boolean):void
touchProcessorproperty 
touchProcessor:TouchProcessor

The TouchProcessor is passed all mouse and touch input and is responsible for dispatching TouchEvents to the Starling display tree. If you want to handle these types of input manually, pass your own custom subclass to this property.


Implementation
    public function get touchProcessor():TouchProcessor
    public function set touchProcessor(value:TouchProcessor):void
viewPortproperty 
viewPort:Rectangle

The viewport into which Starling contents will be rendered.


Implementation
    public function get viewPort():Rectangle
    public function set viewPort(value:Rectangle):void
Constructor Detail
Starling()Constructor
public function Starling(rootClass:Class, stage:Stage, viewPort:Rectangle = null, stage3D:Stage3D = null, renderMode:String = auto, profile:Object = auto)

Creates a new Starling instance.

Parameters
rootClass:Class — A subclass of 'starling.display.DisplayObject'. It will be created as soon as initialization is finished and will become the first child of the Starling stage. Pass null if you don't want to create a root object right away. (You can use the rootClass property later to make that happen.)
 
stage:Stage — The Flash (2D) stage.
 
viewPort:Rectangle (default = null) — A rectangle describing the area into which the content will be rendered. Default: stage size
 
stage3D:Stage3D (default = null) — The Stage3D object into which the content will be rendered. If it already contains a context, sharedContext will be set to true. Default: the first available Stage3D.
 
renderMode:String (default = auto) — The Context3D render mode that should be requested. Use this parameter if you want to force "software" rendering.
 
profile:Object (default = auto) — The Context3D profile that should be requested.
  • If you pass a profile String, this profile is enforced.
  • Pass an Array of profiles to make Starling pick the first one that works (starting with the first array element).
  • Pass the String "auto" to make Starling pick the best available profile automatically.
Method Detail
advanceTime()method
public function advanceTime(passedTime:Number):void

Dispatches ENTER_FRAME events on the display list, advances the Juggler and processes touches.

Parameters

passedTime:Number

dispose()method 
public function dispose():void

Disposes all children of the stage and the render context; removes all registered event listeners.

makeCurrent()method 
public function makeCurrent():void

Make this Starling instance the current one.

nextFrame()method 
public function nextFrame():void

Calls advanceTime() (with the time that has passed since the last frame) and render().

render()method 
public function render():void

Renders the complete display list. Before rendering, the context is cleared; afterwards, it is presented (to avoid this, enable shareContext).

This method also dispatches an Event.RENDER-event on the Starling instance. That's the last opportunity to make changes before the display list is rendered.

setRequiresRedraw()method 
public function setRequiresRedraw():void

Makes sure that the next frame is actually rendered.

When skipUnchangedFrames is enabled, some situations require that you manually force a redraw, e.g. when a RenderTexture is changed. This method is the easiest way to do so; it's just a shortcut to stage.setRequiresRedraw().

showStatsAt()method 
public function showStatsAt(horizontalAlign:String = left, verticalAlign:String = top, scale:Number = 1):void

Displays the statistics box at a certain position.

Parameters

horizontalAlign:String (default = left)
 
verticalAlign:String (default = top)
 
scale:Number (default = 1)

start()method 
public function start():void

As soon as Starling is started, it will queue input events (keyboard/mouse/touch); furthermore, the method nextFrame will be called once per Flash Player frame. (Except when shareContext is enabled: in that case, you have to call that method manually.)

stop()method 
public function stop(suspendRendering:Boolean = false):void

Stops all logic and input processing, effectively freezing the app in its current state. Per default, rendering will continue: that's because the classic display list is only updated when stage3D is. (If Starling stopped rendering, conventional Flash contents would freeze, as well.)

However, if you don't need classic Flash contents, you can stop rendering, too. On some mobile systems (e.g. iOS), you are even required to do so if you have activated background code execution.

Parameters

suspendRendering:Boolean (default = false)

stopWithFatalError()method 
public function stopWithFatalError(message:String):void

Stops Starling right away and displays an error message on the native overlay. This method will also cause Starling to dispatch a FATAL_ERROR event.

Parameters

message:String

Event Detail
context3DCreate Event
Event Object Type: starling.events.Event

Dispatched when a new render context is created. The 'data' property references the context.

fatalError Event  
Event Object Type: starling.events.Event

Dispatched when a fatal error is encountered. The 'data' property contains an error string.

render Event  
Event Object Type: starling.events.Event

Dispatched when the display list is about to be rendered. This event provides the last opportunity to make changes before the display list is rendered.

rootCreated Event  
Event Object Type: starling.events.Event

Dispatched when the root class has been created. The 'data' property references that object.

Constant Detail
VERSIONConstant
public static const VERSION:String = 2.7

The version of the Starling framework.