Occasionally I will set myself a problem in a little practice animation in order to torture myself. In this case I have 15 squares (movieclips) on the stage and when I press a start button I want them to get larger and smaller in sequential order. In other words when the first one finishes growing and shrinking the next one follows and then the next one etc. This was an exercise in controlling several movieclips with an array. There is also the option of stopping and then resuming the animation and that is where I got into trouble. When I would resume the animation ALL of the previous movieclips were still held in the memory and they would ALL resume rather than just the one that had been stopped. I solved the problem by having whatever the current movieclip that was moving equal to null once it shrank. I’ll highlight that line of code below. So it all works perfectly now, but I am getting an error message (Cannot access a property or method of a null object reference.) when I hit the Stop or Resume buttons. It doesn’t effect what’s happening though. It works perfectly, but I am not comfortable living with the error code. 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;
var indexNumber: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();
}
}
}
btnStart.addEventListener(MouseEvent.CLICK,squareStart);
function squareMove():void {
var currentClip:MovieClip;
var increment:Number;
var clipGrow:Number=2;
var clipShrink:Number=-2;
btnStart.y=-35;
btnStop.y=13;
btnResume.y=-35;
currentClip=aSquares[index];
function squareGrow(evt2:Event):void {
increment=clipGrow;
currentClip.width+=increment;
currentClip.height+=increment;
if (currentClip.width>=100) {
currentClip.removeEventListener(Event.ENTER_FRAME,squareGrow);
currentClip.addEventListener(Event.ENTER_FRAME,squareShrink);
}
}
currentClip.addEventListener(Event.ENTER_FRAME,squareGrow);
function squareShrink(evt3:Event):void {
increment=clipShrink;
currentClip.width+=increment;
currentClip.height+=increment;
if (currentClip.width<=46) {
currentClip.removeEventListener(Event.ENTER_FRAME,squareShrink);
//below is the line tripping the error message
currentClip=null;
if (index<14) {
index++;
squareMove();
} else if (index==14) {
btnStart.y=13;
btnStop.y=-35;
btnResume.y=-35;
}
}
}
function stopSquare(evt4:Event):void {
//trace(currentClip.name);
btnStart.y=-35;
btnStop.y=-35;
btnResume.y=13;
if (increment==clipGrow) {
currentClip.removeEventListener(Event.ENTER_FRAME,squareGrow);
} else if (increment==clipShrink) {
currentClip.removeEventListener(Event.ENTER_FRAME,squareShrink);
}
}
btnStop.addEventListener(MouseEvent.CLICK,stopSquare);
function resumeSquare(evt5:Event):void {
btnStart.y=-35;
btnStop.y=13;
btnResume.y=-35;
if (increment==clipGrow) {
currentClip.addEventListener(Event.ENTER_FRAME,squareGrow);
} else if (increment==clipShrink) {
currentClip.addEventListener(Event.ENTER_FRAME,squareShrink);
}
}
btnResume.addEventListener(MouseEvent.CLICK,resumeSquare);
}