Referencing stage in a Flex App.

I’m probably not the first to blog on this, but some repetition is often valuable 🙂 Most examples illustrating integration of AS 3 with Flex show a function executed on the creationComplete event of the Application. It is helpful to understand the startup order of events in a Flex application.

If your attempts to access the stage in a Flex app. result in a null reference, try executing your script on the applicationComplete event, as shown in the following code segment (make sure the display object has been added to the display list). This particular segment is taken from a test driver that allows bones to be drawn interactively with mouse clicks, similar to 3ds max. I’ve removed much of the MXML and script that is not relevant to the topic.


<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" applicationComplete="test()" width="600" height="500">
.
.
.
<mx:Script>
<![CDATA[
import Singularity.Rigs.Bone;
import Singularity.Rigs.Chain;
import Singularity.Numeric.Consts;

import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;

import mx.controls.sliderClasses.Slider;
import mx.core.UIComponent;
import mx.events.SliderEvent;

// bone (bn) namespace
public namespace bn;

bn var __move:Boolean = false;
bn var __count:uint = 0;
bn var __bone:Bone = null;
bn var __chain:Chain = new Chain();
bn var __hand:Chain = new Chain();
bn var __isTerminated:Boolean = false;

bn var __myReference:UIComponent = new UIComponent();

// user-interface (ui) namespace
public namespace ui;
ui var __stage:Stage = null; // Stage reference

public function test():void
{
addChild(bn::__myReference);

bn::__myReference.addChild(bn::__chain);
bn::__myReference.addChild(bn::__hand);

// notification of low-level bone interactivity
bn::__chain.setNotify(onBone);

// stage listeners
ui::__stage = bn::__chain.stage;
ui::__stage.addEventListener(MouseEvent.CLICK, onSelected, true);
ui::__stage.addEventListener(KeyboardEvent.KEY_DOWN, terminate);

// add the hand bone to the hand chain
bn::__hand.addBoneAt(30, 70, 30, 50, “hand”, 0, Bone.SQUARE);
bn::__hand.setNotify(onBone);
bn::__hand.enabled = true;

__status__.text = “Click anywhere to start drawing a bone. New clicks create new bones. Click SPACE BAR to end the chain”;
}
.
.
.
If the test() function was executed on the creationComplete event, the reference

ui::__stage = bn::__chain.stage;

would have been null. When executed on applicationComplete, everything works as expected. I hope this small observation helps you in a future application.

Some old downloads

Following are links to some of the more popular downloads from the old Singularity blog.

AS 3 Priority Queue

AS 2 Prime Factorization

AS 2 Pascal’s Triangle (forward and reverse recursion)

AS 2 Recursive Taylor Series Example [ TechNote reference here ]

AS 2 Timer Class (choreograph events in timed sequence)

AS 2 Pair and Triple classes – STL-style pair and triple used for coordinates. Includes great-circle distance.

AS 2 – The magic of the number 1089.

The XML Portfolio Viewer is at Ultrashock.com (Flash 8 tutorials section).