Adding 1 object randomly to the screen?

Up vote 0 down vote favorite share g+ share fb share tw.

On my previous post Adding a object randomly on the screen in as3 I explained the specifics of my situation. But I will go over it again. I have a box with a class(not my document class.

I do have one called Main but this one is just an AS class referencing my box. ) The classes name is Box and my MC box is exported as Box. This is the code this is in my main file on the main timline addEventListener(Event.

ENTER_FRAME, createbox); var _box:Box = new Box; var boxlimit:int = 2; function createbox (event:Event):void{ _box = new Box; _box. X = Math.random()*stage. StageWidth ; _box.

Y = Math.random()*stage. StageHeight; addChild(_box); } This is my Box class //package { // import flash.display. MovieClip; // import flash.events.

Event; // import flash.events. MouseEvent; // // public class Main extends MovieClip { // // public function Main() { // createBox(); // // } // // private function createBox():void { // // trace(Math.random()*stage. StageWidth) // _box.

X = Math.random()*stage. StageWidth ; // _box. Y = Math.random()*stage.

StageHeight; // stage. AddChild(_box); // // } // } //} This was actualy what was on the class before I tried what was above but I would rather keep all the code in the class. Any suggestions?

Flash actionscript-3 actionscript flash-cs4 link|improve this question asked Dec 20 '11 at 21:46Thor625254 57% accept rate.

Just to be sure, you want to create a class that will add a new instance of your Box library item randomly on the Stage. So you'll just have to call new Box() for it to work, right? – rvmook Dec 20 '11 at 22:02.

Package { import flash.events. Event; import flash.display. MovieClip; public class Main extends MovieClip { private var _box:Box; public function Main() { addEventListener(Event.

ENTER_FRAME, createbox); } private function createbox (event:Event):void { _box= new Box(); _box. X = Math.random()*stage. StageWidth ; _box.

Y = Math.random()*stage. StageHeight; addChild(_box); } } }.

Here's one way to go about it: When you set the class name for your MovieClip (export as actionscript class) you have the option to specify a base class for the clip. You can add your random position code in this base class like so: public class BoxBase extends MovieClip { public function BoxBase() { super(); addEventListener(Event. ADDED_TO_STAGE, _onStaged); } public function _onStaged(event:Event):void { this.

X = Math.random()*stage. StageWidth; this. Y = Math.random()*stage.

StageHeight; } } Note the ADDED_TO_STAGE event listener. Now that the code is inside your MC, you have to wait until it's been added to the display list (placed on the stage or as a child of a clip on the stage) before you can reference the "stage" variable. Once you've set BoxBase to be the base class for Box, you can create a new instance of Box at a random position by placing the following code in your document class: var b:Box = new Box(); addChild(b).

Here you have the opposite situation as in your previous question. You declare a Box variable outside the createBox() function , so redeclaring the variable inside the function will not create more boxes. //It should be something like this, without the // need to declare the _box variable outside the // function.

// As dugulous points out you have to make sure that // stage is not null before calling this function createbox (event:Event):void { if( stage! = null ) { var _box = new Box; _box. X = Math.random()*stage.

StageWidth ; _box. Y = Math.random()*stage. StageHeight; addChild(_box); } } In your example , you use an EnterFrame event to add your boxes... How many do you need?

A Timer would give you more control over the number of boxes and it's also easier to stop. Var delay:int = 500; // in milliseconds var numBoxes:int = 2000; // you could add an optional limit var timer:Timer = new Timer( delay , numBoxes ); // var timer:Timer = new Timer( delay ); would work too timer. AddEventListener( TimerEvent.

TIMER , createBox ); timer.start(); //Change createBox() accordingly... function createbox (event:TimerEvent):void { var _box = new Box; _box. X = Math.random()*stage. StageWidth ; _box.

Y = Math.random()*stage. StageHeight; addChild(_box); } Whenever you're bored with adding boxes, you simply call this timer.stop().

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions