thor625
thor625

Reputation: 87

how to add a limited amount of objects to the stage

So I have a box exported as Box in my library. I have tried :

package {

    import flash.display.MovieClip;
    import flash.events.*;

    public class Main extends MovieClip {

        private var _box:Box=new Box  ;
        private var boxAmount:Number=0;
        private var boxLimit:Number=16;


        private var _root:Object;
        public function Main() {

            addEventListener(Event.ENTER_FRAME, eFrame);

            addEventListener(MouseEvent.MOUSE_DOWN, mouseclick);
        }

        private function eFrame(event:Event):void {
            if (boxAmount <= boxLimit) {
                boxAmount++;
                _box.y=Math.random()*stage.stageHeight;

                _box.x=Math.random()*stage.stageWidth;

                addChild(_box);

            } else if (boxAmount >= boxLimit) {
                removeEventListener(Event.ENTER_FRAME, eFrame);
            } else {
                addEventListener(Event.ENTER_FRAME, eFrame);
            }
        }
    }
}

But it did not work as planned.

What I am trying to do is make my box stay on the screen at a random place on the stage and remove it when clicked (but that will come later). This code is for some reason adding the object to the stage and then removing it and adding it again up to 16 times.

Thanks

Upvotes: 0

Views: 154

Answers (4)

kapex
kapex

Reputation: 29999

Although the variable boxAmount suggests otherwise, you said you only want one box. So, to do this, you just need to move the following lines into the constructor (Main).

_box.y=Math.random()*stage.stageHeight;
_box.x=Math.random()*stage.stageWidth;
addChild(_box);

Then remove or disable the enter frame event. You don't need it in this case. To check if the box got clicked, attach the listener to the box itself, not to it's parent:

_box.addEventListener(MouseEvent.MOUSE_DOWN, mouseclick);

if (boxAmount<=boxLimit) {
    // ...
} else if (boxAmount >= boxLimit) {
    // ...
} else {
    // ...
}

This part looks really strange. The first condition covers a case that is also covered by the second condition, together they already cover all possible cases. boxAmount is either less or equals to boxLimits or it is greater than it. Checking for equality twice is confusing. There is no need to include the last else statement. It actually has the same behaviour as the following code.

if (boxAmount<=boxLimit) {
    // ...
} else if (boxAmount > boxLimit) {
    // ...
}

Upvotes: 0

shanethehat
shanethehat

Reputation: 15580

What you have at the moment is just repositioning the same box over and over because you only ever create one Box instance. You need to create multiple instances of Box and add them to the stage individually.

package {

    import flash.display.MovieClip;
    import flash.events.*;

    public class Main extends MovieClip {

        private var boxAmount:Number=0;
        private var boxLimit:Number=16;
        private var _root:Object;

        public function Main() {
            addEventListener(Event.ENTER_FRAME, eFrame);
            addEventListener(MouseEvent.MOUSE_DOWN, mouseclick);
        }

        private function eFrame(event:Event):void {
            if (boxAmount<=boxLimit) {
                boxAmount++;
                //create a new box instance
                var _box:Box = new Box();
                _box.y=Math.random()*stage.stageHeight;
                _box.x=Math.random()*stage.stageWidth;

                addChild(_box);
            } else {
                removeEventListener(Event.ENTER_FRAME, eFrame);
            } 
        }
    }
}

Upvotes: 0

Cadin
Cadin

Reputation: 4649

In your code you are only ever creating one box. Your enterFrame handler is just assigning it a new random position 16 times. If you want 16 boxes you'll need to create a new box each time in the enterFrame function.

But you don't need to use the ENTER_FRAME event here. You could just use a for loop or a while loop to create the 16 boxes.

Here's some code:

package {

import flash.display.MovieClip;
import flash.events.*;

public class Main extends MovieClip {

    private var boxAmount:Number=0;
    private var boxLimit:Number=16;

    public function Main() {
        addBoxes();
    }

    private function addBoxes():void {

        while (boxAmount<=boxLimit) {
            boxAmount++;

            var box:Box = new Box();
            box.y=Math.random()*stage.stageHeight;
            box.x=Math.random()*stage.stageWidth;

            addChild(box);

            // listen for mouse clicks
            box.addEventListener(MouseEvent.CLICK, onBoxClick);
        }
    }

    private function onBoxClick(e:MouseEvent):void {
        var clickedBox:Box = e.target as Box;
        removeChild(clickedBox);
    }

}
}

I removed your enterFrame handler and just made a function called addBoxes. I'm using a while loop to crate the boxes. Notice that each time through the loop I'm creating a NEW box, not just reusing the old one. I'm also adding a mouse click event listener to each box so it can be removed from the stage when clicked.

You'll surely want to change some of this to get it to work for your purposes, but it should get you headed in the right direction.

Upvotes: 1

Evert
Evert

Reputation: 8541

I it seems like you have created one _box, and re-add it to the timeline on enter frame. It should work if you create a new box instance inside the eFrame function rather than before it, then you keep reassigning to the same variable name, rather than reusing the one object eg:

package {

import flash.display.MovieClip;
import flash.events.*;

public class Main extends MovieClip {


    private var boxAmount:Number=0;
    private var boxLimit:Number=16;


    private var _root:Object;
    public function Main() {

        addEventListener(Event.ENTER_FRAME, eFrame);

        addEventListener(MouseEvent.MOUSE_DOWN, mouseclick);
    }

    private function eFrame(event:Event):void {
        if (boxAmount<=boxLimit) {
            boxAmount++;

            var _box:Box=new Box  ;

            _box.y=Math.random()*stage.stageHeight;

            _box.x=Math.random()*stage.stageWidth;

            addChild(_box);


        } else if (boxAmount >= boxLimit) {
            removeEventListener(Event.ENTER_FRAME, eFrame);
        } else {
            addEventListener(Event.ENTER_FRAME, eFrame);





        }
    }
}
}

Upvotes: 1

Related Questions