Jump to content

How to move and shoot at same time with multi-touch?


MattNewbie
 Share

Recommended Posts

Hi, I am new to phaser and am trying to figure out how to have my player move and fire at the same time. I am trying to use 2 pointers with no luck. I've read through a few posts here, but still can't get my player to do both actions simultaneously. The player will move or fire only exclusively. Sorry the code below is a bit sloppy and redundant for now. Thanks!

    /***Player Controls***/
    this.player.body.velocity.y = 0;
      
    //touchscreen  
    var direction = 0;
    var firePress = false;
    

    //check pointer 1
    if(this.game.input.pointer1.isDown) {
      var targetX = this.game.input.pointer1.position.x;
      //left side for player movement
      if (targetX < this.game.world.centerX){          
        var targetY = this.game.input.pointer1.position.y;  
        direction = targetY >= this.game.world.centerY ? 1 : -1;
      }         
      //right side for fire bullet
        if (targetX > this.game.world.centerX){
          firePress=true;
      }
    }

   //check pointer 2
    if(this.game.input.pointer2.isDown) {
      var targetX = this.game.input.pointer2.position.x;
      //left side for player movement
      if (targetX < this.game.world.centerX){          
        var targetY = this.game.input.pointer2.position.y;  
        direction = targetY >= this.game.world.centerY ? 1 : -1;
      }         
      //right side for fire bullet
        if (targetX > this.game.world.centerX){
          firePress=true;
      }
    }

     //move and or fire bullet 
      if (direction != 0 && !firePress){ //just move
        this.player.body.velocity.y = direction * this.PLAYER_SPEED;  
      }else if(direction !=0 && firePress){ //move and fire
         this.player.body.velocity.y = direction * this.PLAYER_SPEED; 
         this.createPlayerBullet();
      }else if (direction == 0 && firePress ){ //just fire
         this.createPlayerBullet(); 
      }

Link to comment
Share on other sites

I added debug code it appears my phone (Moto x 2nd gen) thinks pointer 1 and 2 are the same finger even with two fingers on screen? It shows coordinates for just one finger and rapidly flips the ID between 1 and 2.

 

   this.game.debug.pointer(this.game.input.pointer2);
      this.game.debug.pointer(this.game.input.pointer1);

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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