微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

我正在使用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');
....

解决方法:

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

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;

这个代码用于Boot状态

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

        game.scale.pageAlignHorizontally = true;
        game.scale.pageAlignVertically = true;
        game.scale.forceLandscape = true;
        game.scale.setScreenSize(true);

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

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

也迫使它成为肖像

这段代码在我的游戏中运行良好

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐