Be a Supporter!

AS3 Event Listener problem

  • 405 Views
  • 13 Replies
New Topic Respond to this Topic
Sushin00
Sushin00
  • Member since: Jan. 21, 2011
  • Offline.
Forum Stats
Member
Level 08
Game Developer
AS3 Event Listener problem 2013-01-24 18:30:43 Reply

When I call this function more than once to create multiple zombies, the event listener only works on the first one I call. Any advice?

function SpawnZombie(x_:int, y_:int):void{
	mcZombie = new EnemyZombie;
	mcZombie.x = x_;
	mcZombie.y = y_;
	
	
	function ZombieUpdate():void{
		mcZombie.ChasePlayer(mcMainCharacter);
	}
	
	mcZombie.addEventListener(Event.ENTER_FRAME, ZombieUpdate);
	aEnemies.push(mcZombie);
	stage.addChild(mcZombie);
}
milchreis
milchreis
  • Member since: Jan. 11, 2008
  • Offline.
Forum Stats
Member
Level 26
Programmer
Response to AS3 Event Listener problem 2013-01-24 18:56:14 Reply

At 1/24/13 06:30 PM, Sushin00 wrote: Any advice?
mcZombie.addEventListener(Event.ENTER_FRAME, ZombieUpdate);

Do not add an ENTER_FRAME to each zombie.
Instead: iterate over the array of zombies you've got:

aEnemies.push(mcZombie);

Do not add anythign to the stage, just add it to the document class:

stage.addChild(mcZombie);
Sandremss128
Sandremss128
  • Member since: Aug. 22, 2009
  • Offline.
Forum Stats
Supporter
Level 11
Programmer
Response to AS3 Event Listener problem 2013-01-24 19:06:08 Reply

I got mad weedâEU¦dude!

function SpawnZombie(x_:int, y_:int):void{
	mcZombie = new EnemyZombie;
	mcZombie.x = x_;
	mcZombie.y = y_;
	
	function makeZombieUpdate(zombie : Zombie) : Function{
		var handler : Function = function(e : Event) : void {
			zombie.ChasePlayer(mcMainCharacter);
		}
		return handler;
	}
	
	mcZombie.addEventListener(Event.ENTER_FRAME, makeZombieUpdate(mcZombie));
	aEnemies.push(mcZombie);
	stage.addChild(mcZombie);
}

besides the fact that you're using global variables in the handler, local variables won't work. You need to go one level deeper with the nested function with a local variable and the anonymous function as handler. Haven't tested it but I hope you get the idea.
Obviously there are better ways to do this but if you want to use local functions here you go.

Sushin00
Sushin00
  • Member since: Jan. 21, 2011
  • Offline.
Forum Stats
Member
Level 08
Game Developer
Response to AS3 Event Listener problem 2013-01-24 20:35:07 Reply

At 1/24/13 07:06 PM, Sandremss128 wrote: I got mad weedâEU¦dude!

function SpawnZombie(x_:int, y_:int):void{
mcZombie = new EnemyZombie;
mcZombie.x = x_;
mcZombie.y = y_;

function makeZombieUpdate(zombie : Zombie) : Function{
var handler : Function = function(e : Event) : void {
zombie.ChasePlayer(mcMainCharacter);
}
return handler;
}

mcZombie.addEventListener(Event.ENTER_FRAME, makeZombieUpdate(mcZombie));
aEnemies.push(mcZombie);
stage.addChild(mcZombie);
}

besides the fact that you're using global variables in the handler, local variables won't work. You need to go one level deeper with the nested function with a local variable and the anonymous function as handler. Haven't tested it but I hope you get the idea.
Obviously there are better ways to do this but if you want to use local functions here you go.

Well uh, that did it. I would love to know a better way because I'm pretty much being a pioneer.
All I really want to do is call the ChasePlayer function in a way that updates each frame for each zombie that I spawn.

Sandremss128
Sandremss128
  • Member since: Aug. 22, 2009
  • Offline.
Forum Stats
Supporter
Level 11
Programmer
Response to AS3 Event Listener problem 2013-01-24 21:47:59 Reply

At 1/24/13 08:35 PM, Sushin00 wrote: All I really want to do is call the ChasePlayer function in a way that updates each frame for each zombie that I spawn.

Simply do as milchreis said and work with arrays.

Sushin00
Sushin00
  • Member since: Jan. 21, 2011
  • Offline.
Forum Stats
Member
Level 08
Game Developer
Response to AS3 Event Listener problem 2013-01-24 23:55:16 Reply

At 1/24/13 09:47 PM, Sandremss128 wrote:
At 1/24/13 08:35 PM, Sushin00 wrote: All I really want to do is call the ChasePlayer function in a way that updates each frame for each zombie that I spawn.
Simply do as milchreis said and work with arrays.

K. I don't really get it but I'll see if I can figure out how that works.

Sushin00
Sushin00
  • Member since: Jan. 21, 2011
  • Offline.
Forum Stats
Member
Level 08
Game Developer
Response to AS3 Event Listener problem 2013-01-24 23:56:35 Reply

At 1/24/13 11:55 PM, Sushin00 wrote:
At 1/24/13 09:47 PM, Sandremss128 wrote:
At 1/24/13 08:35 PM, Sushin00 wrote: All I really want to do is call the ChasePlayer function in a way that updates each frame for each zombie that I spawn.
Simply do as milchreis said and work with arrays.
K. I don't really get it but I'll see if I can figure out how that works.

Er..actually, I would enjoy it if someone explained that idea in a little more detail.

MSGhero
MSGhero
  • Member since: Dec. 15, 2010
  • Offline.
Forum Stats
Supporter
Level 16
Game Developer
Response to AS3 Event Listener problem 2013-01-25 00:37:58 Reply

At 1/24/13 11:56 PM, Sushin00 wrote: Er..actually, I would enjoy it if someone explained that idea in a little more detail.

An arrayis basically a collection of all the stuff you put in it, and you can access each element by its index. Instead of

obj0.x += 5;
obj1.x += 5;
etc

you can loop through an array.

for (var i:int = 0; i < array.length; i++) {
   array[i].x += 5;
}
nitokov
nitokov
  • Member since: May. 8, 2012
  • Offline.
Forum Stats
Member
Level 05
Programmer
Response to AS3 Event Listener problem 2013-01-25 01:57:39 Reply

you can create array with new Array(), and put stuff in it with simple function like so:

your_array = new Array();
your_array.push(your_object);

RangeError: Error #1125: The index 4 is out of range 4.

Sushin00
Sushin00
  • Member since: Jan. 21, 2011
  • Offline.
Forum Stats
Member
Level 08
Game Developer
Response to AS3 Event Listener problem 2013-01-25 02:09:39 Reply

Thanks but I know what array is guys. I'm creating my zombies and pushing them into the array.

Specifically, I'm wondering what mil said here:

Do not add an ENTER_FRAME to each zombie.
Instead: iterate over the array of zombies you've got:
aEnemies.push(mcZombie);
Do not add anythign to the stage, just add it to the document class:

Does he mean to add the event listener inside of the class of mcZombie? How can I do that if I need a parameter from outside of the class to run it, like the player's x-coordinate?

MSGhero
MSGhero
  • Member since: Dec. 15, 2010
  • Offline.
Forum Stats
Supporter
Level 16
Game Developer
Response to AS3 Event Listener problem 2013-01-25 02:22:26 Reply

At 1/25/13 02:09 AM, Sushin00 wrote: Thanks but I know what array is guys. I'm creating my zombies and pushing them into the array.

Specifically, I'm wondering what mil said here:

Do not add an ENTER_FRAME to each zombie.
Instead: iterate over the array of zombies you've got:
aEnemies.push(mcZombie);
Do not add anythign to the stage, just add it to the document class:

Does he mean to add the event listener inside of the class of mcZombie? How can I do that if I need a parameter from outside of the class to run it, like the player's x-coordinate?

Your game only needs one enterframe total, not one for each zombie. In that one enterframe (located in the game's main class), you handle everything that needs updating. Loop through the array and call zombie.update() which allows the zombie to handle its own movement, check for collisions, etc. And then just addChild stuff to that main class, not to a stage reference

milchreis
milchreis
  • Member since: Jan. 11, 2008
  • Offline.
Forum Stats
Member
Level 26
Programmer
Response to AS3 Event Listener problem 2013-01-25 05:53:07 Reply

private function spawnZombie(x:int, y:int):Zombie
{
	var zombie:Zombie = new Zombie();
	zombie.x = x;
	zombie.y = y;
	
	enemies.push(zombie);
	addChild(zombie);

        return zombie;
}
private function gameLoop(e:Event):void
{
	for each (var enemy:Whatever in enemies)
                enemie.update();
}
milchreis
milchreis
  • Member since: Jan. 11, 2008
  • Offline.
Forum Stats
Member
Level 26
Programmer
Response to AS3 Event Listener problem 2013-01-25 07:36:10 Reply

fixing typo:

At 1/25/13 05:53 AM, milchreis wrote: private function gameLoop(e:Event):void
{
for each (var enemy:Whatever in enemies)
enemy.update(); // ie -> y
}
Sushin00
Sushin00
  • Member since: Jan. 21, 2011
  • Offline.
Forum Stats
Member
Level 08
Game Developer
Response to AS3 Event Listener problem 2013-01-25 13:02:02 Reply

At 1/25/13 05:53 AM, milchreis wrote: private function spawnZombie(x:int, y:int):Zombie
{
var zombie:Zombie = new Zombie();
zombie.x = x;
zombie.y = y;

enemies.push(zombie);
addChild(zombie);

return zombie;
}

private function gameLoop(e:Event):void
{
for each (var enemy:Whatever in enemies)
enemie.update();
}

Thanks a lot. I'll give that a shot.