Jump to content

SOLVED issue with world bounds collision


NoxBrutalis
 Share

Recommended Posts

Hi everyone, I'm trying to implement world bounds and can't get it to work.

Here's how I'm doing it:

 

this.physics.world.setBounds(0, 0, this.layer1.width, this.layer1.height, true, true, true, true);

this.player = new Player(this, objLayer[0].x, objLayer[0].y, 'playerSheet', game).setDepth(3);
this.player.body.setCollideWorldBounds(true);
this.player.body.onWorldBounds = true;
this.physics.world.on('worldbounds', function(body){
    console.log('hello from the edge of the world', body);
},this);


player just walks wherever he damn pleases, like there are no bounds. The only thing I can think is that it's because he's part  of a group. I tried adding a collider between the playerGrp and this.physics.world.bounds, but no joy there either.

I saw someone in a tutorial setting their player to collide and they didn't use .body, probably because their sprite was extended, and so is mine. So i tried this.player.setCollideWorldBounds(true) also, but that doesn't change anything.

All the tutorials I've seen basically do the above and apparently it works, but not for me. If there's nothing immediately wrong with this approach, I'll try a minimal example. As the game does use scrolling in all directions, so maybe that is affecting it.

Anyways, thanks for your time.

Link to comment
Share on other sites

SOLVED AT LAST :D

Im an idiot really as I think I read a post about this recently, but Im guessing a group is considered a container? I saw someone say that containers wipe set data from gameobjects when you add them to the container. This is definitely the case with group, though I'm wondering if this is expected behaviour? Im not sure as many people would spot it, as most people seem to have their players as standalone, but in my project the player is in a group with other friendly a.i sprites.

So basically, if you add a sprite to a group after having set :


 

this.player.setCollideWorldBounds(true);
this.player.onWorldBounds = true;

then its as if you never set it. So you either have to set them to true again, or set them to true after adding them to the group. There's possibly a way to set such properties on the group as a whole, but I don't know it.

Anyways, so if your world bounds aren't working, check the order in which you initialise some of these properties  in relation to when you add the sprite to a group etc.

Link to comment
Share on other sites

  • NoxBrutalis changed the title to SOLVED issue with world bounds collision
  • 5 months later...
  • 1 year later...
 Share

  • Recently Browsing   0 members

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