(click anywhere to close)
OPEN MENU

[Phaser] Extraordinary Events

category: Games | course: Phaser | difficulty:
IN PRINT
QUICK CONTENTS:Intro
1. So Easy?!

Phaser also has a few built-in events ready for anything, think of them as the Event Listeners you find in many languages.

First, to save CPU, every event is set to false and is not checked. The event sends of 'Phaser signals' and waits for them to be picked up by something. If we want our game to recognize them, we simply add a listener with our function it should call to it. Like this:

//Here's how you attach an event to any object (let's take our player sprite)
player.events.<THE EVENT HERE>.add(yourFunctionToCall(), this);

//And these are all the events that are possible, which I thought were quite self-explanatory 
//(so, substitute any of them with '<THE EVENT HERE>', and you're good to go)

onAddedToGroup
onRemovedFromGroup
onKilled
onRevived
onOutOfBounds
onEnterBounds

onInputOver
onInputOut
onInputDown
onInputUp
onDragStart
onDragStop

onAnimationStart
onAnimationComplete
onAnimationLoop

So Easy?!

Yes. Well, the input events require some extra work, for input detection is turned off by default. To make them work, you have to:

//Make it detect any input on the object
sprite.inputEnabled = true;

//Make dragging available for the object
sprite.enableDrag();
 
CONTINUE WITH THIS COURSE
Do you like my tutorials?
To keep this site running, donate some motivational food!
Crisps
(€2.00)
Chocolate Milk
(€3.50)
Pizza
(€5.00)