Jump to content

Make a sprite immovable by player


owen
 Share

Recommended Posts

I have some sprites that I want to be 'immovable', ie. they are deadweights that drop to the floor and stay there. They display OK and the drop to the floor correctly, but for some reason the player is able to 'push them' and they slide off screen.  This is something I want to disallow.  I want the player to collide with them but not move them.  Is there a property I should be setting? I have tried all of the following so far:

tons.forEach(function (ton) {        game.physics.arcade.enable(ton);        ton.body.bounce.y = TON_BOUNCE;        ton.body.gravity.y = TON_GRAVITY;        ton.body.velocity.x = 0;        ton.body.drag = 10;        ton.body.blocked.left = true;        ton.body.blocked.right = true;        ton.body.collideDown = true;        ton.body.collideLeft = true;        ton.body.collideRight = true;        ton.body.collideUp = true;                ton.body.collideWorldBounds = true;    }, this, false);

You will see the problem in action here: http://www.owensouthwood.com/mrgoggles

 

I also have tried the obvious:

ton.body.immovable = true

This almost works. It makes the sprite into a dead weight but it also seems to stop collision with other weights.  I want it to be 'immovable' only once it's landed/settled into position and is not moving.  I want it to fall and land on top of other weights and only then does it become immovable. How can I achieve that exactly?

 

 

Thanks!

Owen

Link to comment
Share on other sites

Don't know about your specific case, but couldn't you add a condition in the update(), like when a "tons" sprite isn't moving anymore for more than 100ms (or a number of update calls) you set it's body.immovable to true

 

This is what I would like to do but what is the syntax for checking of a sprite isn't moving? 

 

Thanks

Owen

Link to comment
Share on other sites

Thanks for the help so far.  I'm still having problems unfortunately... I'm calling the following function from within update() so in theory it's constantly running.  The problem is it has absolutely no effect, the player can still cause the tons to move by pushing against them from left or right.   The key thing here is that I only want to immobilize the tons against horizontal movement (not vertical).  And I only want to do this if they are 'settled' ie. not currently falling.

 

function fixTons() {    // for all of the currently not moving tons, immobilize them so player cannot 'push' them sideways.        tons.forEach(function (t) {                if (t.body.velocity.y == 0  || t.body.deltaY == 0  )  {            // ton is NOT FALLING so immobilize it against player pushes.            console.log("imobilising a non-moving ton"); // this outputs so it gets this far but the lines below dont have an effect!            t.body.moves = false;            t.body.enable = false;            t.body.gravity.x = 0;            t.body.velocity.x = 0;        }    }, this, false);}
 
Link to comment
Share on other sites

EDIT:  Thanks. For now I've worked around it by having it affect all of the 'ton' sprites on the map (not just those I'm detecting as 'not moving').

 

function fixTons() {// stop 'tons' from moving    tons.forEach(function (t) {                                t.body.gravity.x = 0;            t.body.velocity.x = 0;               }, this, false);}
This works - or at least it gives an acceptable effect which is not quite what I wanted.  It allows my 'ton' weights to be pushed around however they remain still when untouched by the player.  (Before, they were sliding off the screen without stopping, after the slightest touch).  
 
I think I can settle for this now - except that it seems very inefficient to run this setting for every iteration of the update() loop so I will have to put in a limitation for it to run only upon the tons touched by the player (and every few cycles).
 
@Dumtard - I'm going to look at your suggestion next.
Link to comment
Share on other sites

 

Thanks for the help so far.  I'm still having problems unfortunately... I'm calling the following function from within update() so in theory it's constantly running.  The problem is it has absolutely no effect, the player can still cause the tons to move by pushing against them from left or right.   The key thing here is that I only want to immobilize the tons against horizontal movement (not vertical).  And I only want to do this if they are 'settled' ie. not currently falling.

 

function fixTons() {    // for all of the currently not moving tons, immobilize them so player cannot 'push' them sideways.        tons.forEach(function (t) {                if (t.body.velocity.y == 0  || t.body.deltaY == 0  )  {            // ton is NOT FALLING so immobilize it against player pushes.            console.log("imobilising a non-moving ton"); // this outputs so it gets this far but the lines below dont have an effect!            t.body.moves = false;            t.body.enable = false;            t.body.gravity.x = 0;            t.body.velocity.x = 0;        }    }, this, false);}

 

When using this approach, did you get "imobilising a non-moving ton" in the console ? if yes, then maybe you should try using

 

t.body.immovable = false

 

to make the tons immovable once they stop moving.

Link to comment
Share on other sites

When using this approach, did you get "imobilising a non-moving ton" in the console ? if yes, then maybe you should try using

 

t.body.immovable = false

 

to make the tons immovable once they stop moving.

 

But I don't want it totally immovable.  Only immovable from horizontal directions (left/right).  I want it still able to fall downward.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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