roko68 Posted April 30, 2015 Share Posted April 30, 2015 I'm looking at this example:http://phaser.io/examples/v2/arcade-physics/shoot-the-pointer I'm trying to figure out how to have each bullet stop at the point where the pointer has been clicked. I think it would be similar to a Missile Command type of function. My limited knowledge isn't getting me very far, and I'm searching the forum for some guidance. Thanks for any advise! Link to comment Share on other sites More sharing options...
MichaelD Posted April 30, 2015 Share Posted April 30, 2015 Fast and dirty solution would be to check the x/y of the bullet versus the input x/y and when the bullet x/y is greater remove the bullet or set velocity.x = 0; velocity.y = 0; Link to comment Share on other sites More sharing options...
drhayes Posted May 1, 2015 Share Posted May 1, 2015 fuzzyEqual is probably going to be your friend, here. Link to comment Share on other sites More sharing options...
roko68 Posted May 3, 2015 Author Share Posted May 3, 2015 Thanks so much! I kind of see how this could work using fuzzyEqual, but I'm not sure how to detect the position of the input? Link to comment Share on other sites More sharing options...
roko68 Posted May 4, 2015 Author Share Posted May 4, 2015 Any other thoughts? It's over my head, and could use a little more help. I'm using the Shoot the Pointer example, and basically want the bullets to stop at the point where the pointer is clicked. I'd like to then create an explosion that kills bad guys if they're close to it. The issue I'm running into is that I don't know how to detect the position where the pointer is clicked. Also, If I use the fuzzyEqual method, to compare the bullet velocity with the pointer clicked position... should I do this in the fire function? Or update? I'm not finding any examples of how fuzzyEqual works. Link to comment Share on other sites More sharing options...
drhayes Posted May 4, 2015 Share Posted May 4, 2015 Check out "game.input.pointer.x" and "game.input.pointer.y". It might be worldX/worldY, instead. I *think* those are what you're looking for. When the user clicks, check those values for the point in world where everything should go all explodey and you're good. Link to comment Share on other sites More sharing options...
roko68 Posted May 5, 2015 Author Share Posted May 5, 2015 Thanks for this. It sounds pretty straight-forward, but I'm just not able to get it working. I'm creating a variable: this.difference = 10; Here's the code I'm using. I'm placing it in the update function, but I'm unsure if this is where it should go: if (this.math.fuzzyEqual(bullet.body.velocity, game.input.pointer.x, this.difference) || this.math.fuzzyEqual(bullet.body.velocity, game.input.pointer.y, this.difference)); {console.log("Stopping Bullet");bullet.body.velocity = 0;} I'm getting an error, I think because the bullets don't exist until they've been fired. Are there any working examples of something similar that I can look at? Link to comment Share on other sites More sharing options...
drhayes Posted May 5, 2015 Share Posted May 5, 2015 Start your if condition like this: "if (bullet && this.math.fuzzyEqual(" to make sure the bullet exists first and you might be good. roko68 1 Link to comment Share on other sites More sharing options...
trasheater Posted May 5, 2015 Share Posted May 5, 2015 I made an example of this for you. I was not sure if you wanted to make the bullets explode next to the pointer or if you wanted the bullets to explode when they reached the x,y location of the pointer when you fired the bullet. I am assuming that you would like to have the bullets explode when the reach the x,y of the pointer when fired. to do this i have stored the x,y of the pointer at the moment of firing the bullet within the bullet itself. function fire() { if (game.time.now > nextFire && bullets.countDead() > 0){ nextFire = game.time.now + fireRate; var bullet = bullets.getFirstDead(); bullet.endPointX = game.input.x; bullet.endPointY = game.input.y; bullet.reset(sprite.x - 8, sprite.y - 8); game.physics.arcade.moveToPointer(bullet, 300); } }I am setting the endPointX and endPointY of the bullet to the pointer x and y. We will check if they are close to that location within the update function. bullets.forEachAlive(function(item) { if( this.math.fuzzyEqual(item.x,item.endPointX,5) && this.math.fuzzyEqual(item.y,item.endPointY,5) ){ emitter.x = item.x; emitter.y = item.y; emitter.start(true, 2000, null, 10); item.kill(); } }, this); As suggested by drhayes i have used fuzzyEqual() to detect if the bullet is close to the saved pointer x and y. Here is a working example:http://jsfiddle.net/trasheater/6rdrq2v0/ Link to comment Share on other sites More sharing options...
roko68 Posted May 5, 2015 Author Share Posted May 5, 2015 Awesome! This is exactly what I was trying to figure out.... not sure that I would have, I'm still pretty new to this. Thank you guys so much! Link to comment Share on other sites More sharing options...
Recommended Posts