Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent an animation stop on texture change #90

Merged
merged 1 commit into from
Aug 15, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions src/components/Character.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { BaseTexture, ISpritesheetData, Spritesheet } from 'pixi.js';
import { useState, useEffect } from 'react';
import { useState, useEffect, useRef } from 'react';
import { AnimatedSprite, Container, Text } from '@pixi/react';
import * as PIXI from 'pixi.js';

Expand Down Expand Up @@ -47,12 +47,21 @@ export const Character = ({
void parseSheet();
}, []);

if (!spriteSheet) return null;

// The first "left" is "right" but reflected.
const roundedOrientation = Math.round(orientation / 90);
const direction = ['left', 'up', 'left', 'down'][roundedOrientation];

// Prevents the animation from stopping when the texture changes
// (see https://github.com/pixijs/pixi-react/issues/359)
const ref = useRef<PIXI.AnimatedSprite | null>(null);
useEffect(() => {
if (isMoving) {
ref.current?.play();
}
}, [direction, isMoving]);

if (!spriteSheet) return null;

return (
<Container x={x} y={y} interactive={true} pointerdown={onClick}>
{isThinking && (
Expand All @@ -64,6 +73,7 @@ export const Character = ({
<Text x={18} y={-10} scale={0.8} text={'💬'} anchor={{ x: 0.5, y: 0.5 }} />
)}
<AnimatedSprite
ref={ref}
isPlaying={isMoving}
textures={spriteSheet.animations[direction]}
animationSpeed={speed}
Expand Down