Add Event Listener to an Entity


(Dennis Coleman) #1

I tried to add event listener to an entity (addEventListener), but it seems like it can’t do it. I’ve figured it out, that you can add event listeners to MovieClips and probably to someting higher in the hierarchy (Like DisplayObject), but MyClass extends Entity, Entity - Tweener, Tweener - nothing. So how can I add listeners to Entities? I had an idea to just to add extends Movieclip to Tweener, but I am afraid to mess something up.


(Martí Angelats i Ribera) #2

It depens of what event you want to handle.

For example, to handle is the mouse is over you can use this code in your class:

override public function update():void
{
	if (collidePoint(x, y, world.mouseX, world.mouseY))
	{
		//the code updated every frame that's over
	}
}

For mouse events you’ll have to use more code but for the other ones you’ll have to use

FP.stage.addEventListener(event, handeler);

but be sure to not use this too many times and check for the correspondent checks in the handeler to be sure you don’t tigger it when you don’t want to.



PD: What you want to handle?


(Dennis Coleman) #3

@Copying I just wanted to know that in general.


(Martí Angelats i Ribera) #4

For mouse you should use the mouse variables. For the keyboard events there is the Key class and also some functions. For anything else you’ll have to use that (FP.stage.addEventListener).


(Jacob Albano) #5

Adding extends MovieClip to tweener would have no effect. MovieClip event listeners aren’t called unless they’re added to the stage.

Ultimately, there’s no way to add event listeners to Entities. Flashpunk does things differently than you might be used to in Flash.


(TaylorAnderson) #6

Yeah, as Jacob said, pretty much everything that event listeners do Flashpunk handles in a different way. It’s a good idea to read some Flashpunk tutorials before you start using the framework, to get a handle on how things operate.