Sign in to follow this  
khleug35

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

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/

Share this post


Link to post
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();
}    

 

Share this post


Link to post
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!!

 

 

 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.