繁体   English   中英

Phaser3 图像资源未加载 | 404(未找到)

[英]Phaser3 Image Assets Not Loading | 404 (Not Found)

我正在按照本教程使用 Phtomstorm 的样板构建多场景游戏模板。 我在 GameScene.js 的 preload() function 中添加的徽标图像收到 404。 我觉得我已经按照 T 的说明进行操作,但可能没有正确公开 webpack 中的资产,因为我是这个工具的新手。

让这个 logo.png 显示在屏幕上我做错了什么?

教程 - https://phasertutorials.com/creating-a-phaser-3-template-part-1/

项目模板 - https://github.com/photonstorm/phaser3-project-template.git

索引.js

import Phaser from "phaser";
import config from './Config/config';
import GameScene from './Scenes/GameScene';
 
class Game extends Phaser.Game {
  constructor () {
    super(config);
    this.scene.add('Game', GameScene);
    this.scene.start('Game');
  }
}
 
window.game = new Game();

配置.js

import 'phaser';
 
export default {
  type: Phaser.AUTO,
  parent: 'phaser-example',
  width: 800,
  height: 600
};

GameScene.js

import 'phaser';
 
export default class GameScene extends Phaser.Scene {
  constructor () {
    super('Game');
  }
 
  preload () {
    // load images
    this.load.image('logo', 'assets/logo.png');
  }
 
  create () {
    this.add.image(400, 300, 'logo');
  }
};

Webpack base.js

const webpack = require("webpack");
const path = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const { CleanWebpackPlugin } = require("clean-webpack-plugin");
module.exports = {
  mode: "development",
  devtool: "eval-source-map",
  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        use: {
          loader: "babel-loader"
        }
      },
      {
        test: [/\.vert$/, /\.frag$/],
        use: "raw-loader"
      },
      {
        test: /\.(gif|png|jpe?g|svg|xml)$/i,
        use: "file-loader"
      }
    ]
  },
  plugins: [
    new CleanWebpackPlugin({
      root: path.resolve(__dirname, "../")
    }),
    new webpack.DefinePlugin({
      CANVAS_RENDERER: JSON.stringify(true),
      WEBGL_RENDERER: JSON.stringify(true)
    }),
    new HtmlWebpackPlugin({
      template: "./index.html"
    })
  ]
};

在此处输入图像描述

npm 开始

> phaser3-project-template@1.1.1 start
> webpack-dev-server --config webpack/base.js --open

i 「wds」: Project is running at http://localhost:8081/
i 「wds」: webpack output is served from /
i 「wds」: Content not from webpack is served from C:\Users\xeonp\Documents\phaser.io\phaser3-project-template
Browserslist: caniuse-lite is outdated. Please run next command `npm update`
i 「wdm」: wait until bundle finished: /
i 「wdm」: Hash: a401be8d3773943c3f38
Version: webpack 4.41.2
Time: 2313ms
Built at: 12/06/2020 9:30:36 AM
     Asset       Size  Chunks             Chunk Names
index.html  158 bytes          [emitted]
   main.js   17.2 MiB    main  [emitted]  main
Entrypoint main = main.js
[0] multi (webpack)-dev-server/client?http://localhost:8081 ./src 40 bytes {main} [built]
[./node_modules/ansi-html/index.js] 4.16 KiB {main} [built]
[./node_modules/phaser/dist/phaser.js] 5.99 MiB {main} [built]
[./node_modules/strip-ansi/index.js] 161 bytes {main} [built]
[./node_modules/webpack-dev-server/client/index.js?http://localhost:8081] (webpack)-dev-server/client?http://localhost:8081 4.29 KiB {main} [built]
[./node_modules/webpack-dev-server/client/overlay.js] (webpack)-dev-server/client/overlay.js 3.51 KiB {main} [built]
[./node_modules/webpack-dev-server/client/socket.js] (webpack)-dev-server/client/socket.js 1.53 KiB {main} [built]
[./node_modules/webpack-dev-server/client/utils/createSocketUrl.js] (webpack)-dev-server/client/utils/createSocketUrl.js 2.91 KiB {main} [built]
[./node_modules/webpack-dev-server/client/utils/log.js] (webpack)-dev-server/client/utils/log.js 964 bytes {main} [built]
[./node_modules/webpack-dev-server/client/utils/reloadApp.js] (webpack)-dev-server/client/utils/reloadApp.js 1.59 KiB {main} [built]
[./node_modules/webpack-dev-server/client/utils/sendMessage.js] (webpack)-dev-server/client/utils/sendMessage.js 402 bytes {main} [built]
[./node_modules/webpack/hot sync ^\.\/log$] (webpack)/hot sync nonrecursive ^\.\/log$ 170 bytes {main} [built]
[./src/Config/config.js] 113 bytes {main} [built]
[./src/Scenes/GameScene.js] 2.71 KiB {main} [built]
[./src/index.js] 2.1 KiB {main} [built]
    + 21 hidden modules
Child html-webpack-plugin for "index.html":
     1 asset
    Entrypoint undefined = index.html
    [./node_modules/html-webpack-plugin/lib/loader.js!./index.html] 313 bytes {0} [built]
    [./node_modules/lodash/lodash.js] 530 KiB {0} [built]
    [./node_modules/webpack/buildin/global.js] (webpack)/buildin/global.js 472 bytes {0} [built]
    [./node_modules/webpack/buildin/module.js] (webpack)/buildin/module.js 497 bytes {0} [built]
i 「wdm」: Compiled successfully.
i 「wdm」: Compiling...
i 「wdm」: Hash: 210a30ea65002a251ebc
Version: webpack 4.41.2
Time: 91ms
Built at: 12/06/2020 9:30:55 AM
     Asset       Size  Chunks             Chunk Names
index.html  158 bytes          [emitted]
   main.js   17.2 MiB    main  [emitted]  main
Entrypoint main = main.js
[./src/index.js] 2.1 KiB {main} [built]
    + 35 hidden modules
Child html-webpack-plugin for "index.html":
     1 asset
    Entrypoint undefined = index.html
       4 modules
i 「wdm」: Compiled successfully.
i 「wdm」: Compiling...
i 「wdm」: Hash: 46d83d562089e6944d44
Version: webpack 4.41.2
Time: 118ms
Built at: 12/06/2020 9:31:11 AM
     Asset       Size  Chunks             Chunk Names
index.html  158 bytes          [emitted]
   main.js   17.2 MiB    main  [emitted]  main
Entrypoint main = main.js
[./src/index.js] 2.08 KiB {main} [built]
    + 35 hidden modules
Child html-webpack-plugin for "index.html":
     1 asset
    Entrypoint undefined = index.html
       4 modules
i 「wdm」: Compiled successfully.

在此处输入图像描述

在此处输入图像描述

用下面的代码替换 preload function 就可以正常工作了...

preload () {
    // load images
    this.load.image('logo', require('../assets/logo.png'));
  }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM