Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Cleaning Up Vector (array like) in pure AS3 project

Contributor ,
Apr 03, 2014 Apr 03, 2014

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

            }

        }

}


TOPICS
ActionScript
2.0K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Apr 03, 2014 Apr 03, 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);

}

}                 

Translate
Community Expert ,
Apr 03, 2014 Apr 03, 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Apr 03, 2014 Apr 03, 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 03, 2014 Apr 03, 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);

}

}                 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Apr 03, 2014 Apr 03, 2014

Thank you kglad,

I'll try this out.

I very much appreciate your help.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 03, 2014 Apr 03, 2014

you're welcome.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Apr 03, 2014 Apr 03, 2014

OK, tried it. The function works perfectly to remove the draggableVector and targetVector items.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 03, 2014 Apr 03, 2014

great!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Apr 03, 2014 Apr 03, 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Apr 03, 2014 Apr 03, 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,

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Apr 04, 2014 Apr 04, 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Apr 04, 2014 Apr 04, 2014

Thank you again for your helpful feedback and suggestions. I still have a lot to learn and appreciate hearing about "best practices."

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 04, 2014 Apr 04, 2014

about coding, this is an excerpt from a book i wrote (Flash Game Development: In a Social, Mobile and 3D World),

it's taken from a chapter that shows how coding typically evolves for a novice with a main idea and then progresses as more and more features are added:

Everything works the way it should but there is a major problem. Actually, there is more than one major problem and there are some minor problems but I will address the biggest problem, first: my Main class is quickly becoming a mess.

I have all this code for controlling player in Main and I will need more code which will make Main an even bigger mess. In addition, I will need to add an introduction view (by view, I mean what is presented on-stage) to give some information about the game and I should probably allow users to customize the keys they use for movement. Not everyone likes using the arrow keys. (Heck, I do not like using the arrow keys.) In addition, I will need a game-over view, eventually.

I am heading for a major mess in Main. So, while I can continue to put all my coding in Main, that is just like putting all your code in frame 1 of the main timeline and that bypasses one of the biggest advantages of OOP, encapsulation.

By encapsulation, I mean each class should contain only the information it needs to do its job. I want my Main class to control which views are presented on-stage. When the game starts, Main will add an intro view. When the user is finished with the intro view, Main will remove the intro view and add the game view (where the player and enemy tanks will fight) and when the game is over, Main will remove the game view and add the game over view.

I will use an IntroView class to control what happens in the introduction view and I'll use a GameView class to monitor what happens when player and enemy fight and an EndView to control what is displayed when combat is over - maybe the user's score or a taunting message.

I do not think I want the GameView class to control the fighting. I want all the code that controls player to be in the PlayerTank class and all the code that controls enemy to be in the EnemyTank class. That will simplify Main so it is easier to work with, will provide better encapsulation and will make it easier and faster to find the code I need to edit when changes and additions are needed.

While I am simplifying my code, I am going to create a keyboard controls class (KBcontrols) to store the key codes for moving left, right, up and down. I'm going to use static methods so I can assign those key codes in IntroView and use them in PlayerTank.

I do not want to be forced to pass those key codes back-and-forth because I am going to have one PlayerTank instance in IntroView (so users can test the keyboard controls) and a different PlayerTank instance in GameView. In addition, I may later want to allow users to re-define the keyboard controls while in GameView or elsewhere. I could make KBcontrols a singleton class but I feel like making it a class with static properties for no special reason.

(Actually, I already used a singleton class in Chapter 4 for a shape-based hit test that I showed in the BitmapData section. So, showing the other way to make data easily available among several classes is reasonable.)

There are quite a few things I am thinking about adding (like scoring and a timer and enemy intelligence etc) and I do not know exactly what classes I'm going to have at the end of this project and I sure do not know all the code that will be in each class that I anticipate will be needed. That may be a little unsettling but it is how applications and games evolve (at least, when I am the developer).

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Apr 04, 2014 Apr 04, 2014

I'd highly recommend the book Actionscript 3 Design Patterns. To be honest, I didn't understand everything in it when I first read it, but having all that "stuff" rattling around in the back of my brain helped give me spots to put new information as I learned it. There's also a lot of good information on the site (as3dp.com), but I get malware alerts when I go there so visit at your own risk.

There may be better books now, such as Head First Design Patterns. I'm not sure, I haven't read it.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Apr 04, 2014 Apr 04, 2014

Thank you, kglad and Amy, for these helpful resources.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Apr 04, 2014 Apr 04, 2014

One thing that you should be a little careful about is that Singletons and static Classes are considered to be anti-patterns among many developers. This is especially true if you aspire to be a software architect, where you'll need to do things that simply can't be done if you use these anti-patterns, like Test Driven Development.

The alternative is to learn how to pass a reference to an instance into the piece of code that needs it instead of having the code reach out where it's been made available to everybody or to use events for communication, avoiding the need for pieces of code to have references to each other alltogether.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Apr 04, 2014 Apr 04, 2014
LATEST

Thank you, Amy.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 04, 2014 Apr 04, 2014

you're welcome.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines