javier_m Posted November 24, 2017 Share Posted November 24, 2017 Hello, I have a sprite which has velocity.x=100 moving from {x:0; y: 200}. I need to set velocity.x=0 (e.g. stop sprite) once sprite reaches the coordinates {x:300; y:200}. How can it be done? I can't call collide function since there is nothing on that coordinates. I guess I need use the signal? Or any other solution? Thank you in advance. Any help will be appreciated. Link to comment Share on other sites More sharing options...
Machine-dev Posted November 26, 2017 Share Posted November 26, 2017 in update() I would do something like: if (player.world.y >= 200) { player.body.velocity.y = 0; player.body.velocity.x = 0; } Link to comment Share on other sites More sharing options...
javier_m Posted November 26, 2017 Author Share Posted November 26, 2017 Thanks for your answer but I worry that update() will consume the processor since I have over 40 sprites to check for stop point. Or is it okey amount? I'm just new to the phaser. Link to comment Share on other sites More sharing options...
Machine-dev Posted November 26, 2017 Share Posted November 26, 2017 I don't think 40 would overwhelm the browser but safer to test and find out. If it does cause problems I would flag each object in an associative array and only check till it has been stopped once. Link to comment Share on other sites More sharing options...
javier_m Posted November 27, 2017 Author Share Posted November 27, 2017 Btw, is the any chance to put signal on point reach? Like it works on signal on.die? Link to comment Share on other sites More sharing options...
DanielKlava Posted November 27, 2017 Share Posted November 27, 2017 Hello! Perhaps you could insert a blank sprite where you want the sprite to stop, and check the collision between only the sprites you want. Then, in the collision callback you set the velocity to 0. Something like this. Assuming that your sprite is moving horizontally from left to right, and that he must stop regardless of it's Y position: create : function (){ //adds sprite at the position needed this.checkpoint = this.add.sprite(100, 2); //scale the height of the sprite to full height, so the user cannot miss it. this.checkpoint.scale.y = this.game.height; }, update : function (){ if (this.checkpoint != null && this.checkOverlap(this.player, this.checkpoint)) { //perform full stop this.player.body.velocity.y = 0; this.player.body.velocity.x = 0; //set the sprite to null so the collision callback occurs only once. this.checkpoint = null } }, //use a simple function to check the overlap so you won't have to initialize Physics in the collision sprite and save some processing checkOverlap : function (spriteA, spriteB) { var boundsA = spriteA.getBounds(); var boundsB = spriteB.getBounds(); return Phaser.Rectangle.intersects(boundsA, boundsB); } I am using a similar logic to trigger various events, I think it should work! Link to comment Share on other sites More sharing options...
Recommended Posts