如何使用 Phaser 加载图像

How to load an image using Phaser

我正在尝试学习如何使用 Phaser,按照在线说明如何加载图像; Phaser 提供的示例之一是加载图像示例。

这是 Phaser 提供的代码。

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

function preload() {

    //  You can fill the preloader with as many assets as your game requires

    //  Here we are loading an image. The first parameter is the unique
    //  string by which we'll identify the image later in our code.

    //  The second parameter is the URL of the image (relative)
    game.load.image('einstein', 'assets/pics/ra_einstein.png');

}

function create() {

    //  This creates a simple sprite that is using our loaded image and
    //  displays it on-screen
    game.add.sprite(0, 0, 'einstein');

}

html

<html>
<head>
  <meta charset="UTF-8">
  <title>Experiments</title>
  <script src="../phaser.js"></script>
  <script src="game.js"></script>
</head>
<body>

</body>
</html>

我正在尝试 运行 我的服务器上的这段代码(使用节点),但我看不到图像,有人能解释一下我做错了什么吗?

尝试在 <body></body> 标签内添加 <div id="phaser-example"></div>

<html>
   <head>
       <meta charset="UTF-8">
       <title>Experiments</title>
       <script src="../phaser.js"></script>
       <script src="game.js"></script>
   </head>
   <body>
        <div id="phaser-example"></div>
   </body>
</html>

js:

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

function preload() {

    //  You can fill the preloader with as many assets as your game requires

    //  Here we are loading an image. The first parameter is the unique
    //  string by which we'll identify the image later in our code.

    //  The second parameter is the URL of the image (relative)
    game.load.image('einstein', 'assets/pics/ra_einstein.png');

}

function create() {

    //  This creates a simple sprite that is using our loaded image and
    //  displays it on-screen
    game.add.sprite(0, 0, 'einstein');

}