flowers
flowers

Reputation: 173

Spawning and destroying things in AS3

Okay, so I am making a small game where the user picks randomly spawned flowers. When a flower is picked, it disappears and the user gets points.

Here is some of my code:

import flash.events.MouseEvent;

function makeFlower() {

    var flower:Flower = new Flower();

    addChild(flower);
    flower.x = Math.random() * 500;
    flower.y = Math.random() * 400;
}

function removeFlower(event:MouseEvent):void {
    flower.pick();
}


setInterval(makeFlower, 2500);
addEventListener(MouseEvent.CLICK, removeFlower);

So in the above code, flower.pick() doesn't work because it's out of scope (how would I get it in-scope, by the way?)... that is one problem. But this problem got me questioning where the creation of the flower should happen. Would it be better to put addChild() in my constructor method?

And then the destruction part... the event listener which detects the flower being clicked... should this be separate (like how I have it) or should it be put into the Flower class? I am new to ActionScript and would like to know where things should be put.

Thanks!

Upvotes: 0

Views: 1715

Answers (2)

shaunhusain
shaunhusain

Reputation: 19748

Where you should handle things depends on quite a few factors, number one on my list would be if you're using the Flex framework or not, number two would be what is best for a given application. For your case it appears you're just using Flash IDE.

You'll want to have a collection of the flower objects your adding to the display tree. Then on click you'll want to use the target of the click to know which "flower" they picked, instead of arbitrarily removing some flower.

The other good thing about having a collection of the flowers is that you can actually just toggle the visibility (this also toggles the ability for a touch/click to interact with an object). Then you can have the Flower objects all created at once or at least re-used instead of having to be garbage collected and making a ton more objects (as pointed out by Daniel below the term is object pooling).

import flash.events.MouseEvent;

var allFlowers:Array = [];
var oldFlowers:Array = [];

function makeFlower() {
    var flower:Flower;

    if(oldFlowers.length>0)  //if there's stale ones, already created but since picked
    {
        flower = oldFlowers.pop();//grab the last one from the list
        flower.visible=true;//show it, update to position below should happen fast enough so you won't see it move
    }
    else
        flower = new Flower();

    allFlowers.push(flower);

    addChild(flower);

    flower.addEventListener(MouseEvent.CLICK, removeFlower);  //Adding the listener directly to the Flower object, assuming it's a Sprite/MovieClip/InteractiveObject of some sort

    flower.x = Math.random() * 500;
    flower.y = Math.random() * 400;
}

function removeFlower(event:MouseEvent):void {
    var clickedFlower:Flower = (event.target as Flower); //this is a local reference
    clickedFlower.pick();  //assuming this toggles the visibility
    oldFlowers.push(allFlowers.splice(clickedFlower,1)); //removing it from the "active array" putting it in the "stale array"
}


setInterval(makeFlower, 2500);

Ideally this would all be wrapped up in a class, really scoping variables just makes way more sense to me in the context of a class than just dangling in some block of code somewhere.

Since in your original code you declared that flower was a var and gave it the type of object stored at that var (Flower), it's scope is limited to the opening and closing curly braces for that function. In the case of the variables I defined they should be available so long as the object that contains the code is available.

There are three primary "scope modifiers" you can use when declaring variables or functions in a class in AS3 (namely public, protected, or private). Using public means the property/method (member) is accessible within the class definition (generally the file, something.as) as well as by anything else, if you make a variable public it can be modified by anything that uses the code, this can lead to un-predictable behavior of your application if change from the outside is unintended. Using protected means anything that inherits from or extends a class can access/use the protected member, but nothing that just has an instance of the object can access these members (hence the term access modifier). The third level is private which means only code within that class (across all methods) can access the member but no inheriting/sub-classes of the class can access the member. This is essentially an implementation of the concept generally termed encapsulation in computer science. There's a lot more detail, but really it's already getting too long for a decent SO post.

Upvotes: 2

mgraph
mgraph

Reputation: 15338

try:

import flash.events.MouseEvent;

var flower:Flower;

function makeFlower() {
    flower = new Flower();
    addChild(flower);
    flower.x = Math.random() * 500;
    flower.y = Math.random() * 400;

    flower.addEventListener(MouseEvent.CLICK, removeFlower);
}

function removeFlower(event:MouseEvent):void {
    event.target.pick();
    //removeChild(event.target); // active this to remove the clicked flower
}

setInterval(makeFlower, 2500);

Upvotes: 2

Related Questions