我得到了PreView.vue:88 PixiJS Warning: [Assets] Asset id /src/assets/liuxin/liuxin.png was not found in the Cache
(anonymous) @ PreView.vue:88
Promise.then
(anonymous) @ main.ts:26
Show 25 more frames
PreView.vue:89 PixiJS Warning: [Assets] Asset id /src/assets/liuxin/liuxin.png was not found in the Cache
PreView.vue:140 Failed to fetch model list: TypeError: Cannot read properties of undefined (reading 'source')
at new _Spritesheet2 (chunk-NQFDCHBS.js?v=53b38b7a:1280:34)
at PreView.vue:89:25
It seems you're encountering issues with loading assets using PixiJS in your Vue application. The warnings and errors you're seeing indicate that PixiJS is unable to locate or properly load your image and JSON files. Let's break down the problem and work towards a solution.
Error Analysis
Asset Not Found in Cache Warning:
code
PreView.vue:88 PixiJS Warning: [Assets] Asset id /src/assets/liuxin/liuxin.png was not found in the Cache
This warning suggests that PixiJS cannot find the image asset in its cache when trying to create a texture from it.
TypeError on Reading 'source':
code
PreView.vue:140 Failed to fetch model list: TypeError: Cannot read properties of undefined (reading 'source')
at new _Spritesheet2 (chunk-NQFDCHBS.js?v=53b38b7a:1280:34)
at PreView.vue:89:25
This error occurs when attempting to create a new Spritesheet. PixiJS tries to access the source property of an undefined object, indicating that the texture or data required for the spritesheet isn't properly loaded.
Possible Causes
Incorrect Asset Paths:
The paths to your image and JSON files might not be resolving correctly, leading to failed imports.
TypeScript Module Declarations:
TypeScript may not know how to handle imports of .png and .json files without proper module declarations.
Asynchronous Asset Loading:
PixiJS might be trying to use assets before they are fully loaded, especially if the assets are large or if there's a loading delay.
Solutions
1. Verify Asset Paths and Imports
Ensure that the paths to your assets are correct. In your code, you're importing assets like this:
typescript
import liuxinPath from '@/assets/liuxin/liuxin.png';
import liuxinPathJS from '@/assets/liuxin/liuxin.json';
Confirm that the files exist in the specified directory.
Check if the paths are correct relative to the @ alias, which usually points to the src directory in Vue projects.
2. Add TypeScript Declarations for Non-Code Assets
TypeScript needs to understand how to handle imports of image and JSON files. You can add custom type declarations to help TypeScript and your bundler (like Webpack or Vite) process these files.
Create a types directory in your src folder, and add a file named assets.d.ts:
When working with PixiJS, it's crucial that all assets are fully loaded before you attempt to use them.
Use PixiJS's Asset Loader:
Instead of direct imports, use PixiJS's Asset Loader to load your assets. This method ensures that assets are loaded asynchronously and are available in the PixiJS cache when needed.
typescript
import { Assets } from 'pixi.js';
onMounted(async () => {
// Load assets
const [liuxinTexture, liuxinData] = await Promise.all([
Assets.load('@/assets/liuxin/liuxin.png'),
fetch('@/assets/liuxin/liuxin.json').then((response) => response.json()),
]);
// Create spritesheet
const liuxinSheet = new Spritesheet(liuxinTexture, liuxinData);
await liuxinSheet.parse();
// Continue with your code to create sprites, etc.
});
Note: Adjust the paths in Assets.load and fetch to match your project's structure.
4. Verify the Content of Imported JSON
Make sure that the JSON file (liuxin.json) contains the correct data for your spritesheet.
Log the JSON Data:
typescript
console.log('liuxinPathJS:', liuxinPathJS);
This will help you verify that the data is correctly imported and contains the expected structure.
5. Adjust the PixiJS Application Initialization
Typically, when initializing a PixiJS Application, you pass the options directly without an init method.
Update Application Creation:
typescript
const app = new Application({
background: '#1099bb',
});
Remove the await app.init(...) line, unless you're using a custom application class that requires initialization.
6. Check for Asynchronous Data Fetching Issues
In your onMounted function, you're fetching a model list and then processing it:
typescript
const httpdata = await Pixihttp.get_model_list();
// ... process data ...
If httpdata is undefined or doesn't have the expected structure, accessing its properties will result in errors.
Ensure Pixihttp.get_model_list() Returns Data:
Verify that the get_model_list method is functioning correctly and returns the expected data structure.
Add Error Handling:
typescript
try {
const httpdata = await Pixihttp.get_model_list();
if (httpdata && httpdata.data) {
// Proceed with processing
} else {
console.error('Invalid data received:', httpdata);
}
} catch (error) {
console.error('Error fetching model list:', error);
}
Summary of Steps
Confirm asset paths and ensure that the files exist in the specified locations.
Add TypeScript declarations for .png and .json files.
Use PixiJS's Asset Loader to load assets asynchronously and ensure they're ready before use.
Verify that your JSON data is correctly imported and has the expected structure.
Simplify and correct the PixiJS application initialization if necessary.
Add proper error handling and verify data when fetching the model list.
Example Code Adjustment
Here's how you might adjust your onMounted function incorporating the above suggestions:
By following these steps, you should be able to resolve the asset loading issues and get your PixiJS application running smoothly within your Vue project.