Solved

Class constructor triggred multiple times

Posted on 2009-05-12
12
318 Views
Last Modified: 2013-11-11
Hi,

I have this class called test, which is linked to a symbol which is a movieclip.

when the movieclip appears in the first frame, the constructor gets called, when gotoAndPlay to another frame, the constructor is triggered again, as if it is a new instance.

I made sure it is the same instance name across frames.

I tried moving the movieclip across frames with and without keyframes but it still gets triggered multiple times.

I also tried without a class, I put the trace inside an onload handler inside the symbol and it too got triggred mutliple times.


what is going on ? how can I keep it down to one instance of the class/code/event handler.


thanks
class test extends MovieClip

{		

	function test()

	{

		super ();

		

		trace (">> new instance, name: " + this._name);

	}

	

}

Open in new window

0
Comment
Question by:moshem
  • 6
  • 5
12 Comments
 
LVL 22

Expert Comment

by:rascalpants
ID: 24369805
please post your framecode...

you ARE trying to make a new instance of your class, if the contruct gets triggered again.


rp / ZA
0
 
LVL 1

Author Comment

by:moshem
ID: 24393590
there was no code in the frame, just a new keyframe..

as it turns out,, each time flash moves to the next frame within root and the movieclip that has the class on it exists on that frame - it creates a new instance instead of using the previous one.

when I made root one frame long... and did all the extra stuff in child movieclips (other frames), it did not exhibit this behavior...
0
 
LVL 39

Expert Comment

by:blue-genie
ID: 24395986
it's also a case of how you're implementing your object.
u're using AS2 syntax so i'm assuming you're doing AS2 class - no problems here with multiple instantiation.
0
 
LVL 22

Expert Comment

by:rascalpants
ID: 24396234

Im a bit confused...  are you saying that your movieclip is in one layer extended across multiple frames and it still creates a new instance on each frame...

or

are you saying, you had a movieclip that was broken up into multiple frames, and it created a new instance with each frame?


because the first thing should not happen because the playhead only hits the movieclip once...  where are if the layer is split into multiple keyframes, then yes you would be firing off a new instance for each time the movieclip itself was instanciated.

what you did, by putting the code all in one frame was a good idea...  

a better idea would be to pull ALL of the code out of the SWF, and associate the main class by using the properties in the Library.


rp / ZA



 
0
 
LVL 1

Author Comment

by:moshem
ID: 24397780
well, I did not build the layers, the designer did, but I guess it's the latter since it did create a new instance, and BTW, the code was in an external AS file (AS 2) and associated by the library.

I wonder what the designer should have done to prevent this from happening..
0
 
LVL 22

Expert Comment

by:rascalpants
ID: 24398655

They should have put all of their interface elements (movieclips, buttons, textfields, etc.) in one master movieclip, and then associated that clip with a class in the library.

the code can be external of the FLA/SWF and still be run multiple times, if the movieclip is on multiple frames...


FYI... just in case you didn't already know this... (not to assume anything) an external AS file is not the same as an external Class file that has an extension of ".as".

Again, I might be stating the obvious to you, but I am not aware of your skill level in actionscript.


so what is the status of this question?


rp / ZA
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 1

Author Comment

by:moshem
ID: 24399454
rascalpants, thanks for your reply...

I have 20 years of experience in software but very little in actionscript.

I don't know the difference between external AS and a class....please explain..

why is it necessary  to put everything in one movieclip ? why can't the root timeline contain several frames?

I will gladly accept your answer as the solution.
0
 
LVL 22

Accepted Solution

by:
rascalpants earned 500 total points
ID: 24399701

yes, the Flash IDE is quite hard for traditional programmers to grasp...  it is a bit counter intuitive, but it has to be so that Designers can use the software like a graphic design tool.

You can think of it this way...  each time a frame is played, it runs the code in it...  so if there are multiple movieclips or framescript that call the same class or function, it will run each time that frame is played.  so imagine a SWF that plays a frame or set of frames over and over again... that would be the equivalent to running a for/loop with the class init code in it...  hence the reason why your code keeps making new instances of a class.

as for the "external AS file" VS a Class...  an external AS file that uses include FileName.as;  is the equivalent to a server-side or a JS include...  it is the same as the code running from that exact point in the frame...

whereas a Class file (also has an .as extension) needs to be imported... and the filename needs to match the contructor method name as well... like this...

SendEmail.as

// code

class SendEmail
{

   public function SendEmail()
   {
      // you are in the construct
   }

}



building out all of your visual assets and giving them instance names, and then associating a class with them in the library is the best way to separate code from visual interface.

basically, a movieclip is a Class... and you can assign it to a custom class of your own, as a starting point for all... a base class if you will.   when the SWF first runs, by default the class construct gets run.  so there is no need to write any code in your FLA that instantiates the class.  It just happens.

BUT, you CAN have a timeline have all sorts of code and movieclips all over the place, but it tends to be a complete mess.  Kind of like having inline CSS styles in the HTML...  not a good idea.  dont do it unless you have to.


well, i hope i have shed some light on Actionscript, and also on the best way to implement it within a project.  There are other ways to import Classes in the frames, and call classes that way, but then you can loose scope on valuable programming like dispatched events/listeners that bubble up through the interface elements.


rp / ZA





0
 
LVL 1

Author Comment

by:moshem
ID: 24401499
thank you!


I still don't get why flash creates a new instance of the class if the movieclip spans across keyframes in the root timeline, and does not create ones if the same frames are put in a movieclip inside one and only frame in the root timeline
0
 
LVL 22

Expert Comment

by:rascalpants
ID: 24401521

no no...  I was saying that if a movieclip spans frames it is only called once...  but if the frame that has the code is run again, it will create a new instance...  such as if you don't have a stop() at the end of the frames... it loops around to frame again.

rp / ZA
0
 
LVL 1

Author Comment

by:moshem
ID: 24401575
I see.

we did have _root.gotoAndPlay (1) in the code....

meaning we can't use root for "base" and "error messages" for example, and then return to frame 1..

what if this same thing is done in an inner movieclip ? will it behave the same ?
0
 
LVL 22

Expert Comment

by:rascalpants
ID: 24404011
that would make it start all over again...  ;)

in a SWF the AS 2 reference to _root, means the main timeline, so yes... it would all the main timeline and start it over...


rp / ZA
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

This is intended to introduce all collision detection principles in flash, their strengths, weaknesses and workarounds. The main method for Collision Detection in flash is using hitTestObject. But unless you'll be pushing rectangular shapes without …
I know the transition can be hard. We got used to the ease of use ActionScript 2 had, but honestly, it became problematic later on, especially if designers were involved in the project and found it easy to add code as they saw fit. So, this artic…
The goal of the tutorial is to teach the user how to how to record live broadcast.
The goal of the tutorial is to teach the user how to select which audio input to use. Once you have an audio input plugged into the laptop or computer, you will go into the audio input settings and choose which audio input you want to use.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now