Sign in to follow this  
cruseyd

Object Specific Event Handlers

Recommended Posts

I've noticed a pattern while rifling through the Phaser 3 example code. Events seem to nearly always be handled globally, but the scene input. For example, in the snippet below from the drop zone tutorial, the code defines a handler for a gameObject being dropped into a drop  zone. But what if I have several types of game object which I want to handle being dropped differently? You could put a conditional in the global callback to check what kind of gameObject you're dealing with, or you could give all your gameObjects a callback with the same name (e.g. onDrop(args)), but I feel like there should be a much cleaner solution. Can I define all this functionality in class members for example?

Sorry if I've missed something obvious; the documentation for Phaser 3 seems like it's still relatively new. 

image.png.a29e2d82651cf9774596684fc5ace639.png

Share this post


Link to post
Share on other sites

Mmkay, I'm seeing that in the source code which is useful, but I can't seem to get the specific 'drop' event to fire. How does it differ from 'dragend'? Based on the source code it *looks like* a 'drop' has the semantics of a drag ending inside a valid drop zone, but I can't get it to trigger that way either (but I am able to trigger e.g. 'dragenter' which also requires a drop zone). 

Share this post


Link to post
Share on other sites
On 10/11/2018 at 8:47 AM, cruseyd said:

but I feel like there should be a much cleaner solution. Can I define all this functionality in class members for example?

    this.input.on('drop', function (pointer, gameObject, dropZone) {

        if(gameObject.myondrop) gameObject.myondrop(pointer,dropZone,whatever)

    });

 

Share this post


Link to post
Share on other sites
11 hours ago, cruseyd said:

Based on the source code it *looks like* a 'drop' has the semantics of a drag ending inside a valid drop zone, but I can't get it to trigger that way either (but I am able to trigger e.g. 'dragenter' which also requires a drop zone). 

It worked for me in http://labs.phaser.io/edit.html?src=src/input\zones\drop zone.js.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.