Cleaning Up Vector (array like) in pure AS3 project
I have been learning a game type animation from a tutorial at this site: http://www.emanueleferonato.com/2011/11/08/as3-version-of-nodes-engine-full-playable-game-with-infinite-solvable-levels/. Source code there online and also available for download.
Want to try to set up additional levels of difficulty, involving adding more draggables and targets. In order to change this level during a game, (with a set of radio buttons and change handler noting the change in radio button selection) using a pure AS3 and single frame necessitates removing (cleaning up) the draggables and targets already placed on stage in 2 Vector type arrays.
Have tried various versions of removeChild, removeChildAt with no success.
Here is the relevant AS3. Maybe someone could suggest a way to clean up/remove all Vector members before the new ones are instantiated on stage. If not removed, they end up at 0,0, one atop the other, while the new ones are randomly placed onto the stage.
Thank you for your help.
public class Main extends Sprite {
private const DRAGGABLES:Number=4;
private const TARGETS:Number=5;
private var draggableNode:DraggableNode;
private var target:Target;
private var laserCanvas:Sprite=new Sprite();
private var draggableVector:Vector.<DraggableNode>=new Vector.<DraggableNode>();
private var targetVector:Vector.<Target>=new Vector.<Target>();
private var isDragging:Boolean=false;
public function Main() {
addChild(laserCanvas);
placeDraggables(false);
placeTargets(false);
shuffleDraggables();
drawLaser();
stage.addEventListener(MouseEvent.MOUSE_MOVE,moving);
}
private function placeDraggables(justMove:Boolean):void {
for (var i:Number=0; i<DRAGGABLES; i++) {
if (! justMove) {
draggableNode=new DraggableNode();
addChild(draggableNode);
draggableVector.push(draggableNode);
draggableNode.addEventListener(MouseEvent.MOUSE_DOWN,drag);
draggableNode.addEventListener(MouseEvent.MOUSE_UP,dontDrag);
}
do {
var wellPlaced:Boolean=true;
draggableVector[i].x=Math.floor(Math.random()*600)+20;
draggableVector[i].y=Math.floor(Math.random()*440)+20;
for (var j:Number=i-1; j>=0; j--) {
if (getDistance(draggableVector[j],draggableVector[i])<150) {
wellPlaced=false;
}
}
} while (!wellPlaced);
}
}
private function placeTargets(justMove:Boolean):void {
for (var i:Number=0; i<TARGETS; i++) {
if (! justMove) {
target=new Target();
addChildAt(target,0);
targetVector.push(target);
target.alpha=0.5;
}
do {
var wellPlaced:Boolean=true;
var segment=Math.floor(Math.random()*DRAGGABLES);
var p1:Sprite=draggableVector[segment];
var p2:Sprite=draggableVector[(segment+1)%DRAGGABLES];
var angle:Number=Math.atan2((p2.y-p1.y),(p2.x-p1.x))
var targetDistance:Number=Math.random()*getDistance(p1,p2);
targetVector[i].x=p1.x+targetDistance*Math.cos(angle);
targetVector[i].y=p1.y+targetDistance*Math.sin(angle);
for (var j:Number=i-1; j>=0; j--) {
if (getDistance(targetVector[j],targetVector[i])<50) {
wellPlaced=false;
}
}
for (j=0; j<DRAGGABLES; j++) {
if (getDistance(draggableVector[j],targetVector[i])<50) {
wellPlaced=false;
}
}
} while (!wellPlaced);
}
}
}
