检测PhaserJS中的doubleTap

时间:2018-04-19 10:01:53

标签: phaser-framework

我是PhaserJS的全新手。我正在尝试在PhaserJS中开发一个游戏,其中点击屏幕一次应该使用户头像在屏幕上移动并且双击应该使其跳跃。但是,我无法弄清楚如何捕捉双击事件。我在他们的官方文档中看到了这个tutorial,但是没有用。当我把这一行

game.input.onTap.add(onTap, this);

中的

function create(){
    .....
} 

功能,显示此错误

Uncaught TypeError: Cannot read property 'add' of undefined

我可以使用以下代码检测单击事件:

if(game.input.onTap && player.body.touching.down){
    player.setVelocity(-530);
}

我已经阅读了另一个线程,其中成员已经评论说要检测双击,应该启动计时器并检查点击是否在阈值内。但是,我认为这不是一种万无一失的方法,必须有更好的方法。有人可以帮忙吗?

编辑: 相关代码:

<script type="text/javascript">
var width = 800;
var height = 600;

var config = {
    type: Phaser.AUTO,
    width: width,
    height: height,
    physics: {
        default: 'arcade',
        arcade: {
            gravity: { y:300 },
            debug: false
        }
    },
    scene: {
        preload: preload,
        create: create,
        update: update
    }
};

var game = new Phaser.Game(config);

function preload (){
    this.load.image("sky", "{% static 'task_scheduler/assets/sky.png' %}");
    this.load.image("ground", "{% static 'task_scheduler/assets/platform.png' %}");
    this.load.image("star", "{% static 'task_scheduler/assets/star.png' %}");
    this.load.image("bomb", "{% static 'task_scheduler/assets/bomb.png' %}");
    this.load.spritesheet("dude", "{% static 'task_scheduler/assets/dude.png' %}", {frameWidth: 32, frameHeight: 48});
}

var platforms;
var player;
var cursors;
var stars;
var bombs;
var score = 0;
var scoreText;

function create (){
    //some other code here

    //mouse and touch
    this.input.onTap.add(onTap, this);
}

function onTap(pointer, doubleTap){
    if (doubleTap){
        player.setVelocityY(-530);
    }else{
        player.setVelocityX(160);
        player.anims.play('right', true);
    }
}

1 个答案:

答案 0 :(得分:1)

function create (){
    //some other code here

    //mouse and touch
    this.input.onTap.add(onTap, this);
}

在这里你应该使用game.input.onTap.add(onTap, this); 因为this将引用调用create函数的上下文。因此,this不具有属性add以及game变量可用的其他属性。

就Double Tap而言,相位器会检测到它。我认为你不需要为此编写任何明确的代码。 onTap函数可以正常工作。

相关问题