gpt4 book ai didi

javascript - 如何使用 Phaser 加载图像

转载 作者:行者123 更新时间:2023-11-28 19:25:18 28 4
gpt4 key购买 nike

我正在尝试学习如何使用 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>

我正在尝试在我的服务器上运行此代码(使用节点),但我没有看到图像,有人可以解释我做错了什么吗?

最佳答案

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

<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');

}

关于javascript - 如何使用 Phaser 加载图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27996228/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com