将Phaser移入HTML容器div中

19

目前正在使用Phaser制作一个基于浏览器的游戏,并尝试将其添加到我创建的容器

标签中,但是Phaser似乎将自己推到了容器
下面。

以下是两个截图供比较:

这是我HTML页面的代码:

    <!DOCTYPE html>
    <html>
    <head>
    <title>This is our menu bar!</title>

    <link rel="stylesheet" type="text/css" href="styles.css"/>

    </head>

    <body>


    <ul id="menu">
    <li><a href="file:///H:/AINT103/xampp/htdocs/Weekfour/index.html" class="highlight"><span>Home</span></a></li>  
    <li><a href="file:///H:/AINT103/xampp/htdocs/Weekfour/index.html"><span>Link1</span></a></li>   
    <li><a href="file:///H:/AINT103/xampp/htdocs/Weekfour/index.html"><span>Link2</span></a></li>   
    <li><a href="file:///H:/AINT103/xampp/htdocs/Weekfour/index.html"><span>Link3</span></a></li>   
    <li><a href="file:///H:/AINT103/xampp/htdocs/Weekfour/index.html"><span>Link4</span></a></li>   
    </ul>   


    <div class="container">

    <script rel="javascript" type="text/javascript" src="phaser.js"></script>
    <script rel="javascript" type="text/javascript" src="game.js"></script>


    <div class="header">
    Title of game & small talk about about with some a box surrounding this text before another text box surrounding the game below
    </div>

    <div class="instructions">
    </div>
    Instructions
    </div>

    <div id="footer">
     Copyright 2013 marc

    </div>

    </body>


    </html>

这是我的 CSS 代码:

    body {
    font-family: Century Gothic, Arial;
    background-color: #CCCCCC;
    margin: 0px auto;
    text-align: center;
    }
    .container {
    background-color: #999999;
    margin: auto;
    width: 1000px;
    height: 1000px;

    }
    .header {
    font-size: 22px;
    background-color: #999999;
    border: 1px dashed #666666;
    color: #444444;
    width: 800px;
    font-size: 14px;
    text-align: center;
    margin: 10px auto 0px auto;
    }   
    #menu {
    float: center;
    padding: 0px 10px;
    margin: 10px;
    border-bottom: 5px solid #6D7AB2;
    }

    #menu li {
    display: inline;
    list-style: none;
    margin: 0px;
    padding: 0px;
    }

    #menu li a {
    float: center;
    color: #000000;
    text-decoration: none;
    background: url('menuleft.gif') top left no-repeat;
    margin: 0px 0px;
    padding: 9px 0px 0px 9px;
    }

    #menu li a span {
    background: url('menuright.gif') top right no-repeat;
    padding: 9px 25px 6px 15px;
    }

    #menu li a:hover, 
    #menu li a.highlight {
    background: url('menuleft-hl.gif') top left no-repeat;
    color: #ffffff;
    }

    #menu li a:hover span, 
    #menu li a.highlight span {
    background: url('menuright-hl.gif') top right no-repeat;
    }


    canvas {
    padding: 0px;
    margin: auto;
    }

    #footer {
    clear:both;
    margin-top: 10px;
    border-top: 5px solid #6D7AB2;
    padding: 20px 0px;
    font-size: 80%;
    text-align:center;


    }

有人能帮助我指出我哪里出了问题吗?


请问您期望的输出是什么? - Amarnath Balasubramanian
我希望Phaser游戏能够在巨大的深灰色容器div中输出,就像<div class="header">一样。 - Marc Brooks
只是一个关于 OT 的注意事项,如果在 font-family 声明中有多个字体,您需要将包含空格的名称放在引号外面。 font-family: Century Gothic, Arial; -> font-family: 'Century Gothic', Arial; - aksu
3个回答

26

使用在phaser github上找到的示例 (https://github.com/photonstorm/phaser)

在HTML中:

<div id="phaser-example"></div>

在 .js 上

var game = new Phaser.Game(800, 600, Phaser.AUTO, 'phaser-example', { preload: preload, create: create, update: update, render: render });

在Phaser.Game()中的第四个参数是DOM元素的ID,你可以将Phaser创建的元素插入其中。


19

9
Phaser.Game构造函数的第四个参数是父容器。如果您将其留空,它将把画布注入到文档体中。如果您给出一个字符串,它将在该字符串上运行document.getElementById(),如果找到则将画布注入其中。如果您提供一个HTMLElement,则会跳过ID检查,并将画布注入该元素中。
因此,在您的情况下,我建议您创建一个带有ID的div,然后将该ID传递给构造函数。
更多详细信息请参见:http://gametest.mobi/phaser/docs/Phaser.Game.html

哇,你的回答中有一些我不知道的有用内容!感谢你指出文档链接。顺便问一下,如果我使用iframe呢?第四个参数是可选的吗? - Gokigooooks
如果您使用iframe,则通常将第四个参数留空,以便填充整个文档。确保您的iframe尺寸和游戏尺寸匹配或兼容。 - PhotonStorm

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接