rn2071
04-02-2008, 03:03 PM
Greetings,
I'm creating my first AS3 project and I've already hit my first snag, nested display objects (aka buttons and movieClips). Let's say you create a movieClip and place buttonA on frame 1 and buttonB on frame 10. You create a class for that movieClip that tells it to jump to frame X after evoking a custom method. With the same method you want to assign the buttons on that frame their functions, but you can't because Flash thinks that DisplayObject does not exist.
How do you make sure all DisplayObjects for a frame have been loaded, so that you can reference them?
I have figured a way around it by using the ADDED event listener. Apparently this event is triggered every time any DisplayObject is added, whether it is nested or not. What's interesting is the number of children (numChildren) for the parent movieClip stays constant. So, this code takes advantage of that fact.
package com {
import flash.display.*;
import flash.events.*;
public class Pages extends MovieClip {
var lbl:String;
function Pages(prmLabel:String) {
trace("Pages");
changePage(prmLabel);
}
function changePage(prmLabel:String):void {
trace("Change Page: " + prmLabel);
lbl = prmLabel;
this.gotoAndStop(lbl);
this.addEventListener(Event.ADDED, lstAdded);
}
function lstAdded(e:Event):void {
trace("lstAdded");
var frameLoaded:Boolean = true;
for (var i:Number = 0; i < numChildren; i++) {
if (getChildAt(i) == null) {
frameLoaded = false;
}
}
if (frameLoaded) {
this.removeEventListener(Event.ADDED, lstAdded);
switch (lbl) {
case "one":
getChildByName("btnNext").addEventListener(MouseEvent.CLICK, fncNext);
break;
case "two":
getChildByName("btnBack").addEventListener(MouseEvent.CLICK, fncBack);
break;
}
}
}
function fncNext(e:MouseEvent):void {
trace("NEXT");
changePage("two");
}
function fncBack(e:MouseEvent):void {
trace("BACK");
changePage("one");
}
}
}
Personally, I like having menus and such spaced along a time line, especially when they will share things like backgrounds and certain bits of text.
I don't know if there is a better way to do this, or an Event type that I'm not aware of, but this is the only way I can get'er done. Any advice is always welcome. ;)
I'm creating my first AS3 project and I've already hit my first snag, nested display objects (aka buttons and movieClips). Let's say you create a movieClip and place buttonA on frame 1 and buttonB on frame 10. You create a class for that movieClip that tells it to jump to frame X after evoking a custom method. With the same method you want to assign the buttons on that frame their functions, but you can't because Flash thinks that DisplayObject does not exist.
How do you make sure all DisplayObjects for a frame have been loaded, so that you can reference them?
I have figured a way around it by using the ADDED event listener. Apparently this event is triggered every time any DisplayObject is added, whether it is nested or not. What's interesting is the number of children (numChildren) for the parent movieClip stays constant. So, this code takes advantage of that fact.
package com {
import flash.display.*;
import flash.events.*;
public class Pages extends MovieClip {
var lbl:String;
function Pages(prmLabel:String) {
trace("Pages");
changePage(prmLabel);
}
function changePage(prmLabel:String):void {
trace("Change Page: " + prmLabel);
lbl = prmLabel;
this.gotoAndStop(lbl);
this.addEventListener(Event.ADDED, lstAdded);
}
function lstAdded(e:Event):void {
trace("lstAdded");
var frameLoaded:Boolean = true;
for (var i:Number = 0; i < numChildren; i++) {
if (getChildAt(i) == null) {
frameLoaded = false;
}
}
if (frameLoaded) {
this.removeEventListener(Event.ADDED, lstAdded);
switch (lbl) {
case "one":
getChildByName("btnNext").addEventListener(MouseEvent.CLICK, fncNext);
break;
case "two":
getChildByName("btnBack").addEventListener(MouseEvent.CLICK, fncBack);
break;
}
}
}
function fncNext(e:MouseEvent):void {
trace("NEXT");
changePage("two");
}
function fncBack(e:MouseEvent):void {
trace("BACK");
changePage("one");
}
}
}
Personally, I like having menus and such spaced along a time line, especially when they will share things like backgrounds and certain bits of text.
I don't know if there is a better way to do this, or an Event type that I'm not aware of, but this is the only way I can get'er done. Any advice is always welcome. ;)