About Orland MediaDevelopmentTrainingBlogRSS FeedContact us
Microphone

ActionScript 3 Course

Date: Wednesday 25th March 2009
Time: 18:19
Category: ActionScript 3.0
Views: 489
Comments: No comments

We are about to schedule an AS3 course for May 2009. If you’re interested in booking a place please let us know.

Rate this post
1 star2 stars3 stars4 stars5 stars   (Roll over and click)

ADDED_TO_STAGE event fires twice

Date: Monday 23rd March 2009
Time: 14:38
Category: ActionScript 3.0
Views: 1,524
Comments: 2 Comments

Display objects often need to perform set-up tasks which depend on the stage. They might wish to access stage.stageWidth or stage.stageHeight, for example. The trouble is, the stage property will return null until that object is on the Display List.

The standard way around this is to put such set-up tasks in an init method, which you then trigger separately:

private var _box:Box;
_box = new Box();
addChild(_box);
_box.init();

So the init method is triggered once the object is added to the Display List. This works fine. The only downside is that the object cannot initialise itself. This solution seems slightly clumsy from an OOP point of view also a little verbose. Could the object not auto-trigger its own init method once it is on the Display List?

Not until Flash Player 9.0.28.0, which added support for the ADDED_TO_STAGE event, used as follows:

// constructor
public function Box():void
{
   addEventListener(Event.ADDED_TO_STAGE, init);
}

public function init(e:Event):void
{
   // code dependent on stage property
   y = stage.stageHeight-100;
}

The trouble is, however, this event erroneously fires not only for the given item, but also when its parents are added to the Display List. This runs your init method multiple times, possibly causing problems.

This issue is Flash Player bug known to Adobe. Read more about the ADDED_TO_STAGE event firing twice issue in the official bug report entry. A workaround with a sample class may be found there. We use that workaround inherited from a superclass for all our display objects, and until the issue is solved we suggest you either use it too, or use the init method solution when set-up code depends upon the stage.

Rate this post
1 star2 stars3 stars4 stars5 stars   (3 votes, average: 3.67 out of 5)
Lenovo Microsoft Adobe Heart Internet Envirowise
Copyright © 2008 Orland Media Ltd | All rights reserved Company details Ethical policy Terms & conditions Privacy policy