Facebook即时游戏中与Phaser一起使用的区域大小是多少?

时间:2018-05-11 22:25:07

标签: facebook phaser-framework

我正在使用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() {

}

1 个答案:

答案 0 :(得分:1)

进行搜索时,会出现一些相关问题https://stackoverflow.com/a/49034911/2107253 因此,您可以尝试使用此代码,因为您可以根据设备窗口的内部属性创建游戏,并使用SHOW_ALL进行缩放,根据名为 Phaser Scale Manager指南< / strong>你可以在这里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() {

}