owen Posted August 16, 2014 Share Posted August 16, 2014 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 = trueThis 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 More sharing options...
haden Posted August 16, 2014 Share Posted August 16, 2014 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 Link to comment Share on other sites More sharing options...
PXBILL Posted August 16, 2014 Share Posted August 16, 2014 try:sprite.body.moves = false;or:sprite.body.enable = false; Link to comment Share on other sites More sharing options...
owen Posted August 17, 2014 Author Share Posted August 17, 2014 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? ThanksOwen Link to comment Share on other sites More sharing options...
valueerror Posted August 17, 2014 Share Posted August 17, 2014 just check against sprite.body.velocity.x and sprite.body.velocity.y ?if(sprite.body.velocity.y == 0 && sprite.body.velocity.x == 0){notmoving=true} owen 1 Link to comment Share on other sites More sharing options...
Dumtard Posted August 17, 2014 Share Posted August 17, 2014 http://docs.phaser.io/Phaser.Physics.Arcade.Body.html#deltaAbsX and http://docs.phaser.io/Phaser.Physics.Arcade.Body.html#deltaAbsY will be the difference in position between current and previous frame. If that is == 0 then it has not moved. Link to comment Share on other sites More sharing options...
owen Posted August 17, 2014 Author Share Posted August 17, 2014 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 More sharing options...
Dumtard Posted August 17, 2014 Share Posted August 17, 2014 I would look to use customSeparateX and customSeparateY then. Setting one of those to true will stop the separation on that axis, you will have to handle that yourself in a processCallback in the collide function. owen 1 Link to comment Share on other sites More sharing options...
owen Posted August 17, 2014 Author Share Posted August 17, 2014 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 More sharing options...
haden Posted August 18, 2014 Share Posted August 18, 2014 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 More sharing options...
owen Posted August 18, 2014 Author Share Posted August 18, 2014 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 More sharing options...
valueerror Posted August 19, 2014 Share Posted August 19, 2014 you could make sure if deltaabsX is bigger than 0 - if so substract the delta from the x position to reset it.. brutal but it should work.. owen 1 Link to comment Share on other sites More sharing options...
Recommended Posts