Jump to content

How to use onInputDown for mobile touch


Regentix
 Share

Recommended Posts

Hello, i am new to phaser and wondering why the event i bound to my sprite doesn't work.

I am trying to make it so when you tap the sprite on mobile/desktop, it has an upwards velocity, but this is not working. 

I am following the docs/examples on how to do so.

This is my code:

var game = new Phaser.Game('100', '100', Phaser.AUTO, '', {preload: preload, create: create, update: update});
        var ball, counterText;

        function preload() {
            game.load.image('ball', 'assets/ball.png');
        }
        function create() {
            game.physics.startSystem(Phaser.Physics.ARCADE);
            game.stage.backgroundColor = '#F4F4F4';

            ball = game.add.sprite(game.world.width / 2 - 50 ,game.world.height / 2 - 100,'ball');

            game.physics.arcade.enable(ball);
            ball.body.bounce.y = 0.9;
            ball.body.gravity.y = 0;
            ball.body.collideWorldBounds = true;
            ball.inputEnabled = true;
            ball.events.onInputDown.add(onBallTap, this);

            counterText = game.add.text(game.world.centerX, 40, 'TEST', {fontSize: '40px', fill: '#fff'});
            counterText.anchor.setTo(0.5);
        }
        function update() {
            if (game.input.pointer1.isDown) {
                ball.body.gravity.y = 3000;
            }
            if (ball.body.onFloor()) {
                counterText.addColor(randomHexColor(), 0);
            }
        }
        function randomHexColor() {
            color = Math.random() * 0xffffff;
            hexColor = '#' + parseInt(color).toString(16);
            return hexColor;
        }
        function onBallTap() {
            ball.body.velocity.y = -1300;
        }

 

Link to comment
Share on other sites

Hey,

Delete the ' ' from the resolution specified.

var game = new Phaser.Game(100, 100, Phaser.AUTO ....

Delete #1 from update() and add #2 in create()

#1
if (game.input.pointer1.isDown) {
    ball.body.gravity.y = 3000;
}

#2
game.inputEnabled = true;
game.input.onDown.add(function () {
    ball.body.gravity.y = 3000;
}, this);

 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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