I’ve included the code I made for a little exercise in arrays that I found extremely useful in messing around with AS2. I couldn’t upload the .fla because it’s too large, but maybe somebody can see how my thinking is flawed by the construction of the code.
What should happen is that when the start button is pushed each square grows and shrinks in sequence. When the stop button is pushed the current square stops. When the resume button is pushed the current square resumes its motion and so on. I was able to get all this to work just fine in AS2, but AS3 is a different story.
When I tried to duplicate it in AS3 I only got so far. Everything works fine until I try stop the motion and then try to restart it with the Resume button. At that point I can’t really even tell what’s happening but multiple squares start growing and shrinking in a very confusing fashion.
Code below:
var aSquares:Array=new Array(mcSquare0,mcSquare1,mcSquare2,mcSquare3,mcSquare4,mcSquare5,mcSquare6,mcSquare7,mcSquare8,mcSquare9,mcSquare10,mcSquare11,mcSquare12,mcSquare13,mcSquare14);
var index:Number;
function squareStart(evt:Event):void {
for (var i:Number=0; i<aSquares.length; i++) {
if (["mcSquare"+i]==aSquares*.name) {
i=index;
index=0;
squareMove();
}
}
function squareMove() {
var currentIndex:Number=0;
var target=aSquares[index];
var increment:Number=5;
function squareGrow(evt:Event):void {
trace(index);
btnStart.y=-35;
btnStop.y=13;
btnResume.y=-35;
btnStop2.y=-35;
btnResume2.y=-35;
target.width+=increment;
target.height+=increment;
function squareStop(evt:Event):void {
btnStart.y=-35;
btnStop.y=-35;
btnResume.y=13;
btnStop.y=-35;
btnResume2.y=-35;
target.removeEventListener(Event.ENTER_FRAME,squareGrow);
function squareResume(evt:Event):void {
btnStart.y=-35;
btnStop.y=13;
btnResume.y=-35;
target.addEventListener(Event.ENTER_FRAME,squareGrow);
}
btnResume.addEventListener(MouseEvent.CLICK,squareResume);
}
btnStop.addEventListener(MouseEvent.CLICK,squareStop);
if (target.width>=100) {
target.removeEventListener(Event.ENTER_FRAME,squareGrow);
target.addEventListener(Event.ENTER_FRAME,squareShrink);
}
}
target.addEventListener(Event.ENTER_FRAME,squareGrow);
function squareShrink(evt:Event):void {
btnStart.y=-35;
btnStop.y=-35;
btnResume.y=-35;
btnStop2.y=13;
btnResume2.y=-35;
trace("it works");
target.width-=increment;
target.height-=increment;
function squareStop2(evt:Event):void {
btnStart.y=-35;
btnStop2.y=-35;
btnResume2.y=13;
target.removeEventListener(Event.ENTER_FRAME,squareShrink);
function squareResume2(evt:Event):void {
btnStart.y=-35;
btnStop2.y=13;
btnResume2.y=-35;
target.addEventListener(Event.ENTER_FRAME,squareShrink);
}
btnResume2.addEventListener(MouseEvent.CLICK,squareResume2);
}
btnStop2.addEventListener(MouseEvent.CLICK,squareStop2);
if (target.width<=46) {
target.removeEventListener(Event.ENTER_FRAME,squareShrink);
if (index<14) {
index++;
squareMove();
//target.addEventListener(Event.ENTER_FRAME,squareGrow);
} else if (index==14) {
btnStart.y=13;
btnStop.y=-35;
btnResume.y=-35;
btnStop2.y=-35;
btnResume2.y=-35;
}
}
}
}
}
btnStart.addEventListener(MouseEvent.CLICK,squareStart);