Be a Supporter!

AS3 function for all instance names

  • 704 Views
  • 7 Replies
New Topic Respond to this Topic
Thrallec101
Thrallec101
  • Member since: May. 15, 2012
  • Offline.
Forum Stats
Member
Level 01
Blank Slate
AS3 function for all instance names 2012-10-19 00:21:36 Reply

so i'm making a zombie game, and i'll have a lot of zombies on stage with the same instance name( or linkage?) and i dont want to have to name them all diffrently, zombie1, zombie2, ect so how can i write a code for all of them?

also i just started learning as3 so dumb it down for me please


...

MintPaw
MintPaw
  • Member since: Jun. 11, 2006
  • Offline.
Forum Stats
Member
Level 10
Programmer
Response to AS3 function for all instance names 2012-10-19 00:28:38 Reply

Short answer = don't, store all their references in an array as they're being created.

Quick example.

var z:Zombie = new Zombie();
z.x = 10;
z.y = 10;
addChild(z);
_zombieArray.push(z);

Now the zombie reference will be pushed into the Array ready to be called upon while looping through it at other times.

Instance names are a thing from the devil.


If ya have something to say, PM me. I have a lot of time to spare.
Also never PM egg82.

BBS Signature
Thrallec101
Thrallec101
  • Member since: May. 15, 2012
  • Offline.
Forum Stats
Member
Level 01
Blank Slate
Response to AS3 function for all instance names 2012-10-19 03:16:00 Reply

At 10/19/12 12:28 AM, MintPaw wrote: Short answer = don't, store all their references in an array as they're being created.

Quick example.

var z:Zombie = new Zombie();
z.x = 10;
z.y = 10;
addChild(z);
_zombieArray.push(z);

Now the zombie reference will be pushed into the Array ready to be called upon while looping through it at other times.

Instance names are a thing from the devil.

ok this is my code now

var zombieArray :Array = [];

for(var i:Number = 0; i < 10; i++){
var z:Zombie = new Zombie();
z.x = Math.random()*300
z.y = Math.random()*300
zombieArray.push(z);
addChild(z);
}
function start2(e:Event):void {
zombieArray[1].rotation ++;
}
addEventListener(Event.ENTER_FRAME, start2);

i made 10 zombies and i can control the first one using zombieArray[1].rotation ++; but how can i control all the zombies in the array? (i diddnt understand what you meant by "looping" maybe you could give me an example?) something like

zombieArray[ALL_ZOMBIES].rotation ++;?

>_<


...

milchreis
milchreis
  • Member since: Jan. 11, 2008
  • Offline.
Forum Stats
Member
Level 26
Programmer
Response to AS3 function for all instance names 2012-10-19 04:39:47 Reply

At 10/19/12 12:21 AM, Thrallec101 wrote: how can i write a code for all of them?

That's what classes are for.
Store multiple instances of that class in an array.

ProfessorFlash
ProfessorFlash
  • Member since: Oct. 6, 2007
  • Offline.
Forum Stats
Member
Level 32
Programmer
Response to AS3 function for all instance names 2012-10-19 06:58:49 Reply

At 10/19/12 03:16 AM, Thrallec101 wrote: i made 10 zombies and i can control the first one using zombieArray[1].rotation ++; but how can i control all the zombies in the array? (i diddnt understand what you meant by "looping" maybe you could give me an example?) something like

zombieArray[ALL_ZOMBIES].rotation ++;?
for(var i:Number = 0; i < 10; i++){

What do you think "i" is and what does this do?


You can solve pretty much any problem you may have with AS3 by consulting the AS3 Language reference.

MintPaw
MintPaw
  • Member since: Jun. 11, 2006
  • Offline.
Forum Stats
Member
Level 10
Programmer
Response to AS3 function for all instance names 2012-10-19 10:33:42 Reply

Just loop through them all.

Example:

addEventListener(Event.ENTER_FRAME, update);

function update(e:Event):void
{
     for (var i:int = 0; i < _zombieArray.length; i++)
     {
          _zombieArray[i].rotation++;
     }
}

Name your functions some more meaningful.


If ya have something to say, PM me. I have a lot of time to spare.
Also never PM egg82.

BBS Signature
egg82
egg82
  • Member since: Jun. 24, 2006
  • Offline.
Forum Stats
Supporter
Level 05
Game Developer
Response to AS3 function for all instance names 2012-10-19 10:47:55 Reply

At 10/19/12 03:16 AM, Thrallec101 wrote: ok this is my code now

fixed.

private var _zombieArray:Vector.<Zombie> = new Vector.<Zombie>;
		
		private function init(e:Event):void {
			for (var i:uint = 0; i < 10; i++) {
				_zombieArray[i] = new Zombie();
				_zombieArray[i].x = Math.random() * 300;
				_zombieArray[i].y = Math.random() * 300;
				addChild(_zombieArray[i]);
			}
			
			addEventListener(Event.ENTER_FRAME, update);
		}
		
		private function update(e:Event):void {
			for (var i:uint = 0; i < _zombieArray.length; i++) {
				_zombieArray[i].rotation++;
			}
		}
package  {
	import flash.display.Sprite;
	
	/**
	 * ...
	 * @author Alexander
	 */
	
	public class Zombie extends Sprite {
		public function Zombie() {
			
		}
	}
}

Programming stuffs (tutorials and extras)
PM me (instead of MintPaw) if you're confuzzled.
thank Skaren for the sig :P

BBS Signature
Thrallec101
Thrallec101
  • Member since: May. 15, 2012
  • Offline.
Forum Stats
Member
Level 01
Blank Slate
Response to AS3 function for all instance names 2012-10-19 18:01:57 Reply

At 10/19/12 10:33 AM, MintPaw wrote: Just loop through them all.

Example:

addEventListener(Event.ENTER_FRAME, update);

function update(e:Event):void
{
for (var i:int = 0; i < _zombieArray.length; i++)
{
_zombieArray[i].rotation++;
}
}

Name your functions some more meaningful.

that worked! thank you so much mintpaw for helping this noob out :)


...