Jump to content

body.blocked.left/right always false


jamezwhite33
 Share

Recommended Posts

HI all,

 

I am new to Phaser and am so far having fun :)

 

After finishing the "creating your first game" tutorial I started extending the project, but i am having trouble with 'body.blocked' functionality - both left and right are always false, even though my character is walking into the wall repeatedly.

 

Here I am registering collision between the player and my wall tiles:

game.physics.arcade.collide(player, walls);

Immediately after this, I do a simple check to see if i cannot move left or right anymore:

if (player.body.blocked.left || player.body.blocked.right) 

However, both the left and right booleans are always false, and my character continues to walk into the wall. Does anyone have any suggestions how I might be able to fix this?

 

Thanks,

 

Jamie

 

Link to comment
Share on other sites

23 hours ago, jamezwhite33 said:

Anyone?

Why don't you set the velocity of the player to 0?

function update(){
	game.physics.arcade.collide(player, wall, stop, null, this);
}

function stop(){
	player.body.velocity.x = 0;
	player.body.velocity.y = 0;
}

 

Link to comment
Share on other sites

On 12/01/2016 at 7:29 PM, Batzi said:

Why don't you set the velocity of the player to 0?

function update(){
	game.physics.arcade.collide(player, wall, stop, null, this);
}

function stop(){
	player.body.velocity.x = 0;
	player.body.velocity.y = 0;
}

 

Hi there,

Apologies for the late response.

For this project I am constantly applying a velocity to the player. When the charcater hits a wall (i.e. bumps into a wall whilst running, not jumping) I would like to reverse the direction. I have made some adjustments based on your example:

game.physics.arcade.collide(player, walls, checkdirection, null, this);

function checkdirection(){

	if (player.body.blocked.left || player.body.blocked.right) 
	{
		direction *= -1;
        player.body.velocity.x = 150 * direction;
    }
}

However, the 'body.blocked' flags are still always false, even when this function is called.

Link to comment
Share on other sites

3 hours ago, jamezwhite33 said:

Hi there,

Apologies for the late response.

For this project I am constantly applying a velocity to the player. When the charcater hits a wall (i.e. bumps into a wall whilst running, not jumping) I would like to reverse the direction. I have made some adjustments based on your example:

game.physics.arcade.collide(player, walls, checkdirection, null, this);

function checkdirection(){

	if (player.body.blocked.left || player.body.blocked.right) 
	{
		direction *= -1;
        player.body.velocity.x = 150 * direction;
    }
}

However, the 'body.blocked' flags are still always false, even when this function is called.

Don't you want the player to stop when they hit the wall?

Link to comment
Share on other sites

Hi, in arcade physics there are two sets of flags:

  • blocked when "the Body collides with the World bounds or a Tile"
  • touching when "when the Body collides with another"

 Is your wall made of tiles or is it just another body? Hmmm, from your code it seems to be body ... then use "touching".

 

Link to comment
Share on other sites

On 19/01/2016 at 11:05 AM, Tom Atom said:

Hi, in arcade physics there are two sets of flags:

  • blocked when "the Body collides with the World bounds or a Tile"
  • touching when "when the Body collides with another"

 Is your wall made of tiles or is it just another body? Hmmm, from your code it seems to be body ... then use "touching".

 

Yes, the tiles are a group of sprites with bodies. The 'touching' function has solved my problem - thank you very much!

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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