javascript - 无法在 Phaser 3 游戏中加载精灵
问题描述
我尝试使用来自网络的链接和 PC 路径设置我的路径,但没有成功,我的屏幕上显示了这个绿色方块:
我的game.ejs
文件:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="//cdn.jsdelivr.net/npm/phaser@3.55.2/dist/phaser.js"></script>
<script src="//cdn.jsdelivr.net/npm/phaser@3.55.2/dist/phaser.min.js"></script>
<title>Document</title>
</head>
<body>
<script>
const config = {
type: Phaser.AUTO,
width: 800,
height: 600,
parent: 'phaser-example',
physics: {
default: 'arcade'
},
scene: {
preload: preload,
create: create,
update: update
}
};
var debug;
var source;
var target = new Phaser.Math.Vector2();
var distanceText;
new Phaser.Game(config);
function preload ()
{
this.load.image('worm', 'assets/worm.png');
}
function create ()
{
source = this.physics.add.image(100, 300, 'worm');
debug = this.add.graphics();
this.input.on('pointerdown', function (pointer) {
target.x = pointer.x;
target.y = pointer.y;
this.physics.moveToObject(source, target, 250);
}, this);
distanceText = this.add.text(10, 10, 'Click to set target', { fill: '#00ff00' });
}
function update ()
{
var distance = Phaser.Math.Distance.Between(source.x, source.y, target.x, target.y);
if (source.body.speed > 0)
{
distanceText.setText('Distância: ' + distance);
if (distance < 4)
{
source.body.reset(target.x, target.y);
}
}
}
</script>
</body>
</html>
就像我说的那样,我的电脑上的 url 或路径没有任何作用,我只想在没有那个绿色方块的情况下在屏幕上显示我的精灵播放器,我看到了另一个问题,但没有提供解决方案,我该怎么办?
解决方案
它似乎找不到它正在寻找的图像,这意味着它不存在,或者更可能它不存在于您提供的位置。
如果您从根目录提供此网页(也就是可见,http://localhost
或者http://127.0.0.1
您可以尝试/
在路径前简单地添加一个,例如:/assets/worm.png
,前导/
表示它将从您的网页的根目录搜索资产.