Copy link to clipboard
Copied
I have a .fla file names test.fla and I have this variable in it:
var my_var;
stage.addEventListener(MouseEvent.CLICK, onLoaded);
function onLoaded(e:Event):void
{
my_var = "myVariable";
//trace(my_var);
}
I have a .as file called Main.as.
I want to pass **my_var** from test.fla to the Main.as.
I will really appreciate, if you can help me in this matter!
It is noticeable that I have used the method mentioned in "http://stackoverflow.com/questions/9603049/actionscript-3-pass-a-variable-from-the-main-fla-to-exter...", but it does not work for me!!!
I wrote in my Main.as:
package
{
public class Main extends Sprite
{
public function Main()
{
trace(root.my_var);
}
}
}
Thanks in advance!
make Main your document class and have it extend the MovieClip class.
Copy link to clipboard
Copied
kglad it is here!
Plz help me!
Copy link to clipboard
Copied
make Main your document class and have it extend the MovieClip class.
Copy link to clipboard
Copied
kglad Main is already document class and I extend to MovieClip class.
the code is like this:
test.fls:
import Main;
var my_var;
stage.addEventListener(MouseEvent.CLICK, onLoaded);
function onLoaded(e:Event):void
{
my_var = "Maziar";
//trace(my_var);
}
Main.as
package
{
import flash.display.Sprite;
import flash.geom.Point;
import flash.events.MouseEvent;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.events.Event;
import flash.display.MovieClip;
public class Main extends MovieCLip
{
public function Main()
{
if (stage)
{
init();
}
else
{
addEventListener(Event.ADDED_TO_STAGE, init);
}
addEventListener(Event.ENTER_FRAME, waitForMyVar);
}
private function waitForMyVar(e:Event):void
{
if (my_var != null)
{
trace(my_var);
removeEventListener(Event.ENTER_FRAME, waitForMyVar);
}
}
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
}
...
}
}
I still get the errors "error: 1120: Access of undefined property my_var".
the error is in line: if (my_var != null) and line : trace(my_var);
Copy link to clipboard
Copied
that couldn't have been your document class or you would have seen an error.
anyway, use:
maziarser wrote:
kglad Main is already document class and I extend to MovieClip class.
the code is like this:
test.fls:
import Main;
var my_var;
stage.addEventListener(MouseEvent.CLICK, onLoaded);
function onLoaded(e:Event):void
{
my_var = "Maziar";
dispatchEvent(new Event('varReadyE'));
}
Main.as
package
{
import flash.display.Sprite;
import flash.geom.Point;
import flash.events.MouseEvent;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.events.Event;
import flash.display.MovieClip;
public class Main extends MovieCLip
{
public function Main()
{
if (stage)
{
init();
}
else
{
addEventListener(Event.ADDED_TO_STAGE, init);
}
this.addEventListener('varReadyE',waitForMyVar);}
}
private function waitForMyVar(e:Event):void
{
trace(my_var);
}
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
}
...
}
}
Copy link to clipboard
Copied
Thanks again!
But I have still this error:
1120: Access of undefined property my_var. |
In line:
trace(my_var);
In Main.as
I have changed trace(my_var) to trace ("Hello") and it does not work also.
Copy link to clipboard
Copied
attach a screenshot of your properties panel that shows Main is your document class: