Jump to content

Shoot to pointer XY position


roko68
 Share

Recommended Posts

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

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

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

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

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

 Share

  • Recently Browsing   0 members

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