Copy link to clipboard
Copied
Actionscript 3.0 looping through stage and DisplayObject items
Hello,
I'm trying to convert to Actionscript 3.0 and I'm trying to find out how to loop through Stage objects as well as DisplayObject.
With AS 2.0 - I would only have to do:
for(var i in _root) // or MovieClip, this, ect
{
trace("i: " + i + " : " + _root.);
}
In AS 3.0 I found this equivalent which works w/ some objects:
public function debug_object(o:Object, show_all:Boolean):void
{
import flash.utils.*;
var def:XML = describeType(o);
var props:XMLList = def..variable.@name;
if(show_all) props += def..accessor.@name;
for each (var prop:String in props)
{
trace(prop + ": " + o[prop]);
}
}
this.showChildren(Stage);
This kind of works but the o[prop] always traces undefined and it's not showing any movieClips on the stage.
I would like to be able to have the debug object function be able to take Stage and DisplayObject arguments like such:
this.debug_object(Stage, true);
this.debug_object(flash.display.DisplayObject, true);
Any ideas on how I could achieve this?
Thanks,
Clem C
Copy link to clipboard
Copied
You're processing Class objects, not instances:
this.debug_object(Stage, true);
this.debug_object(flash.display.DisplayObject, true);
Try sending the stage instance:
this.debug_object(this.stage, true);
Note: You'll need to handle errors, such as this:
Error: Error #2071: The Stage class does not implement this property or method.
at Error$/throwError()
at flash.display::Stage/get textSnapshot()
at Untitled_fla::MainTimeline/debug_object()
at Untitled_fla::MainTimeline/frame1()
Copy link to clipboard
Copied
Raymond,
Big thanks for your help and great advice. The only problem is that I'm trying to access the Stage from a class method and I get this error:
1119: Access of possibly undefined property stage through a reference with static type com:videoControl.
Copy link to clipboard
Copied
You'll need to wait until your com.VideoControl instance is added to the stage before you can run your procedure.
In your ctor, add something like this:
this.addEventListener(Event.ADDED_TO_STAGE, someHandler);
... and add a handler:
function someHandler(event:Event)
{
if(event.type == Event.ADDED_TO_STAGE)
{
// do stuff here
}
}
Copy link to clipboard
Copied
Oh... what is the superclass of com.VideoControl?
Copy link to clipboard
Copied
MovieClip is the superclass of videoControls.
Copy link to clipboard
Copied
Good. I thought perhaps you were using something other than a DisplayObject.
So you'll need to check that the stage property if not null before using it.
Copy link to clipboard
Copied
When I trace it in the class - it's definitely null. Is that because the class isn't fully loaded at that time?
In as 2, I would do this in the constructor:
public function myClass()
{
this.onLoad = init;
}
private function init():Void
{
}
Copy link to clipboard
Copied
There is always a lag from instanciation to the DisplayObject being added to the display list. You can either listen for Event.ADDED_TO_STAGE and then use the stage object, or Event.ENTER_FRAME and check that the stage value is not null.
Copy link to clipboard
Copied
Here's my code so far . I can't get the this.addEventListener(Event.ADDED_TO_STAGE, init); to call init.
package com
{
import flash.utils.Timer;
import flash.events.Event;
import flash.display.Stage;
import flash.net.NetStream;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.net.SharedObject;
import flash.net.NetConnection;
import flash.display.MovieClip;
import flash.display.DisplayObject;
public class videoControl extends MovieClip
{
const BUFFER_TIME:Number = 8; // time to buffer for the video in sec.
const SMOOTHING:Boolean = true; // smoothing for video. may slow down old computers
const DEFAULT_VOLUME:Number = 0.6; // start volume when initializing player
const DISPLAY_TIMER_UPDATE_DELAY:int = 10; // update delay in milliseconds.
private var objInfo:Object;
private var xmlPlaylist:XML;
private var mcVideoControls;
private var intActiveVid:int;
private var nsStream:NetStream;
private var ncConnection:NetConnection;
private var strSource:String; //fix: = _root.loaderInfo.parameters.playlist == null ? "playlist.xml" : root.loaderInfo.parameters.playlist;
private var tmrDisplay:Timer;
private var urlLoader:URLLoader;
private var urlRequest:URLRequest;
private var intLastVolume:Number = DEFAULT_VOLUME;
private var thisStage:Stage;
private var bolLoaded:Boolean = false;
private var bolVolumeScrub:Boolean = false;
private var bolProgressScrub:Boolean = false;
private var bolDescriptionHover:Boolean = false;
private var bolDescriptionHoverForward:Boolean = true;
private var shoVideoPlayerSettings:SharedObject = SharedObject.getLocal("playerSettings");
public function videoControl():void
{
this.addEventListener(Event.ADDED_TO_STAGE, init);
};
public function init(evnt:Event):void
{
trace("**(videoControl:init)** flash.display.DisplayObject.stage: " + Stage + " this.stage: " + this.stage);
}
}
}
Copy link to clipboard
Copied
How are you adding the object to the display list? Timeline? Code?
Try adding an ENTER_FRAME to check the stage
public function videoControl():void
{
this.addEventListener(Event.ADDED_TO_STAGE, init);
this.addEventListener(Event.ENTER_FRAME, checkStage);
};
public function init(evnt:Event):void
{
trace("**(videoControl:init)** flash.display.DisplayObject.stage: " + Stage + " this.stage: " + this.stage);
}
public function checkStage(evnt:Event):void
{
if(stage != null)
{
this.removeEventListener(Event.ENTER_FRAME, checkStage);
trace("**ON STAGE**, stage);
}
}
Copy link to clipboard
Copied
That's probably a big part of the problem. I want this class to control all things video. I have a video screen laid out and all my controls. I have code that worked in the timeline but I'm porting it all to a class as my first AS 3 project.
Right now I'm loading the class via this publishing class which I call in the properties panel:
package
{
import com.videoControl;
import flash.display.Sprite;
public class loadVideo extends Sprite
{
private var thisVid:videoControl;
public function loadVideo()
{
this.thisVid = new videoControl();
}
}
}
Copy link to clipboard
Copied
It's weird - I can trace this.stage and it will show [object Stage] but when I loop through the stage object I get this:
tabIndex: -1
visible: true
scaleX: 1
scaleY: 1
stageFocusRect: true
stageHeight: 365
showDefaultContextMenu: true
mask: null
fullScreenWidth: 0
focus: null
alpha: 1
scaleMode: noScale
mouseChildren: true
fullScreenSourceRect: null
align:
scale9Grid: null
numChildren: 1
width: 574
rotation: 0
fullScreenHeight: 0
blendMode: normal
I do not show any items that are on the stage.
Also when I add an event handler like this:
this.addEventListener(Event.ENTER_FRAME, this.testHandler);
and trace stage I get a perpetual null item. When I trace the stage object before the evenlistener is declared, it prints out the stage object no problem?
Find more inspiration, events, and resources on the new Adobe Community
Explore Now