Jump to content

Problem with touch screen


Farhang
 Share

Recommended Posts

hi everyone

I'm going to build a simple game with phaser. there's a ball which you can click on it and hold for seconds, longer you hold longer ball throws when you release click.
here's my code wich works fine with pc and mouse but not working with mobile touch screen. I don't understand what's the problem and why it's not working on touch screen(android):

function create() { 
    ball = game.add.sprite(32, game.world.height - 100, 'ball');
    game.physics.arcade.enable(ball);
    ball.body.bounce.y = 0.2;
    ball.body.gravity.y = 400;
    ball.inputEnabled = true;
}

var power = 0;

function update() {
    if(ball.input.downDuration()>0){
        power = ball.input.downDuration();
    }

    //if(star.input.pointerUp()==true && power > 0) {
    if(game.input.activePointer.isDown==false && power > 0) {
        ball.body.velocity.x = power*2;
        ball.body.velocity.y = -400;
        power=0
    }
}

Edit:

I changed my code as below still not working on touch screen devices but works fine with mouse:

function create() { 
    ball = game.add.sprite(32, game.world.height - 100, 'ball');
    game.physics.arcade.enable(ball);
    ball.body.bounce.y = 0.2;
    ball.body.gravity.y = 400;

    game.input.onUp.add(onBallDownHold, this);
}

var power = 0;
function update() {

    if(ball.input.downDuration()>0)
        power = star.input.downDuration();

    if(game.input.activePointer.isDown==false && power > 0) {   
        power=0;
    }
}
function onBallDownHold(pointer) {
    if( power > 0) {       
        ball.body.velocity.x = pointer.duration * 2;
        ball.body.velocity.y = -400;
    }
}

 

 

 

Link to comment
Share on other sites

I found the solution:

ball.input.downDuration() doesn't work on touch screen so we need to use "game.input.activePointer.duration" along with  "ball.input.checkPointerOver(game.input.activePointer)".

below code works on touch screen

function create() { 
    ball = game.add.sprite(32, game.world.height - 100, 'ball');
    game.physics.arcade.enable(ball);
    ball.body.bounce.y = 0.2;
    ball.body.gravity.y = 400;
    ball.inputEnabled = true;
}

var power = 0;
function update() {

    if(game.input.activePointer.duration>0 && ball.input.checkPointerOver(game.input.activePointer)) {
        power = game.input.activePointer.duration;   
    }

    if(game.input.activePointer.isDown==false && power > 0) {
        power=0;
        ball.body.velocity.x = power*0.2;
        ball.body.velocity.y = -400;
}

Although I found a solution to get sprite click duration, still don't understand why sprite.input.downDuration() doesn't work on touch screen.

 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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