Jump to content

PIXI.Loader() not working in React.js - Need Help!


kule90
 Share

Recommended Posts

I am having hard times to make pixi.js work in react...
Can someone explain me how to get loader in react component?
I have tried withconst pixiApp = useApp();
and const loader = new PIXI.Loader();

but I am unable to get loader - it does not exists.

here is my component where whole point of it is to render images from .json file- spritesheet:

 

import React, { useEffect, useState } from "react";
import { AnimatedSprite, useApp } from "@pixi/react";
import * as PIXI from "pixi.js";
import HeroJSON from "../assets/sprites/hero/hero.json";

const AnimatedSpriteComponent = ({ xPropPos, yPropPos }) => {
  const [frames, setFrames] = useState([]);
  const pixiApp = useApp();

  console.log("pixiApp: ", pixiApp);

  const spritesheet = HeroJSON;

  console.log(" spritesheet: ", spritesheet);

  useEffect(() => {
    const loadTextures = async () => {
      const loadedFrames = [];

      await new Promise((resolve) =>
        pixiApp.loader.add(spritesheet).load((_, resource) => {
          const frames = Object.keys(resource[spritesheet].data.frames).map(
            (frame) => PIXI.Texture.from(frame)
          );
          loadedFrames.push(...frames);
          resolve();
        })
      );

      setFrames(loadedFrames);
    };

    loadTextures();
  }, [pixiApp.loader, spritesheet, pixiApp]);

  if (frames.length === 0) {
    return null;
  }
  return (
    <AnimatedSprite
      anchor={0.5}
      textures={frames}
      isPlaying={true}
      initialFrame={0}
      animationSpeed={0.1}
      width={100}
      height={70}
      x={xPropPos}
      y={yPropPos}
    />
  );
};

export default AnimatedSpriteComponent;

here is console log form 8th line:

 

pixiApp:  
_Application2 {stage: _Container2, renderer: _Renderer2, _ticker: _Ticker2, stop: ƒ, start: ƒ, …}
cancelResize:null
queueResize:null
renderer:null
resize:null
stage:null
start:() => { this._ticker.start(); }
stop:() => { this._ticker.stop(); }
_resizeId:null
_resizeTo:null
_ticker:null

so there is no loader, and everything is empty?

Link to comment
Share on other sites

  • 3 months later...

You shall not import json file and provide it to loader. Instead put your json file into "public" folder of your project. You can create same path in "public" folder to save import path as is. Then put your import string into loader.add function. It will be look like this 

loader.add("sprite", "./assets/sprites/hero/hero.json")
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...