Jump to content

[Solved]How to limit the shooting direction of the sprite??


khleug35
 Share

Recommended Posts

I use the following example to get start.

https://phaser.io/examples/v2/arcade-physics/shoot-the-pointer

When the arrow's angle  <= 90 and  >= 90, the arrow will stop move and can not shoot to the left!!!!

A.PNG.429d8dbc9203d122be3690b3bec89e1c.PNG

 

I use the follow code

function update() {
   //right
     if(sprite.angle <= -90 && sprite.angle >= -180){
    	sprite.angle=-90;
    } else if(sprite.angle >= 90 && sprite.angle <= 180){
    	sprite.angle=90;
    }
  //left
  /*
    if(sprite.angle >= -90 && sprite.angle <= 0){

      sprite.angle = -90;  

    } else if(sprite.angle <= 90 && sprite.angle >= 0){
      sprite.angle = 90;  
    } 

    */
}

 

But  I hope the bullet not shoot to the left, any idea??? Thanks you so much

a.png.33b04cb47e640641d0e1bb44bc1e3aca.png

 

Here is the jsfiddle link, Thanks

https://jsfiddle.net/sjrup0h2/

Link to comment
Share on other sites

You need to add the same angle constraints to bullet firing too.

So instead of firing when the pointer is down,

if (game.input.activePointer.isDown){
        fire();
}    

you fire when the pointer is down and your sprite follows your angle constraints.

if (game.input.activePointer.isDown && sprite.angle >= -90 && sprite.angle <=90){
    fire();
}    

 

Link to comment
Share on other sites

1 hour ago, gauravD said:

You need to add the same angle constraints to bullet firing too.

So instead of firing when the pointer is down,


if (game.input.activePointer.isDown){
        fire();
}    

you fire when the pointer is down and your sprite follows your angle constraints.


if (game.input.activePointer.isDown && sprite.angle >= -90 && sprite.angle <=90){
    fire();
}    

 

Works perfectly!!! You are so awesome!!!Thank you very much and have a nice day!!

 

 

 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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