无法在 Phaser 3 游戏中加载精灵

时间:2021-07-20 19:39:59

标签: javascript game-physics phaser-framework

我尝试使用来自网络的链接和 PC 路径设置我的路径,但没有奏效,我的屏幕上曾经显示过这个绿色方块: enter image description here

我的 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 或路径没有任何作用,我只想在没有那个绿色方块的情况下在屏幕上显示我的精灵播放器,我看到了另一个问题,但没有给我提供解决方案,我该怎么办?

1 个答案:

答案 0 :(得分:2)

它似乎找不到要查找的图像,这意味着它不存在,或者更有可能在您提供的位置中不存在。

如果您从根目录提供此网页(也就是它在 http://localhosthttp://127.0.0.1 上可见,那么您可以尝试简单地在路径前添加一个 /,因此:/assets/worm.png,前导 / 表示它将从您网页的根目录搜索资产。