I have created a custom class (the .as is below:). It exists throughout my main timeline in three instances (panel1, panel2, and panel3), and has only one keyframe at present.
When I debug my file, in Frame 1 the constructor function is called for each instance. Cool. However, a little later the constructor function is called AGAIN for each instance, wiping out any changes I have made.
The instances are all placed on the stage by dragging them from the library. There are no ” = new Scorepanel() lines in my scripts.
What triggers this and how can I prevent it?
–Ken (incredibly frustrated) Franklin
{
import flash.display.MovieClip;
import fl.text.TLFTextField;
public class scoreunit extends MovieClip
{
private var team:String = "WHOAMI";
private var points:Number = 0;
private var scorevalue:String;
private var teamname:String;
private var inited:Boolean = false;
function scoreunit()
{
if (! inited)
{
this.teamname = team;
this.scorevalue = String(points);
var teamnamebox=new TLFTextField();
teamnamebox.x = 2.25;
teamnamebox.y = 295.25;
teamnamebox.width = 295.25;
teamnamebox.height = 70;
teamnamebox.text = team;
var scorevaluebox=new TLFTextField();
scorevaluebox.x = 2.25;
scorevaluebox.y = 95.80;
scorevaluebox.width = 295;
scorevaluebox.height = 97.5;
scorevaluebox.text = scorevalue;
trace("I set the starting values");
trace("teamnamebox = ",teamnamebox.text);
trace("scorevalue = ",scorevaluebox.text);
inited = true;
}
}
public function score():Number
{
return points;
}
public function winpoints(n:Number)
{
points += n;
scorevalue = String(points);
scorevaluebox.text = scorevalue;
}
public function losepoints(n:Number)
{
points -= n;
scorevalue = String(points);
scorevaluebox.text = scorevalue;
}
public function setname(s:String)
{
team = s;
teamname = team;
teamnamebox.text = s;
trace("I changed the name to ",teamnamebox.text);
}
public function buzzin()
{
this.gotoAndStop("buzzedin");
}
public function makeddselect()
{
this.gotoAndStop("ddtarget");
}
public function makeyay(y:Number)
{
switch (y)
{
case 1 :
this.gotoAndStop("yay1");
break;
case 2 :
this.gotoAndStop("yay2");
break;
case 3 :
this.gotoAndStop("yay3");
break;
case 4 :
this.gotoAndStop("yay4");
break;
case 5 :
this.gotoAndStop("yay5");
break;
case 6 :
this.gotoAndStop("yay6");
break;
default :
trace("CRASH! "+y+" IS NOT A VALID YAY!");
}
}
public function makeboo(b:Number)
{
switch (b)
{
case 1 :
this.gotoAndStop("boo1");
break;
case 2 :
this.gotoAndStop("boo2");
break;
case 3 :
this.gotoAndStop("boo3");
break;
case 4 :
this.gotoAndStop("boo4");
break;
case 5 :
this.gotoAndStop("boo5");
break;
case 6 :
this.gotoAndStop("boo6");
break;
default :
trace("CRASH! "+b+" IS NOT A VALID BOO!");
}
}
public function makescrewing()
{
this.gotoAndStop("evilface");
}
public function makescrewed()
{
this.gotoAndStop("screwed");
}
public function makesplat()
{
this.gotoAndStop("eekface");
}
public function makefreeze()
{
this.gotoAndStop("frozen");
}
public function makehome()
{
this.gotoAndStop("inactive");
}
}
}
The problem you have here isn’t coming from your code, but from the timeline. The code from your class can only access the elements of the current displayed frame, and loose access to the others instances, symboles, anything which is outside the current frame.
Your problem is that flash goes through each frame a first time, highlighting in debug that each object is correctly built, but once it’ll reach the last frame on the timeline, it gets back to the very first frame, executing again everything from there, as any object present on frame is re-instancied again.
You need to avoid placing class instance on stage by hand.
Also but this is personnal practice, if you need to work with frames and code, prefer to work with single-frame clips, and addChild() icon libraries, assets, and gotoAndStop()’em from the code. you’ll have much more control at the end, and less frustration. But overall, if you’re coding, avoid using frame whenever possible 😉
Hope this helped !