Groups | Blog | Home
all groups > flash actionscript > august 2007 >

flash actionscript : how can a sub class access exisiting stage item?


C-Rock
8/17/2007 6:39:49 PM
i have a button on the stage. the document class loads two sub classes. how do
you get the sub class to access/work with a stage item that is already on the
display list?

as3.0 is killing me!!! grrrr...

thanks!
abeall
8/17/2007 6:48:52 PM
In AS3 there is no global reference to the stage. If a class is not a
DisplayObject(or subclass), then it does not have a built in way to access the
stage.

To get a reference to the stage from inside a non-DisplayObjet class, you
usually pass in a reference to the stage in the subclass constructor.
C-Rock
8/17/2007 6:58:36 PM
That's what I can't find anywhere. I've look for 11 days now. Everyone is
drawing crap and using the add Child. My stuff is already on the stage. I even
spent 75 bucks on a book and can't find out how to access a stage movie clip.

I can't find the code to do this, create a new displayObject and how to get it
to find something on the root...
abeall
8/17/2007 8:03:31 PM
What can't you find? I'm not following... your stuff is already on the stage?
So then they have a 'stage' and a 'root' property already.

However, you said you have a document class which is loading two subclasses.
How are you loading those subclasses? If those subclasses are not
DisplayObjects then you will need to pass them a reference to stage or root in
some way, usually through the constructor.
C-Rock
8/17/2007 8:10:12 PM
the document class imports mapTools and moveMap. the document class is mapDC. i
can access stage items from the mapDC because it's the document class. each
class is imported and then an instance to the constructor is create. So I know
all classes are loaded and initialized.

However the loaded custom classes can't get to the display list because I
don't know how. How do I write the code for the loaded classes to access a
movie clip on the main timeline?
abeall
8/17/2007 8:26:15 PM
From in mapDC:

new mapTools(stage)

In mapTools:

private var stage:Stage;
function mapTools(targetStage){
stage = targetStage;
}

C-Rock
8/17/2007 8:27:50 PM
this project is going to have over 100 classes so i need them broke up. i've
tried importing the display class in my sub classes and using

stage.root.mapSectionMC...

and it doesn't referece the mapSectionMC on the root.
C-Rock
8/17/2007 8:38:26 PM
C-Rock
8/17/2007 8:41:41 PM
The document class mapDC:

package com.main{
// this is the main document class for the rextag map appllication
// import movie clip to extend to use on stage
import flash.display.MovieClip;
import flash.events.MouseEvent;
import com.main.tools.mapTools;
//
public class mapDC extends MovieClip {
// instance of class
public function mapDC() {
trace("MainMap initialized");
new mapTools(stage);
}
public function test() {
trace("here at test");
}
}
}




The mapTools Class:


package com.main.tools{
// import
import flash.events.MouseEvent;
import flash.display.*;
// this class will contain all the move methods and functions
public class mapTools {
private var stage:Stage;
function mapTools(targetStage) {
trace("Map tools initialized");
stage = targetStage;

stage.root.mapSection.eastArrow.addEventListener(MouseEvent.ROLL_OVER,testOut
put);
}
//---------- test output function
----------------------------------------------------------------------------
public function testOutput(evt:MouseEvent) {
trace("worked");

}
}
}
C-Rock
8/17/2007 8:43:59 PM
didn't work. it initializes the mapTools class. but then i can't get the code to actually access the item to work.

stage.root.mapSection... // errors
stage.mapSection..... //errors

abeall
8/17/2007 10:00:46 PM
What error do you get? Is mapSection a timeline MovieClip? In that case you need:
C-Rock
8/17/2007 10:14:43 PM
When this is the code for the movieClip access:

MovieClip(stage.root).mapSection.eastArrow.addEventListener(MouseEvent.ROLL_OVER
,testOutput);

this is the error:
MainMap initialized
Map tools initialized
TypeError: Error #1034: Type Coercion failed: cannot convert
flash.display::Stage@171aa1e9 to flash.display.MovieClip.
at com.main.tools::mapTools$iinit()
at com.main::mapDC$iinit()


thanks for all your help so far!
abeall
8/17/2007 11:32:55 PM
I'm sorry, that was mistake. Stage.root is a reference to stage. Don't know
why. So I think you'll need to pass in a reference to root instead of stage.

Anything you create in the IDE on the timeline will be inside of root.

From in mapDC:

new mapTools(root)

In mapTools:

private var root:MovieClip;
function mapTools(targetTimeline){
root = targetTimeline as MovieClip;
}
C-Rock
8/18/2007 12:39:41 AM
AHHHHH! Hell yeah! That's what I needed! Thank-you sooooo much, you just saved me from going crazy. Thanks a lot....

AddThis Social Bookmark Button