微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决无法在 Phaser 3 游戏中加载精灵

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

解决方法

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

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。