Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 02-12-2012, 06:00 PM   #1
Teague
Registered User
 
Join Date: Feb 2011
Location: Newcastle
Posts: 9
Default [AS3] Spawning

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.

Code:
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

Last edited by Teague; 02-12-2012 at 06:36 PM.
Teague is offline   Reply With Quote
Old 02-12-2012, 09:51 PM   #2
-:)lauri
Senior Member
 
Join Date: Aug 2008
Location: Helsinki, Finland
Posts: 1,182
Default

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.

ActionScript Code:
Math.random() * 400;
-:)lauri is offline   Reply With Quote
Old 02-12-2012, 10:05 PM   #3
Teague
Registered User
 
Join Date: Feb 2011
Location: Newcastle
Posts: 9
Default

Thank you for the response!

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
Teague is offline   Reply With Quote
Old 02-12-2012, 10:41 PM   #4
-:)lauri
Senior Member
 
Join Date: Aug 2008
Location: Helsinki, Finland
Posts: 1,182
Default

If you have enemy in library with class name "Enemy" with this you can put enemy inside the "movieclip" on the stage:
ActionScript Code:
var enemy:MovieClip = new Enemy(); movieclip.addChild(enemy); enemy.x = Math.random() * 400; enemy.y = 400;
-:)lauri is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 09:05 PM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.