PDA

View Full Version : [AS3] Spawning


Teague
02-12-2012, 06:00 PM
Hello there, I'm having a small problem with spawning enemies. I currently have a movieclip, called section1_mc. Within this movieclip I have another movieclip called spawn1_mc which moves up and down the screen.

I wish to spawn a third movieclip (enemyShip) along a random point of spawn1_mc's x axis.

The part of code which needs help on is shown below.


public function spawn(timer:TimerEvent):void {

var enemyShip:EnemyShip = new EnemyShip();
addChild(enemyShip);
var randomSeed:int = Math.random()*4 + 1;
trace ("enemyshiptimer");


if (randomSeed == 1) {
enemyShip.x = 400; //THE CODE IM GUESSING NEEDS TO CHANGE
enemyShip.y = 400;
trace ("random seed 1");
}



Thats the code I believe needs to be changed. I've attempted it myself but with no success :-( I'm fairly new to as3 but do I need to mess around the root function, and maybe bounding boxes? I'm not too sure as I mentioned but any help would be greatly appreciated! <3 x

-:)lauri
02-12-2012, 09:51 PM
While all values can be taken as a value on x axis of any of objects, here is math to calculate just random value, say from 400.

Math.random() * 400;

Teague
02-12-2012, 10:05 PM
Thank you for the response! :D

Yea I've been messing around with math.random. I'm really stumped on how I would call the enemy movieclip from the library to spawn inside a movieclip on the stage. I just can't seem to code it correctly, any ideas? <3

-:)lauri
02-12-2012, 10:41 PM
If you have enemy in library with class name "Enemy" with this you can put enemy inside the "movieclip" on the stage:

var enemy:MovieClip = new Enemy();
movieclip.addChild(enemy);
enemy.x = Math.random() * 400;
enemy.y = 400;