Phaser:制作背景屏幕宽度而不是内容宽度

Bil*_*ill 2 android phaser-framework

我正在使用Phaser Framework for Android制作游戏.我需要让画布填充整个屏幕宽度,然后用背景颜色填充它,然后将内容放在屏幕的中心.目前,即使我已经将画布的宽度设置为窗口宽度,Phaser仍将其重置为内容宽度,该宽度较小,然后仅使用背景填充内容宽度.

如何阻止Phaser使用内容宽度?

var gameObj = new Phaser.Game($(".parent").width(), 700, Phaser.CANVAS, 'parent');

gameObj.stage.backgroundColor = '#A6E5F5';
gameObj.load.image('prld', 'prl.png');

gameObj.scale.scaleMode = Phaser.ScaleManager.SHOW_ALL;
gameObj.scale.pageAlignHorizontally = true;
gameObj.scale.pageAlignVertically = true;
gameObj.scale.setScreenSize(true);
gameObj.scale.refresh();

gameObj.stage.backgroundColor = '#A6E5F5';    

// Load Content Here
gameObj.load.image('h1', 'res1/h1.png');
gameObj.load.image('h2', 'res1/h2.png');
....
g = gameObj.add.group();
g.create(20, 20, 'h1');
g.create(60, 20, 'h2');
....
Run Code Online (Sandbox Code Playgroud)

Pho*_*orm 8

你需要Phaser 2.2+,但是如果你想让画布填满整个屏幕空间,你可以这样做:

var game = new Phaser.Game("100%", "100%", Phaser.CANVAS, 'parent');

...

game.scale.scaleMode = Phaser.ScaleManager.RESIZE;
Run Code Online (Sandbox Code Playgroud)

如果您使用100%尺寸,也不应使用pageAlign.您也不需要调用refresh或setScreenSize.

在RESIZE缩放模式下工作时,您可以向状态添加一个名为"resize"的函数.只要屏幕尺寸发生变化(可能是方向事件),就会调用它,并传递2个参数:宽度和高度.使用它们来调整游戏内容布局.

你可能想要获得Phaser Scale Manager的书,它更详细地介绍了所有这些(警告:我写了它,但它有一个"付你想要的"价格,包括完全免费)


Sky*_*ker 6

我在我的游戏中使用此代码,它可以很好地工作,而不会改变任何元素的位置

var targetWidth = 720; // the width of the game we want
var targetHeight = 480; // the height of the game we want

// additional ratios

//Small – 360x240
//Normal – 480x320
//Large – 720x480
//XLarge – 960x640
//XXLarge – 1440x960

var deviceRatio = (window.innerWidth/window.innerHeight); //device aspect ratio

var newRatio = (targetHeight/targetWidth)*deviceRatio; //new ratio to fit the screen

var newWidth = targetWidth*newRatio;
var newHeight = targetHeight;

var gameWidth = newWidth;
var gameHeight = newHeight;
var gameRendrer = Phaser.AUTO;
Run Code Online (Sandbox Code Playgroud)

这个代码用于Boot状态

game.scale.scaleMode = Phaser.ScaleManager.SHOW_ALL;

        game.scale.pageAlignHorizontally = true;
        game.scale.pageAlignVertically = true;
        game.scale.forceLandscape = true;
        game.scale.setScreenSize(true);
Run Code Online (Sandbox Code Playgroud)

这适用于横向模式,如果你想让它像肖像更改宽度和高度一样

var targetWidth = 480; // the width of the game we want
var targetHeight = 720; // the height of the game we want
Run Code Online (Sandbox Code Playgroud)

也迫使它成为肖像

这段代码在我的游戏中运行良好 移相器屏幕截图

在此输入图像描述