1

我正在 Phaser 2 CE 中编写游戏,因此实际代码如下所示https://stackoverflow.com/a/50300726/2107253但是当我在移动设备或桌面上打开游戏时,图像不会显示在中心,它是隐藏的

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

}
4

1 回答 1

1

当您进行搜索时,会出现一些相关问题https://stackoverflow.com/a/49034911/2107253 所以您可以尝试此代码它工作正常,因为您根据设备窗口的内部属性创建了一个游戏,并且您使用 SHOW_ALL 对其进行缩放,这可以根据名为“相位器缩放管理器指南”的书进行缩放,您可以在此处进行调整https://gumroad.com/photonstorm

//If you use PixelRatio, the sprite will be smaller according to the resolution of the mobile device

PixelW = window.innerWidth;// * window.devicePixelRatio
PixelH = window.innerHeight;// * window.devicePixelRatio
var game = new Phaser.Game(PixelW, PixelH, 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() {
  game.stage.backgroundColor = 0x3b5998;
  game.scale.scaleMode = Phaser.ScaleManager.SHOW_ALL;
  sprite = game.add.sprite(game.world.centerX, game.world.centerY, 'dude');
  sprite.inputEnabled = true;
  sprite.events.onInputDown.add(myHandler, this);
}

function myHandler() {
  sprite.anchor.setTo(0.5, 0.5);
  sprite.x = Math.floor(Math.random() * PixelW);
  sprite.y = Math.floor(Math.random() * PixelH);
}

function update() {

}
于 2018-05-11T23:37:37.873 回答