View Full Version : How to make an AS3 package to call a function in main timeline
glidealong
January 29th, 2007, 03:23 AM
I am working on AS3 and I have a created a button class which have an off state and on state. I did the class and it works fine, but i need a little fine tuning. Right now I am polling the button state from the root(main timeline) by checking a boolean property in the button class. I feel it is quite inefficient to check the button state on every enterFrame event of the main timeline. Rather I would prefer the button to tell the root time line by invoking a function written in the root, whenever a state change occurs.
It should be cleaner , right??
But when I try to access a function in the root which goes like this
function setVertical(button_name:String):void{
trace("value of button is "+button_name);
}
from the button object, I get the following error
ReferenceError: Error #1069: Property setVertical not found on flash.display.Stage and there is no default value.
at ICT::genBtn/ICT:genBtn::btnClick()
:sigh:
Thanks in Advance
glidealong
January 29th, 2007, 04:33 AM
or is it that I have to make another class to hold all the buttons and define a public function in that so that the button class can initiate the function call to the new class which does the changes on the stage??
Dazzer
January 29th, 2007, 07:06 AM
in the realm of AS3, i believe they do discourage the use of global functions and properties. That could be just me though.
sorry I can't help you otherwise.
glidealong
January 29th, 2007, 07:20 AM
yeah .. they don't have any globals anymore. but i figured it out at last, with some searching on the adobe website and the AS3.0 cookbook i was able to find the light at the end of the tunnel.
Thanks to all..
Dazzer
January 29th, 2007, 07:31 AM
mind telling me, just in case I need to know in the future hehe
senocular
January 29th, 2007, 08:49 AM
You should have the button use dispatchEvent() to broadcast a change in states. Then your main timeline can use addEventListener on that button to listen for the event and react upon a state change rather than polling for it constantly
glidealong
January 30th, 2007, 03:47 AM
You should have the button use dispatchEvent() to broadcast a change in states. Then your main timeline can use addEventListener on that button to listen for the event and react upon a state change rather than polling for it constantly
I should have known that dispatchEvent() is supposed to be used in situations like this. Actually I did it in a different way. It's like this,
I created a document class which defines the root of the document. So when the swf starts to play the root will be defined as _root. The class I used goes like this
package ICT.Root
{
import flash.display.MovieClip;
public class Root extends MovieClip
{
public static var _root:MovieClip;
public function Root ()
{
_root = this;
}
}
}
Then I imported this root class into all the other classes in the package and created a movie clip instance _root with value set to the Root object. Now I can call the functions written in various objects on the stage..
Could anybody give me a comparison on this approach and an eventDispatcher??
Thanks to all
glidealong
January 30th, 2007, 03:51 AM
and anybody know if creating an array of objects is possible in AS3?
And if yes, I would really like to know how..
Thanks in advance
senocular
January 30th, 2007, 10:19 AM
... just like AS2
elmax
May 28th, 2007, 04:35 AM
i´m new to as3 and i would like to call timeline functions from within a class.
Could you show me an example of the following line:
Then I imported this root class into all the other classes in the package and created a movie clip instance _root with value set to the Root object. Now I can call the functions written in various objects on the stage..
THX,
elmax
glidealong
June 18th, 2007, 06:47 AM
Check the attachment
Jaspios
August 30th, 2008, 05:40 PM
Sorry to bring back an old thread, but I am looking for a solution for this as well.
I played with glidealong's file above. Looks good first, but I realized that it just had the strict mode off. As soon as I turn it back on, you get all several errors and it doesn't seem to have solved the initial problem.
Does anyone have other sample files?
Thanks.
Pier25
August 31st, 2008, 07:27 AM
Maybe you can pass a scope parameter to the class
//in the timeline code
var my_object = new my_class(this)
//in the class
class my_class(scope){
scope.my_function();
}
I don't know if that works... but have no time to check either...
Jaspios
September 2nd, 2008, 07:58 PM
Thanks, but I don't exactly know how I should use it... :(
But I found Senocular's older tip in this forum to use TopLevel class and somehow made it work. I'm a novice and don't understand how and why it works, but it's good enough for now :)
chiqui
October 26th, 2009, 01:53 PM
Since i had trouble importing a whole new class to my site to solve this last time problem (i really hate clients) i found a faster approche like this:
In my RootClass I named a public function CalledFromTimeline(). Then in timeline or class used this code:
import RootClass; //Import the cusom root class
var myRoot:RootClass = RootClass(this.root); //make the display object my custom one
myRoot.CalledFromTimeline(); //works!
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.