Jump to content

Detect when sprite collides with Wall


pdiddles03
 Share

Recommended Posts

You should be more explicit in order to get a better answer - are you using a physics system? If yes what? 

Have a good look at Phaser examples section - you'll surely find what you need and even learn a new thing or two. Arcade Physics and Overlap without physics  are two good places to start. 

Than you can take a look at phaser documentation and finally you can google your question - I'm sure there are some answers.

 

Link to comment
Share on other sites

9 minutes ago, pdiddles03 said:

The question is self explanatory.  The documentation i looked at did not show it. Anybody else?

What you want to do is clear, how you are attempting to accomplish it is not... the method for doing this varies greatly between different physics systems, how you are moving your character, how you set up everything...

If you aren't going to provide details or put effort into asking for people to help you, why would we put any effort into helping? 

The documentation and examples 100% have what you are looking to do, but you won't narrow down the search and we aren't going to link you to a dozen pages in hopes that one helps you.

Link to comment
Share on other sites

4 hours ago, pdiddles03 said:

Thank you.  Everybody would have saved a lot of time if i got those links right away :)

You're making me laugh. We would've saved even more time if you bothered to search the docs or examples or google it yourself or give more details when asking the question. 

 

5 hours ago, pdiddles03 said:

The question is self explanatory.  The documentation i looked at did not show it. Anybody else?

Also a condescending tone won't get you far when asking others for help. But maybe english it's not your first language so you're not so versed in using it (it's not mine either but I manage). Let me give you hand with that since I couldn't answer your original question

"I'm sorry if the question wasn't clear enough, here's exactly what I'm looking foor...insert details...The documentation wasn't very helpful either or maybe I didn't know where to look. Anybody else have other ideas?"

7 hours ago, pdiddles03 said:

I don't need to just bounce off the wall, i need to detect when it happens in order to switch direction of my character.  I can't seem to find an event or function for that.

You're probably new to Phaser - so am I! Here's how my train of thought would be if I had this particular problem, maybe it will help you somehow.

"Hmmm....so I'm using physics and I need an event to let me know when I touch the wall. Let's try....Phaser physics event on google. Top link is a phaser example describing collision events for Arcade Physics and the second one is P2 Physics impact events. Well, let's take a look at the first example:

bla blabla bla bla, AHA, 

    face1.body.onCollide = new Phaser.Signal();
    face1.body.onCollide.add(hitSprite, this);

Huh, dunno much about this onCollide thingy, what is it precisely? It says a Phaser.Signal. Let's look for that shall we?

Google it and the first link points to Phaser.Signal - 2.4.4. Click on it and switch to version 2.6.2. Let's see, bunch of text describing what it does, couple of properties which I don't need now and HEY....there's the method I just saw - add. Let's read it and try to understand what it does....got it.

I still don't know much about physics though so let's get back on that body thingy. Let's try.....Phaser Body on google . Sure enough, the first thing that pops is Phaser Physics Arcade Body. Let's see what we have here...scanning the page....it's not a property so let's move to methods....scanning further....well damn, there's no onCollide property. Where did we go wrong? Let's take a look back at the code and ..... oh yes, where creating a new property using the new keyword. But why? Let's go to back to Phaser Signal page and we see that:

As well as listening to pre-defined Signals you can also create your own:

var mySignal = new Phaser.Signal();

This creates a new Signal. You can bind a callback to it:

mySignal.add(myCallback, this);

I hope this short Google tutorial will help you with your future programming endeavours. 

Link to comment
Share on other sites

  • 3 years later...

Great response!

I've been facing the same problem with Phaser 3.  I have an animated fly that needs to face in a different direction when it bounces of the world bounds (using arcade physics).  It looks like the API has changed a bit since version 2.  After a lot of searching and trial and error, this works for me:

    fly = this.physics.add.sprite(780100'fly');
 
  // Detect when the fly bounces on a wall, and change the animation
    fly.body.onWorldBounds = true;
    this.physics.world.on('worldbounds'onWorldBounds);
 
    function onWorldBounds(body) {
        console.log('Bounce!');
        fly.setAnimation();         // method that checks direction and changes animation
    }

Hope this is helpful.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...