Jump to content

React Animated Sprite


Iustin
 Share

Recommended Posts

Hi, guys!

I've searched before make this topic but I didn't find any solution for my problem. If there are more topics with this situation I will delete it after someone will help me. Thank you!

I'm new with pixiJs and I'm trying to make a poll game.
I have some animations to display, but in the same time I have some problem displaying them ?.

My problem and question is how I can use the images because when I start the project is looking for them in the root of  the aplication folder (e.g: "http://localhost:3000/image1.png") and if I don't have the image there, is showing me an error.
I have a folder inside src folder named assets and I want there to be the images, not on root folder (because I can't create the build and take the images too). And if is possible to use the json file from local too will be great (now is on a webserver). 

Please help me with this problem, i've tried it in so many ways and with no success.
Thank you! 

This is the piece of code: 

import React, { useEffect, useState } from "react";
import { AnimatedSprite, Container, useApp } from "@inlet/react-pixi";
import * as PIXI from "pixi.js";

const AnimatedAnswer = () => {
const [frames, setFrames] = useState([]);
const app = useApp();

const spritesheet = "https://pixijs.io/examples/examples/assets/spritesheet/fighter.json";

useEffect(() => {
app.loader.reset();
app.loader.add(spritesheet).load((_, resource) => {
setFrames(
Object.keys(resource[spritesheet].data.frames).map((frame) =>
PIXI.Texture.from(frame)
)
);
});
}, []);

if (frames.length === 0) {
return null;
}

return (
<AnimatedSprite
width={200}
height={150}
anchor={0.5}
animationSpeed={0.5}
isPlaying={true}
loop={false}
/>
);
};

export default AnimatedAnswer;

 

Link to comment
Share on other sites

how I can use the images because when I start the project is looking for them in the root of  the aplication folder

if you are trying to "require('src_path/image')" - it wont work. pixi loader does not have any integration with react, and so far i didnt see that anyone did it - people probably too lazy to share :)

>I have a folder inside src folder named assets and I want there to be the images

webpack copies that folder somewhere to public, right? wherever it copies assets - specify it for pixi loader.

Link to comment
Share on other sites

Thank you @ivan.popelyshevfor your reply. 
Someone help me with this solution: 

import React, { useEffect, useState } from "react";
import { AnimatedSprite, useApp } from "@inlet/react-pixi";
import * as PIXI from "pixi.js";

import frameNew1 from "../assets/frame_apngframe1.png";
import frameNew2 from "../assets/frame_apngframe2.png";
import frameNew3 from "../assets/frame_apngframe3.png";
import frameNew4 from "../assets/frame_apngframe4.png";
import frameNew5 from "../assets/frame_apngframe5.png";
import frameNew6 from "../assets/frame_apngframe6.png";
import frameNew7 from "../assets/frame_apngframe7.png";


const AnimatedAnswer = () => {
  const [frames, setFrames] = useState([]);
  const app = useApp();

  useEffect(() => {
    PIXI.utils.clearTextureCache();
    app.loader.reset();

    app.loader.add(frameNew1);
    app.loader.add(frameNew2);
    app.loader.add(frameNew3);
    app.loader.add(frameNew4);
    app.loader.add(frameNew5);
    app.loader.add(frameNew6);
    app.loader.add(frameNew7).load((_, resource) => {
      setFrames(
        Object.values(resource).map((frame) => PIXI.Texture.from(frame.url))
      );
    });
  }, []);

  if (frames.length === 0) {
    return null;
  }

  return (
	<AnimatedSprite
		width={200}
		height={150}
		anchor={0.5}
		animationSpeed={0.5}
		isPlaying={true}
		loop={false}
	/>
  );
 };
};

export default AnimatedAnswer;

It's took some imports and adds to the loader but in this way react knows where are the assets.

Edited by Iustin
Link to comment
Share on other sites

  • 2 weeks later...

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...