加载图片的结果是404(未找到) - webpack

问题描述 投票:1回答:1

这是错误的文字

enter image description here

我试图在Phaser 3.23.0的游戏中加载一些图片,图片是在下面文件中的函数preload()加载的。

主.js

import Phaser from 'phaser';

function preload() {
  this.load.image('sky', 'assets/sky.png');
  this.load.image('ground', 'assets/platform.png');
  this.load.image('star', 'assets/star.png');
  this.load.image('bomb', 'assets/bomb.png');
  this.load.spritesheet('dude',
    'assets/dude.png',
    { frameWidth: 32, frameHeight: 48 });
}

function create() {
  this.add.image(400, 300, 'sky');
}

function update() {
}

const config = {
  type: Phaser.AUTO,
  width: 800,
  height: 600,
  scene: {
    preload,
    create,
    update,
  },
};

// eslint-disable-next-line no-unused-vars
const game = new Phaser.Game(config);

const canvas = document.querySelector('canvas');

console.log(game);

我输入

npm run webpack-dev-server --mode development

并在localhost:8080启动webpack-dev-server。

然后我得到这些错误。

这些是控制台里的错误 Chrome浏览器。

enter image description here

这些是网络中的错误。Chrome浏览器

enter image description here

这些是控制台中的错误。火狐浏览器。

enter image description here

火狐浏览器没有任何网络活动。甚至没有一个请求。

index.html

    <!doctype html> 
    <html lang="en"> 
    <head> 
        <meta charset="UTF-8" />
        <title>Making your first Phaser 3 Game - Part 1</title>
        <style type="text/css">

        </style>
    </head>
    <body>
    </body>
    </html>

webpack.config.js

const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');


const config = {
  entry: './src/scripts/main.js',
  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        loader: 'babel-loader',
      },
      {
        test: /\.scss$/,
        use: [
          'style-loader',
          'css-loader',
          'sass-loader',
        ],
      },
      {
        test: /\.(jpe?g|png|gif|svg)$/i,
        use: {
          loader: 'file-loader?name=/src/assets/[name].[ext]',
          options: {
            name: '[name].[hash].[ext]',
            outputPath: 'assets/',
          },
        },
      },
      {
        test: /\.html$/i,
        loader: 'html-loader',
      },
    ],
  },
  output: {
    path: path.resolve(
      __dirname,
      'dist',
    ),
  },
  plugins: [
    new HtmlWebpackPlugin({
      title: 'Output Management',
      template: './src/index.html',
    }),
  ],
};

module.exports = (env, argv) => {
  if (argv.mode === 'development') {
    config.devtool = 'source-map';
    config.output.filename = 'main.bundle.js';
  }

  if (argv.mode === 'production') {
    config.plugins.push(new CleanWebpackPlugin());
    config.output.filename = 'main.[hash].bundle.js';
  }

  return config;
};

这个 是提出这个问题时的repo。

javascript webpack webpack-dev-server phaser
1个回答
1
投票

你有一个404。

更改你的 main.js 到。

function preload() {
  this.load.image('sky', require("../assets/sky.png").default);
  this.load.image('ground', require("../assets/platform.png").default);
  this.load.image('star', require("../assets/star.png").default);
  this.load.image('bomb', require("../assets/bomb.png").default);
  this.load.spritesheet('dude',
  require("../assets/dude.png").default,
    { frameWidth: 32, frameHeight: 48 });
}

现在执行。

yarn start / npm start
© www.soinside.com 2019 - 2024. All rights reserved.