Skip to main content
saratogacoach
Inspiring
April 3, 2014
Answered

Cleaning Up Vector (array like) in pure AS3 project

  • April 3, 2014
  • 2 replies
  • 2320 views

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);

            }

        }

}


This topic has been closed for replies.
Correct answer kglad

use:

function removeAllF():void{

for (var j:int=draggableVector.length-1;j>=0;j--) {

draggableVector.parent.removeChild(draggableVector);

// remove listeners, if any

draggableVector.splice(j,1);

}

}                 

2 replies

Amy Blankenship
Legend
April 3, 2014

Why do you have to do everything in Main? Why couldn't you have different level Views, each with its own Vector(s), then toss the dirty/old one when it's no longer useful, replacing it with a clean one?

Note that it's not possible to see anything in your code that would let us help you, since you don't seem to be deleting values from your vector anywhere, much less calling removeChild(). You also didn't include any comments that would help us figure out what it is you're hoping to accomplish at various parts of your code and what is happening instead.

saratogacoach
Inspiring
April 3, 2014

Amy,

All of the source code is at the online site noted above. Plus a download of the FLA and AS is also there.

Why pure AS3? While I use Flash professional, my preference is to learn pure AS3 scripting as well. (You're right: would not be difficult to set up in frames to solve this. But pure AS3 is a challenge IMHO worth learning.) Thanks for your thoughtful comments.

Best Wishes,

Amy Blankenship
Legend
April 4, 2014

Um, I'm not sure I said anything against pure AS3. Also, there's nothing to preclude using Classes with the timeline, which is as valid a use of AS3 as others.

What I was saying is write an AS3 Class that encapsulates what you're doing with the Vectors, then thow away the dirty instance when you need a new level and replace it with a new one. That way your Main Class only has the responsibility of adding and removing that single child and only has to deal with swapping out levels.

Tutorials are a starting point, and honestly most of them lead you into bad habits . Keeping all of your code in a single Class is a bad habit. Dividing responsibilities among smaller Classes is a good habit.

kglad
Community Expert
Community Expert
April 3, 2014

loop through your array backwards, use removeChild (and remove listeners) to ready the vector's reference for gc, and use the splice() method to remove the reference from the vector.

saratogacoach
Inspiring
April 3, 2014

Hi kglad,

Thank you for this. Still limited in my scripting, may take some time to sort this, come up with a script to do this. Will try.

kglad
Community Expert
kgladCommunity ExpertCorrect answer
Community Expert
April 3, 2014

use:

function removeAllF():void{

for (var j:int=draggableVector.length-1;j>=0;j--) {

draggableVector.parent.removeChild(draggableVector);

// remove listeners, if any

draggableVector.splice(j,1);

}

}