Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 04-14-2012, 06:37 AM   #1
Registered User
Join Date: Apr 2012
Posts: 5
Default [AS3] Spawing Different Types of Enemies from Array

Hi. I'm new to AS3 and have been playing around with it for a couple of weeks, learning how to make games. I'm making typical vertical shoot'em up games. Everything is good except I don't know how to spawn enemies of different types. I've searched a lot on this and haven't found that much on it. Most search results is how to spawn just one enemy type. I have other projects which are stuck at the same problem. Any help would be appreciated.

I have 4 movieclips in the library that will be my enemy ships.
I have them in array but I don't know where to go from there.

Here is some of my code:

ActionScript Code:
public var enemyArray:Array = new Array(enemy1,enemy2,enemy3,enemy4); public function newShip (e:Event):void {    var i:int = Math.floor(Math.random()*array.length);    var newEnemy:MovieClip = enemyArray[i];    newEnemy.x = Math.random()*400;    newEnemy.y = 0;    addChild(newObject);    objects.push(newObject);    for (var enemy:newEnemy in enemyArray){          y + y + 3 }
rbrt is offline   Reply With Quote
Old 04-14-2012, 08:48 AM   #2
Immma firing Ninnnnjaaa
Join Date: Aug 2011
Posts: 95

First: array.length won't work, there is no array. But if that is the array you want to use, make it like this: (array.length - 1). If you have 4 items in an array, the length is 4. But a array uses: 0, 1, 2, 3. So if the random thingie get's 4 it will prompt an error.

Second: So what's the problem... Won't it spawn 4 diffrent types? Then maybe try:

ActionScript Code:
public var enemyArray:Array = new Array(new enemy1(),new enemy2(), new enemy3(), new enemy4());

Hope that it helps
Biodiscus is offline   Reply With Quote
Old 04-14-2012, 05:11 PM   #3
Registered User
Join Date: Apr 2012
Posts: 5

Hey that worked! Thanks a lot. I was missing the keyword "new" the whole time.
rbrt is offline   Reply With Quote
Old 04-16-2012, 09:47 PM   #4
throw a trace() in there
Join Date: Dec 2006
Posts: 1,982

What you were creating was an Array of classes, by using new you're creating an Array of instances of those classes.

There is cool stuff you can do with an Array of classes. If you did that, then I think you could go:

var EnemyClass:Class = enemyArray[i];
var newEnemy:MovieClip = new EnemyClass();
If your Array gets really long, I think this approach is better on memory, since the enemies don't get instantiated until you really need them.
rrh is offline   Reply With Quote

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 02:18 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.