Reputation: 4934
I have been busy building the UI for a class file I wrote a while ago.
The problem I have is in referencing the symbols on the TimeLine. All the symbols have an instance name, and only exist in the first frame (main timeline has only one frame anyway).
At the moment I am instantiating my AS3 class file from a timeline layer using
import circles.Spirograph;
var circles:Spirograph = new Spirograph(stage);
so I have a reference to the stage in my class file.
If I have library ScrollBar on the stage called sb1
then how do I access it in the class file, and how can I get its value?
Upvotes: 0
Views: 531
Reputation: 2174
Well, not sure if I fully understand the setup you have and how you want to use things...
What I understood is that you have all your ui symbols in the main timeline, then, you have a class Spirograph
that has the logic in it and needs the symbols to reference them. I'm I right?
The fast solution following your structure would be to setup Spirograph
so that expects a DisplayObject that will contain all the ui assets. Then, you access the instances you need by name from an initialization method.
import circles.Spirograph;
var circles:Spirograph = new Spirograph( this as DisplayObject);
Then, in Spirograph
public function Spirograph(skin:DisplayObject ){
_skin = skin;
if(_skin.stage) _init();
else _skin.addEventListener(Event.ADDED_TO_STAGE,_init);
}
protected function _init(e:Event = null):void{
if(e) e.removeEventListener(e.type,arguments.callee);
_scrollbarUi = _skin.getChildByName("sb1") as Sprite;
...
}
You can check this answer to get an idea of a simple implementation but more complete than what i have posted.
Upvotes: 1
Reputation: 10235
There are a few ways. If Spirograph is a DisplayObject it will have a "root" property. You can cast this property to a MovieClip and then access sb1:
// somewhere inside your class
var mainTimeline:MovieClip = MovieClip(root);
trace(maintTimeline.sb1)
Another option is to pass "this" into the Spirograph constructor, since you're instantiating on the timeline, "this" is referring to the timeline:
var circles:Spirograph = new Spirograph(this);
Then you're stage variable could be obtained like so:
private var _stg:Stage;
public function Spirograph(main:MovieClip){
_stg = main.stage;
// get at sb1
trace(main.sb1);
}
There are a few more ways but those are the most common.
Upvotes: 1