如何在Facebook即时游戏中为移动设备创建一个Phaser按钮[英] How to create a Phaser button in Facebook instant games for a mobile device

问题描述

我正在尝试在移动设备上使用Phaser 2 CE创建一个按钮,但是即使在桌面上工作正常,Entiere代码也不会触发,也将在我的github存储库但是,目前我可以在code中显示一个图像,例如代码bellow

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() {

}

在此处输入图像说明

推荐答案

看来alert("...") 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() {
}

本文地址:https://www.itbaoku.cn/post/1740209.html