My Main class is added to the stage of my .fla and I want to remove and re-add/"restart" the class when it finishes animating. All of my animations are happening in Main and are added to the display tree inside Main. How can I run the finishNow() function from within Main.as?
The .fla file:
var run:Main = new Main(this);
stage.addChild(run);
function finishNow() {
stage.removeChild(run);
var run:Main = new Main(this);
stage.addChild(run);
}
The Main.as file:
var stageHolder:Object;
public function Main(stageHolderTemp) {
stageHolder = stageHolderTemp;
trace(stageHolder);
}
function callFinishFunction():void {
// how to call finishNow() function from .fla file here
}
EDIT: The design of the program has changed. Still trying to do the same thing (call finishNow() function – but it is now in Program.as). It all works fine, except throws an error for program.finishNow();:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
The .fla file:
It now does not contain any code. It is linked to Program.as.
The Program.as file:
package {
import flash.display.MovieClip;
public class Program extends MovieClip {
public function Program() {
startNow();
}
function startNow() {
var run:Main = new Main(this);
addChild(run);
}
function finishNow() {
removeChild(run);
var run:Main = new Main(this);
addChild(run);
}
}
}
The Main.as file:
package {
import flash.display.Sprite;
public class Main extends Sprite
{
var stageHolder:Object;
public var program:Program;
public function Main(stageHolderTemp) {
stageHolder = stageHolderTemp;
trace(stageHolder);
someFunctionsThatDrawGraphics();
}
function callFinishFunction():void {
// how to call finishNow() function from Program.as file here?
program.finishNow();
}
}
}
You can call addframeScript to call FLA function.
If your .fla linked to Main.as file, you can directly called. try this:
public function Main() {
finishNow();
}
.fla code
function testFunction()
{
trace("111");
}
.as file
package {
import flash.display.MovieClip;
public class Main extends MovieClip {
public function Main() {
testFunction();
}
}
}
Actually , no need to create the class object every time . It will be enough to call a function in that class every time .
But for your question ..
Try this ....
In program.as
var run:Main = new Main(this);
run.addEventListener("FINISH",finishNow);
addChild(run);
function finishNow(e:Event)
{
}
In main.as ,
function callFinishFunction():void
{
dispatchEvent(new Event("FINISH"));
}
Related
I have a .fla file names test.fla and I have this variable in it:
import Main;
var my_var;
stage.addEventListener(MouseEvent.CLICK, onLoaded);
function onLoaded(e:Event):void
{
my_var = "Maziar";
//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 "Actionscript 3 : pass a variable from the main fla to external as file", but it does not work for me!!!
I wrote in my 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;
public class Main extends Sprite
{
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);
}
...
}
}
Thanks in advance!
It's important to note that the constructor Main in your ActionScript document file is run before the code found within the frame. When you are attempting to access the my_var variable in your AS document it has not yet been declared in the frame.
So, we need to wait for Flash to run the frame. This can be done using an Event.ENTER_FRAME listener.
Example:
Timeline Code (.fla file)
var my_var:String = "my variable";
Document Code (.as file)
package {
import flash.display.MovieClip;
import flash.events.Event;
public class Main extends MovieClip {
public function Main() {
addEventListener(Event.ENTER_FRAME, waitForMyVar);
}
private function waitForMyVar(e:Event):void {
trace(my_var);
removeEventListener(Event.ENTER_FRAME, waitForMyVar);
}
}
As a side note, it appears my_var is not assigned a value until the user has clicked the stage. An adjustment could be made in the waitForMyVar function to wait for my_var to be non-null.
Example:
if(my_var != null) {
trace(my_var);
removeEventListener(Event.ENTER_FRAME, waitForMyVar);
}
Hope this helps!
Use static class members.
public class Main extends Sprite
{
static public var globalVar:* = 1;
public function doWhatever():void
{
trace(globalVar);
}
}
Then in FLA:
import Main;
var M:Main = new Main();
// or use sprite instance of Main
M.doWhatever();
Main.globalVar = "Hello World!";
M.doWhatever();
This is extremely basic, but to help me understand could someone please explain why this doesn't work. Trying to call a function from one as file to another, and get the following error.
Error: Error #2136: The SWF file file:///test/Main.swf contains invalid data.
at code::Main()[C:\Users\Luke\Desktop\test\code\Main.as:12]
Error opening URL 'file:///test/Main.swf'
Main.as
package code {
import flash.display.MovieClip;
import flash.events.*;
import code.Enemy;
public class Main extends MovieClip
{
public function Main()
{
var enemy:Enemy = new Enemy();
}
public function test():void
{
trace("Test");
}
}
}
Enemy.as
package code {
import flash.display.MovieClip;
import flash.events.*;
import code.Main;
public class Enemy extends Main {
public function Enemy() {
var main:Main = new Main();
main.test();
}
}
}
Assuming Main is your document class, you can't instantiate it. That might explain the SWF invalid data error.
What it looks like you are trying to do is access a function on Main from your Enemy. To do that you just need a reference to Main from inside your Enemy class. If you add the Enemy instance to the display list you can probably use root or parent to get a reference to Main. You could also pass a reference to Main through the constructor of your Enemy class:
public class Main {
public function Main() {
new Enemy(this);
}
public function test():void {
trace("test");
}
}
public class Enemy {
public function Enemy(main:Main) {
main.test();
}
}
From the constructor of the class Main you are creating the Object of Enemy. In the constructor of Enemy you are creating the Object of Main. Hence it continues to create those two objects until there is Stack overflow. It never reaches to the line where you have main.test();
if you wana get data frome main.as you can use the static var.
package {
import flash.display.MovieClip;
public class Main extends MovieClip {
// i well get this var in my Enemy as.
public var i:uint=1021;
public function txtuto() {
// constructor code
}
}
}`
// the Enemy.as
`package {
import flash.display.MovieClip;
public class Enemy extends MovieClip {
public static var tx:Main = new Main;
public function Enemy() {
trace(tx.i);
}
}
}
good luck.
So, I'm having this error with AS3. I have an object(movie clip) called Inimigo2_Caique2, and his is called Inimigo2_Caique2, too. (I don't know why, but the icon of the object in the library is green instead of blue).
When I try to run the file, I get this error message:
5006: An ActionScript file can not have more than one externally visible definition: Inimigo2_Caique2, removeListeners
I have other object called Inimigo_Caique2, and another class called Inimigo_Caique2. (The icon is green too.)
Here is the code of my Inimigo2_Caique2 class:
package{
import flash.events.Event;
import flash.display.MovieClip;
import flash.display.Sprite;
public class Inimigo2_Caique2 extends Sprite{
private var palco:Object;
private var yd:Number;
private var xd:Number;
public function Inimigo2_Caique2(){
addEventListener(Event.ADDED_TO_STAGE,inicia2);
}
private function inicia2(e:Event){
palco=MovieClip(root);
yd=palco.aviao.y-y;
xd=palco.aviao.x-x;
addEventListener(Event.ENTER_FRAME,loop1);
}
private function loop1(e:Event){
var angulo:Number=Math.atan2(yd,xd);
x+=Math.cos(angulo)*10;
y+=Math.sin(angulo)*10;
for(var i:int = 0; i<palco.recipiente.numChildren;i++){
var alvoBala2:Sprite = palco.recipiente.getChildAt(i);
var ris:Number=alvoBala2.y-y;
var run:Number=alvoBala2.x-x;
var dis:Number=Math.sqrt(Math.pow(ris,2)+Math.pow(run,2));
if(dis<100){
if(run<0){
x+=20;
}else{
x-=20;
}
}
}
if(hitTestObject(alvoBala2)){
palco.recipiente.getChildAt(i).removeListeners();
palco.recipiente.removeChild(alvoBala2);
palco.Som2.play();
var boom3:MovieClip = new explosao();
boom3.x=x;
boom3.y=y;
stage.addChild(boom3);
palco.pontos+=300;
var textopontos=String(palco.pontos);
palco.txt_pontos.text=textopontos;
removeEventListener(Event.ENTER_FRAME,loop1);
palco.removeChild(this);
}
}
if(hitTestObject(palco.aviao)){
palco.Som2.play();
var aviaoboom:MovieClip = new explosao();
aviaoboom.x=palco.aviao.x;
aviaoboom.y=palco.aviao.y;
stage.addChild(aviaoboom);
palco.gotoAndStop(2);
}
}
public function removeListeners():void{
removeEventListener(Event.ENTER_FRAME,loop1);
}
}
I don't know why this is happening. I've already checked the brackets and everything, but nothing works.
Thanks in advance. This error is driving me mad.
In the following case, the function myFunction is outside of the public class Main and interpreted as another class by the compiler:
My class
package
{
import flash.display.MovieClip;
public class Main extends MovieClip
{
}
//
public function myFunction()
{
}
//
}
My Fla
myFunction(); // function invoked
This error message occurs: 5006: An ActionScript file can not have more than one externally visible definition: Main, myFunction, because the correct code is:
My class
package
{
import flash.display.MovieClip;
public class Main extends MovieClip
{
public function myFunction()
{
trace('myFunction should be here');
}
}
}
So your error message indicates that your function removeListeners is outside of your class Inimigo2_Caique2
Remark
As MasterRoro says, this block seems to be outside your loop1 function:
if (hitTestObject(palco.aviao)) {
palco.Som2.play();
var aviaoboom:MovieClip = new explosao();
aviaoboom.x=palco.aviao.x;
aviaoboom.y=palco.aviao.y;
stage.addChild(aviaoboom);
palco.gotoAndStop(2);
}
it seems that your block:
if(hitTestObject(palco.aviao)){
palco.Som2.play();
var aviaoboom:MovieClip = new explosao();
aviaoboom.x=palco.aviao.x;
aviaoboom.y=palco.aviao.y;
stage.addChild(aviaoboom);
palco.gotoAndStop(2);
}
} //Extra curly brace
Is supposed to go inside of your loop1(e:Event) function. You terminated the loop1 function earlier than this with 2 curly braces, and then the extra curly brace in said block is terminating your class definition, which is why the compiler is throwing that error.
Try moving this block inside of the loop1() function and removing the extra curly brace at the end.
I'm working with as3 and I don't understand how to pass from a value on a function to a global variable. I have this code (in a .as file):
package {
public class loadInfo {
import flash.events.*;
import flash.net.*;
private var teamA:String;
public var urlLoader:URLLoader = new URLLoader( );
public function loadInfo() {
urlLoader.addEventListener(Event.COMPLETE, handleComplete);
urlLoader.dataFormat = URLLoaderDataFormat.VARIABLES;
urlLoader.load(new URLRequest("data.txt" ));
}
public function handleComplete( event:Event ):void {
this.teamA = urlLoader.data.teamA;
}
public function getTeamA():String{
return teamA;
}
}
}
What I'm doing with this code is to load several variables which are in a .txt file.
and in the .fla file I have:
import loadInfo;
var dat:loadInfo = new loadInfo();
trace(dat.getTeamA());
but the result is "null".
So, I have no clue what to do. Help is appreciated. Thanks.
The problem is that you don't wait for the loader to complete. It takes time to load that txt file and if you call getTeamA immediately, the loader isn't finished. You should do something like this:
var dat:loadInfo = new loadInfo();
dat.addEventListener(Event.COMPLETE, onDataLoaded);
function onDataLoaded(e:Event):void {
trace (dat.getTeamA());
}
And within loaderInfo:
public function handleComplete( event:Event ):void {
this.teamA = urlLoader.data.teamA;
dispatchEvent(new Event(Event.COMPLETE));
}
Should work properly. Keep in mind that loaderInfo must extend EventDispatcher (class loaderInfo extends EventDispatcher {)..
We have created a program in Flash that uses an external actionscript file.
I'm trying to load/import this swf in another flash file but I'm getting a 'Error #1009: Cannot access a property or method of a null object reference'
I know this is to a reference in my external .as file but I'm not sure how to fix this
This is my code to load (and scale) the swf
var ld:Loader = new Loader();
addChild(ld);
ld.load(new URLRequest("tatton.swf"));
ld.contentLoaderInfo.addEventListener(Event.COMPLETE,function(evt) {
var mc = evt.target.content;
mc.scaleX=0.7031;
mc.scaleY=0.7031;
});
Anyone have any ideas?
Thanks
Edit: here is the full error code: TypeError: Error #1009: Cannot access a property or method of a null object reference. at Tatton()
And below is my .as file code:
package
{
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.utils.setTimeout;
import flash.utils.clearTimeout;
import flash.utils.getDefinitionByName;
public class Tatton extends MovieClip
{
public var active_clip:MovieClip;
public var to:Number;
public function Tatton()
{
// Create empty vector of movieclips;
stage.addEventListener(MouseEvent.CLICK, reset_timeout);
init();
}
public function reset_timeout(e:Event = null)
{
clearTimeout(to);
// 3 mins
to = setTimeout(fire_timeout, 150 * 1000);
}
public function fire_timeout():void
{
// Reset the system (and run attractor) if we're not on the attractor already.
if ( !(active_clip is Attractor))
{
init();
}
}
public function init()
{
// Reset globals
Globals.skip_menu_anim = false;
Globals.skip_staff_menu = false;
load_movie("Attractor");
reset_timeout();
}
public function load_movie(name:String):void
{
if (active_clip is MovieClip)
{
kill_active_movie();
}
active_clip = createInstance(name);
addChild(active_clip);
active_clip.startup();
}
public function kill_active_movie():void
{
active_clip.shutdown();
removeChild(active_clip);
active_clip = null;
}
public function createInstance(className:String):Object
{
var myClass:Class = getDefinitionByName(className) as Class;
var instance:Object = new myClass();
return instance;
}
}
}
try this
public function Tatton() {
addEventListener(Event.ADDED_TO_STAGE, stageAvailable);
}
private function stageAvailable(e:Event = null) {
removeEventListener(Event.ADDED_TO_STAGE, stageAvailable);
// Create empty vector of movieclips;
stage.addEventListener(MouseEvent.CLICK, reset_timeout);
init();
}
refer to this article to understand why
You need to add the event listener after you call Loader.load because contentLoaderInfo is null until load is called.