2
votes

Using Flash CS5 Professional I have created a symbol, dragged it onto the stage, and given it an instance name of GreenLight1. If I want to make this visible from the document class, I can simply do the GreenLight1.visible=true; and poof it's good to go when I test the file. As long as I stay in the document class I am good to go, but now I'm trying to move to another class and hitting ALL kinds of trouble just trying to get Flash to allow me to access this simple object. All I am looking to do is have this GreenLight1 go invisible (visible=false) when a certain condition occurs in this new class and Flash just won't let me access GreenLight1 at all.

Things I've tried thus far:

  1. stage is passed to the class and is referenced by _stage and is working just fine when I do _stage.addchild or anything like that. So I have tried "_stage.GreenLight1.visible=false;" and I get "ReferenceError: Error #1069: Property GreenLight1 not found on flash.display.Stage and there is no default value."

  2. My document class extends Sprite, so I figured I'd try the root function. So I tried "Sprite(root).GreenLight1.visible=false;" and I get "1119: Access of possibly undefined property GreenLight1 through a reference with static type flash.display:Sprite."

  3. Finally on the advice of the answer in this thread I tried to create the Resource class as described therein. To which I came across the same problem that I started with in that it doesn't know what GreenLight1 is to begin with so I got "1120: Access of undefined property GreenLight1." Here is my code for Resource.as (am I supposed to pass something to this class from the document class?):

`

package {

import flash.events.Event;
import flash.display.Sprite;
import flash.display.DisplayObject;

public class Resource extends Sprite {
    public static var GL1:GreenLight;

    public function Resource() {
        addEventListener(Event.ADDED_TO_STAGE, init, false, 0, true);
    }

    private function init(e:Event):void{
        Resource.GL1 = GreenLight1;
    }
}

}

The type "GreenLight" is from the source symbol of GreenLight1. I have "Export for ActionScript" checked off and the base class is called GreenLight. So that is where that comes from. Am I supposed to make a "new GreenLight" somewhere or something like that?? I the class that I'm trying to access it from I am using "Resource.GL1.visible=false;", but it never really gets to worry about that because I get the compile error listed in #3 above.

In any event, I'm at a loss as to what to try next. So... How in the world do I get a class that isn't the document class to recognize GreenLight1?

2

2 Answers

2
votes

1 doesn't work because the stage property of an object(assuming it's on the display) is a Stage object. By default, your document class will be the first child of the stage, unless you have inserted something in there using setChildIndex(0) or addChildAt(someObject,0). So you should be able to access via the document class with

this.stage.getChildAt(0).GreenLight1;

2 doesn't work because you are casting your root as a Sprite. It's not a Sprite, it's your document class that is a descendant of Sprite, so this should work:

this.root.GreenLight1

I'm going to skip over number three and try to offer up a more direct solution. You have this symbol in your library, you've set it to 'Export for actionscript' and have given it a class name of GreenLight. Good start. So now anywhere in your code you can do something like this:

var myGreenLight:GreenLight = new GreenLight();

which has created a reference (myGreenLight) to a new instance of your GreenLight symbol. You can now attach this to the display tree of your calling class with

addChild(myGreenLight);

Assuming that the class you're coding in is itself on the stage, then your instance of GreenLight should be visible. You could also, from any object that's on the stage, call this.stage.addChild(myGreenLight); to attach your GreenLight instance directly to the stage if that was what you wanted.

So now, finally to the real question. You have an instance of GreenLight on the stage called GreenLight1. (Please note, by convention only class names start with a capital, variable names and instance names should start with a lower case character). You have another class which is also on the display tree and you need to get a reference to GreenLight1 that's on the stage. Here's a function to do that:

function getMovieClip($instanceName:String,$scope:DisplayObjectContainer):DisplayObject
{
    var child:DisplayObject;
    var loopLength:int = $scope.numChildren;
    for(var i:int = 0; i < loopLength; i++) {
        child = $scope.getChildAt(i);
        if(child.name == $instanceName) return child;
    }
    //didn't find it
    return null;
}

and you use it from any object that can access stage like this:

var greenLightRef:GreenLight = getMovieClip('GreenLight1',this.stage) as GreenLight;
0
votes

For option 3, the Resource class is the document class (so change the extend Sprite to MovieClip). So it will be able to access any instance placed on stage. If you still get errors, double check that you have indeed named the instance or that you do not have multiple keyframes on the timeline with the instance not being on the first frame.