Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 03-22-2012, 09:13 PM   #1
agentx
Registered User
 
Join Date: May 2010
Posts: 11
Question Dealing with multiple external AS3 files

I am trying to use multiple actionscript files so that there is less clutter. However, I can only make an instance of movieclip from the main class. If I try to do it from other classes, it gives me an error. How can I fix this?

thanks
agentx is offline   Reply With Quote
Old 03-22-2012, 09:26 PM   #2
henke37
Senior Member
 
henke37's Avatar
 
Join Date: Mar 2009
Location: Sweden
Posts: 10,833
Send a message via Skype™ to henke37
Default

Not enough context to answer the question.
__________________
Signature: I wrote a pair of articles about the timeline.
henke37 is offline   Reply With Quote
Old 03-22-2012, 09:46 PM   #3
agentx
Registered User
 
Join Date: May 2010
Posts: 11
Default

Sorry I am trying get use to object oriented programming.
So I have my .fla file where I have my movieclips. I have two movieclips called "device_MC" and "photon_MC", which I am exporting. Right now I have two classes: main.as, and time.as.
ActionScript Code:
Main.as public function main (){     createDevice(); } public function createDevice():void{         var device:MovieClip = new device_MC();         addChild(device);         device.x = stage.stageWidth/2;         device.y = stage.stageHeight/2;         var photon_time:time = new time(); //this gives me error         addChild(photon_time);                 //this gives me error } time.as public function time () {         createPhoton();         } public function createPhoton():void{         var photon:MovieClip = new photon_MC();         addChild(photon);         photon.x = stage.stageWidth/2;         photon.y = stage.stageHeight/2; }
It shouldn't be giving me an error because I did import the time as well as the stage in the beginning of each file.
agentx is offline   Reply With Quote
Old 03-23-2012, 02:22 AM   #4
[afz]snickelfitz
Senior Member
 
[afz]snickelfitz's Avatar
 
Join Date: Dec 2011
Location: Tucson, AZ
Posts: 2,398
Default

The stage is not valid in the time.as constructor, in your posted code.
[afz]snickelfitz is offline   Reply With Quote
Old 03-23-2012, 07:49 PM   #5
agentx
Registered User
 
Join Date: May 2010
Posts: 11
Default

Ok. Thanks that works! I was wandering why is the stage invalid even if I imported it?
agentx is offline   Reply With Quote
Old 03-23-2012, 09:11 PM   #6
henke37
Senior Member
 
henke37's Avatar
 
Join Date: Mar 2009
Location: Sweden
Posts: 10,833
Send a message via Skype™ to henke37
Default

The stage is not invalid. The stage property simply reflects the fact that the object is or is not on the display tree of the stage.
__________________
Signature: I wrote a pair of articles about the timeline.
henke37 is offline   Reply With Quote
Old 03-24-2012, 08:42 PM   #7
agentx
Registered User
 
Join Date: May 2010
Posts: 11
Default

Sorry I am still a little bit confused. For the moment, the stage only works with main.as, but not with other files. How do I include stage in time.as without it giving me any errors? I really want to use some of the properties dealing with Stage in time.as.
thank you so much again!

Last edited by agentx; 03-24-2012 at 08:42 PM. Reason: typo
agentx is offline   Reply With Quote
Old 03-24-2012, 10:57 PM   #8
[afz]snickelfitz
Senior Member
 
[afz]snickelfitz's Avatar
 
Join Date: Dec 2011
Location: Tucson, AZ
Posts: 2,398
Default

You cannot add the stage to your Class instance.
You add your class instance to the stage. Once that has occurred, the code in your class file can make references to the stage.

The standard way to do this is to listen for Event.ADDED_TO_STAGE in the external class or swf constructor.
In the Event handler, you can make the references to the stage without errors.
ActionScript Code:
package {     import flash.display.*;     import flash.events.*;     public class ChildClass extends Sprite     {         public function ChildClass()         {             if (stage)             {                 init(null);             }             else             {                 addEventListener(Event.ADDED_TO_STAGE, init);             }         }         private function init(e:Event):void         {             trace(stage);         }     } }
[afz]snickelfitz is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 05:00 AM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.