无法导入JSON Spritesheet

时间:2019-11-29 13:07:41

标签: javascript typescript webpack pixi.js

我正在尝试用PixiJS制作游戏,但无法导入Spritesheet。 我收到的错误是:

Uncaught TypeError: Cannot read property 'scale' of undefined

由于某种原因,当我尝试将图像制作为Spritesheet时会发生这种情况。此错误在浏览器上发生。 花了几个小时来搜索,但仍然无法正常工作。

Webpack配置。我读到json应该正确捆绑,没有任何加载器:

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

module.exports = {
    mode: 'development',
    entry: './src/index.ts',
    output: {
        filename: '[name].bundle.js',
        path: path.resolve(__dirname, 'dist'),
      },
    plugins: [
        new CleanWebpackPlugin(),
        new HtmlWebpackPlugin({
            title: 'Cool website',
        }),
      ],
    module: {
        rules: [
            {
                test: /\.tsx?$/,
                use: 'ts-loader',
                exclude: /node_modules/,
            },
            {
                test: /\.css$/,
                use: [
                    'style-loader',
                    'css-loader',
                ]
            },
            {
                test: /\.(png|svg|jpg|gif)$/,
                use: [
                    'file-loader',
                ],
            },
            {
                test: /\.(glsl|vs|fs)$/,
                loader: 'shader-loader',
                options: {
                    glsl: {
                        chunkPath: path.resolve("/glsl/chunks")
                    }
                }
            }
        ],
    },
    resolve: {
        extensions: [ '.tsx', '.ts', '.js' ],
    },
    devServer: {
        port: 9999
    },
    devtool: 'inline-source-map',
};

index.ts:

// all the setup

const loader = new Loader();

loader.add('hero', heroImage);

const sprites: { [name: string]: Sprite } = {};

loader.load((loader, resources) => {
    const sheet = new Spritesheet(resources.hero.texture.baseTexture, heroSprite); //error happens here

    const animatedSprite = new AnimatedSprite(sheet.animations['idle']);
    app.stage.addChild(animatedSprite);

    app.ticker.add(loop);
});
// loop function

1 个答案:

答案 0 :(得分:0)

解决了。 刚刚使用copy-webpack-plugin并复制了所有资产。然后像导入PixiJS文档loader.add('hero', '/assets/hero/hero.json');

一样导入它