PDA

View Full Version : communicating with stage or root from an AS3 class



the_lar
December 9th, 2007, 09:28 AM
Hi all,

I'm using the following code from an AS3 class which extends a Library symbol to tell the main timeline to do something:

MovieClip(this.root).gotoAndStop("game1");I know I 'should' use stage, but it's not working for me. EG:

package {
import flash.display.MovieClip;
import flash.display.Stage;
import flash.display.SimpleButton;
import flash.events.MouseEvent;

public class StageTest extends MovieClip {
private var theStage:Stage;
public function StageTest(stageRef:Stage){
theStage = stageRef;
init();
}
private function init():void{
SimpleBttn.addEventListener(MouseEvent.MOUSE_UP, simpleBttnHandler);
}
private function simpleBttnHandler(event:MouseEvent):void{
doSomething();
}
private function doSomething():void{
theStage.gotoAndPlay("something interesting");
}
}
}What am I doing wrong here?
Kind regards
Kevin

Dazzer
December 9th, 2007, 09:37 AM
Stage is just a DisplayObjectContainer, and does not control the timeline. Therefore it does not have a gotoAndPlay method. Only a MovieClip has that, and that is the root object.

the_lar
December 9th, 2007, 10:02 AM
I see, so you can't say:

stage.gotoAndPlay("label");Is my first example (above) the only solution then? If you can't use stage in this way, why do they say that stage is a replacement for the AS2 property _root?

Gathan
December 9th, 2007, 02:24 PM
I see, so you can't say:

stage.gotoAndPlay("label");Is my first example (above) the only solution then? If you can't use stage in this way, why do they say that stage is a replacement for the AS2 property _root?
Will the documentation isn't necessary wrong, though you would think so in this case.
The stage is the root of the entire display list.
Whoever the maintimeline is a child of the stage object. Everything is added onto this unless specified to the stage object instead.
The stage object isn't something you should ever be concerned with, unless you want to add objects onto that for some reason.

Dazzer
December 9th, 2007, 11:17 PM
Or to resize etc etc.



function onSomethingClicked(e:MouseEvent):void{
if(this.root != null){
this.root.gotoAndPlay(2);
}
}

Dazzer
December 9th, 2007, 11:27 PM
Oh you might have to typecast "root" to a MovieClip object. Else compiler will go "ah this doesn't work!" lol