如何在Facebook即时游戏中为移动设备创建Phaser按钮

时间:2018-05-11 19:33:19

标签: facebook phaser-framework

我正在尝试在移动设备上创建一个带有Phaser 2 CE的按钮,但它不会触发,即使它在桌面上工作正常,所以entiere代码将在my github repository上,但目前我可以像下面的代码一样显示图像

var game = new Phaser.Game(640, 480, Phaser.AUTO, 'game', { preload: preload, create: create, update: update });

function preload () {

  game.load.image('logo', 'assets/phaser2.png');
  game.load.image("upArrow", "assets/up.png");

}

function create () {
  // Initialize player
  //player = game.add.sprite(20, 20, 'logo');
  game.device.desktop || addMobileInputs();    
}

function addMobileInputs() {
  upButton = game.add.sprite(40, 40, "upArrow");
  upButton.inputEnabled = !0;
  upButton.events.onInputDown.add(myHandler, this);
}

function myHandler() {
  alert("up");
}

function update() {

}

enter image description here

1 个答案:

答案 0 :(得分:1)

似乎alert("...")没有触发,代码是正确的,因为如果你只是让你的精灵移动,它会移动,例如像下面的代码:

var game = new Phaser.Game(640, 480, Phaser.AUTO, 'game', { preload: preload, create: create, update: update });
var sprite;

function  preload () {
  // This is equivalent to <https://examples.phaser.io/assets/>.
  this.load.image('dude', 'assets/sprites/phaser-dude.png');
}

function create() {
  sprite = game.add.sprite(game.world.centerX, game.world.centerY, 'dude');
  sprite.inputEnabled = true;
  sprite.events.onInputDown.add(myHandler, this);
}

function myHandler() {
  sprite.x += 10;
}

function update() {
}