Jump to content

touching not working if called before collide


Bengalaa
 Share

Recommended Posts

Hi there. Not sure if this is an issue, but, just checked on the documentation, and it does not say anything about this behaviour...
 
I have this code, a simple code that collides two sprites:
 
var mainState = ( function () {  var preload = function () {    game.load.spritesheet('cota-cola', 'assets/cota-cola.png', 16, 33);    game.load.spritesheet('carousel', 'assets/baggage-carousel.png', 50, 100);  }  var create = function () {    var block;    cotacola = game.add.sprite(150,0, 'cota-cola');    game.physics.arcade.enable(cotacola);    carousel = game.add.group();    for (var i=0; i<5; i++) {      block = game.add.sprite(100 + i*50, 100, 'carousel');      game.physics.arcade.enable(block);      block.body.immovable = true;            carousel.add(block);    }    cotacola.body.acceleration.y = 400;  }  var update = function () {    console.log(cotacola.body.touching.down);    game.physics.arcade.collide(cotacola, carousel);  };  return { preload : preload,  create : create,  update : update };})();var game = new Phaser.Game(500, 300, Phaser.AUTO, 'game');game.state.add('main', mainState);game.state.start('main');
 
if i call any member of touching before the collide function, it will always be false...
 
    var update = function () {        console.log(cotacola.body.touching.down); // will always print false        game.physics.arcade.collide(cotacola, carousel);    };
but if I call it after the collide function, it will be accurate...
    var update = function () {        game.physics.arcade.collide(cotacola, carousel);        console.log(cotacola.body.touching.down); // this works fine    };

In the documentation http://phaser.io/docs/2.4.3/Phaser.Physics.Arcade.Body.html#touching it does not say anything about this behaviour. But I think that if this isn't an issue, a warning about this should be documented.

 

 

Should I post this as a github issue?

 

== EDIT ==

 

I just discovered that the behaviour I want, is actually the .wasTouching

var update = function () {    game.physics.arcade.collide(cotacola, carousel);    console.log(cotacola.body.wasTouching.down); // this works fine};

This works even if I put it before the collide

var update = function () {    console.log(cotacola.body.wasTouching.down); // this also works fine    game.physics.arcade.collide(cotacola, carousel);};

It is kind of confusing, though... because, in a bigger context, i might want to see whether a sprite 'a' is touching something or not, from the update of another sprite 'b'... and if the collide function is coded on the sprite 'a', and the check is realized from the sprite 'b', then this behavior would be very confusing if you don't know which update function gets executed first.

Link to comment
Share on other sites

This doesn't sound like a bug to me, but maybe it's because of how my brain works. Until you ran the collision you didn't know if it was touching or not. And if you want a specific behavior upon collision you can set a collisionCallback in the collide method..? Would that work?

Link to comment
Share on other sites

i am not really sure if it would work now... back on time, it didn't work...

 

what I was trying to do, was a little conveyor belt, that caused one thing 'A' to move into one direction when standing in it.

 

'A' can have several items inside it, so I need those items to move towards the conveyor belt as well, even if they are "floating" inside the thing 'A', so I needed to check whether or not the 'A' thing was touching the conveyor belt or not, inside the update function of the items, and the collide function of the thing 'A' was inside 'A's update function...

 

That's why I need the touching method, because, i have tried to achieve something like this with a collision callback before, but it didn't work, because the collision only worked when the two objects where strongly colliding, and not when the two objects were only touching each other.

 

The 'wasTouching' function was really useful, as I said at the end of the post... and then, I replaced this behavior for a "copy the velocity of the 'A' thing i am contained into' in the update function of the items, but as I still felt that the touching method wasn't responding accurately, I posted here the problem...

 

Maybe there is a scenario where the touching function of some thing might be called from the update function of another thing... as we don't know (do we?) in which order the update functions get executed, this behavior would be kind of weird... why are wasTouching and touching different? shouldn't they be fused? or probably it should be documented as "returns false until the collide function is executed. If The collision caused it to touch something, it then returns true"... 

 

but i am not really sure about this... i just found it very confusing

Link to comment
Share on other sites

I can't add them as a children of 'A', because i need to apply one mask to 'A', and another different to the items... the item is not intended to be a dumb child of 'A' :S

 

i already figured out how to make this conveyor belt :D i did it by adding it a velocity on the x axis whenever it is touching down the , i can't make it as platforms moving, because the conveyor belt is supposed to be static... but thanks for the fiddle! XD there: http://jsfiddle.net/6018w2an/ I added to it some conveyor belts to the platforms...

 

I guess it is not a bug then...

 

 

 

== EDIT ==

the link was not pointing to the modified version o.O 

sorry, just learning to use the fiddle...

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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