MZ PIXI.AnimatedSprite.play() always returns "TypeError: Cannot read property '_uvs' of undefined"

Hello, everyone. I'm trying to play a set of images, I used the following code:
JavaScript:
const _Create_Background = Scene_Title.prototype.createBackground;
Scene_Title.prototype.createBackground = function() {
    _Create_Background.apply(this, arguments);
  
    const titleCommandButtonCursorSpriteTextureArray = [];
    //titleCommandButtonCursorSpriteRect and titleCommandButtonCursorSpriteJsonObject is obtained from the plugin parameter
    for (const image of titleCommandButtonCursorSpriteJsonObject) {
        console.log("image: ", "img/" + image + ".png")
        let texture = PIXI.Texture.from("img/" + image + ".png");
        titleCommandButtonCursorSpriteTextureArray.push(texture);
    }
  
    let animatedSprite = new PIXI.AnimatedSprite(titleCommandButtonCursorSpriteTextureArray);
    animatedSprite.animationSpeed = 1;
    animatedSprite.loop = true;
    animatedSprite.x = titleCommandButtonCursorSpriteRect.x;
    animatedSprite.y = titleCommandButtonCursorSpriteRect.y;
    animatedSprite.width = titleCommandButtonCursorSpriteRect.width;
    animatedSprite.height = titleCommandButtonCursorSpriteRect.height;
    animatedSprite.onComplete = () => {
          
    };
    animatedSprite.gotoAndPlay(0);
    console.log("animatedSprite: ", animatedSprite)
    this.addChild(animatedSprite);
};
If I don't use play() or gotoAndPlay(), the first image is displayed normally. I'm very new to pixi.js and I don't know what's happening with it. Here is my error log:
SharedScreenshot.jpg

SharedScreenshot2.jpg

If play() is used, _texture will be undefined, otherwise _texture will be the first image. I don't know why I can't use play(), I've searched the internet for a long time but couldn't find an answer, does anyone know what's going on? Any advice is appreciated!
What was the command you typed in the console? Is it supposed to be Play() capitalized? You could also be running the Play() function on the wrong type of object.
If I'm not mistaken, the image you want to call is in an array object, as far as I know if you want to call each image in the array you have to use a for loop first.
The error seems to be saying there's an undefined texture for the current frame. Perhaps check your JSON data. Also, I think you can construct the sprite more concisely using PIXI.AnimatedSprite.fromImages([...urls]) (but that shouldn't change how it works).

It is play, not Play (camelCase methods as usual):
But you've used gotoAndPlay there, and I don't see any obvious mistakes.

Perhaps this example will help:


Instead of PIXI's AnimatedSprite, have you considered following the core scripts' example, i.e. update the sprite/frame via an appropriate update method? That would also ensure the animation keeps pace with the core engine's fluid timestep process, and allow interfacing with the engine's image cache.
That error seems to usually be caused by there being something wrong with the textures - maybe there's a mistake in the list of filenames? The code you've shown appears to be correct.

And the reason for the _texture field being empty is simply because animated sprites don't use it - if you look a bit lower, the _textures field appears to be filled just fine.

EDIT: To think of it, I'm not entirely sure if the texture field is supposed to be empty - I think it would make sense that it's not needed when you have a series available, and it's only here because it's inherited, but the above message made it sound like I'm certain of it.
@Mac15001900 - I was going off lines 41651~41655 of pixi.js (MZ v1.8.0):
JavaScript:
	        this._texture = this._textures[currentFrame];
	        this._textureID = -1;
	        this._textureTrimmedID = -1;
	        this._cachedTint = 0xFFFFFF;
	        this.uvs = this._texture._uvs.uvsFloat32;
Doesn't look like _texture would be empty for a valid frame.
@Mac15001900 - I was going off lines 41651~41655 of pixi.js (MZ v1.8.0):
JavaScript:
            this._texture = this._textures[currentFrame];
            this._textureID = -1;
            this._textureTrimmedID = -1;
            this._cachedTint = 0xFFFFFF;
            this.uvs = this._texture._uvs.uvsFloat32;
Doesn't look like _texture would be empty for a valid frame.
Oh, that code is interesting. So the error seems to indeed be caused directly by _texture being undefined, but that only seems to be possible if currentFrame is an index that doesn't exist - the screenshot shows the array only contains Texture objects. But that sounds like that really shouldn't ever happen if you don't manually mess with these...

Ok, I have an idea. @Arrose , can you try to remove animatedSprite.gotoAndPlay(0);, create some global reference to the animatedSprite, and then manually invoke this function from the console after the sprite is loaded in? My guess is that is might be a problem with iterating over textures before the files have been loaded.
Oh, that code is interesting. So the error seems to indeed be caused directly by _texture being undefined, but that only seems to be possible if currentFrame is an index that doesn't exist - the screenshot shows the array only contains Texture objects. But that sounds like that really shouldn't ever happen if you don't manually mess with these...

Ok, I have an idea. @Arrose , can you try to remove animatedSprite.gotoAndPlay(0);, create some global reference to the animatedSprite, and then manually invoke this function from the console after the sprite is loaded in? My guess is that is might be a problem with iterating over textures before the files have been loaded.
Thanks for the replies!

@caethyril, I've actually tried fromImages and fromFrames, and I've read the documentation you've posted, but I'm interested in the core scripts' example you're talking about, can you tell me which file it's in? I'm very new to MZ's source code.

@Mac15001900, to make it easier to use play() on the console, I changed my code to look like the following:
JavaScript:
const titleCommandButtonCursorSpriteTextureArray = [];
for (const image of titleCommandButtonCursorSpriteJsonObject) {
    console.log("image: ", "img/" + image + ".png")
    let texture = PIXI.Texture.from("img/" + image + ".png");
    titleCommandButtonCursorSpriteTextureArray.push(texture);
}

 
this.animatedSprite = new PIXI.AnimatedSprite(titleCommandButtonCursorSpriteTextureArray);
this.animatedSprite.animationSpeed = 1;
this.animatedSprite.loop = true;
this.animatedSprite.x = titleCommandButtonCursorSpriteRect.x;
this.animatedSprite.y = titleCommandButtonCursorSpriteRect.y;
this.animatedSprite.width = titleCommandButtonCursorSpriteRect.width;
this.animatedSprite.height = titleCommandButtonCursorSpriteRect.height;
this.animatedSprite.onComplete = () => {
     
};
//animatedSprite.gotoAndPlay(0);
console.log("animatedSprite: ", this.animatedSprite)
this.addChild(this.animatedSprite);

Using play() in the console like you said, I find that the animation plays fine, but once I close the DevTools, I get the same error as before: "TypeError: cannot read property '_uvs' of undefined". We seem to have discovered something strange.
SharedScreenshot.jpg


Edit: What actually happens is that when I open DevTools and use play() in console, I can see the animation, but as soon as I put the game screen to the top (i.e., when the game screen gets the focus? it's the same as when I close DevTools), I get the same error as before!


Edit: Really appreciate all the help! I found a way to do it here: https://github.com/pixijs/pixijs/issues/4221#issuecomment-322033324, now the animation plays correctly, but I don't know why I need PIXI.Container(), here's the code I ended up modifying:
JavaScript:
let testContainer = new PIXI.Container(); // need container Befor ! ***********
testContainer.addChild(this.animatedSprite);
this.addChild(testContainer);

Edit: But there is a new problem, when I set animationSpeed to 0.1, the animation is normal and the vertexData is the value of the texture at the first time I enter the page, but if I set animationSpeed to 1, when I enter the page for the first time, the vertexData becomes very large and my animation becomes very large:
SharedScreenshot.jpg

SharedScreenshot2.jpg

So far I don't know why this is happening. All will return to normal after entering the other scene and then returning to enter the title scene a second time.


Edit: Well, I've done this now and it looks like it's okay, but I'm not sure:
JavaScript:
this.animatedSprite.onFrameChange = function(frame){
    //This is where you determine if the coordinates and size of the sprite are the same as the texture.
    this.anchor.set(0);
    this.x = titleCommandButtonCursorSpriteRect.x;
    this.y = titleCommandButtonCursorSpriteRect.y;
    this.width = titleCommandButtonCursorSpriteRect.width;
    this.height = titleCommandButtonCursorSpriteRect.height;
}
Edit: What actually happens is that when I open DevTools and use play() in console, I can see the animation, but as soon as I put the game screen to the top (i.e., when the game screen gets the focus? it's the same as when I close DevTools), I get the same error as before!
Ah, I think I see the problem now! It's specific to RPG Maker.
Spoiler: long explanation
By default RMMZ sets itself to inactive while the window is unfocused. While inactive, the game loop is basically paused, so the active scene does not update itself or its children.

The update method of PIXI.AnimatedSprite expects a deltaTime argument, but core RMMZ calls the update methods for each scene child without an argument. Looking at the code for the animated sprite update method, we see that deltaTime ultimately affects the value of the sprite's _currentTime value. So if deltaTime is undefined, _currentTime will become undefined, causing the next texture lookup to return undefined.

By default PIXI.Container does not automatically update its children. So putting the animated sprite in its own container basically intercepts the unwanted animated sprite update.
Not sure about the animation speed thing, though. Might be related. 😴
Ah, I think I see the problem now! It's specific to RPG Maker.
Spoiler: long explanation
By default RMMZ sets itself to inactive while the window is unfocused. While inactive, the game loop is basically paused, so the active scene does not update itself or its children.

The update method of PIXI.AnimatedSprite expects a deltaTime argument, but core RMMZ calls the update methods for each scene child without an argument. Looking at the code for the animated sprite update method, we see that deltaTime ultimately affects the value of the sprite's _currentTime value. So if deltaTime is undefined, _currentTime will become undefined, causing the next texture lookup to return undefined.

By default PIXI.Container does not automatically update its children. So putting the animated sprite in its own container basically intercepts the unwanted animated sprite update.
Not sure about the animation speed thing, though. Might be related. 😴
I think you're right, but I thought the "core scripts' example" was interesting.

The animation speed issue, I changed it directly in onFrameChange, and it's been fine since then, but I don't know if there's any unknown pitfalls yet.
Oh, sorry!
I'm interested in the core scripts' example you're talking about, can you tell me which file it's in?
I meant the general approach used throughout the core engine, used for character sprites, MV-style battle animations, state overlays, animated tiles, etc. The idea is to manually swap out the sprite's bitmap in an appropriate update method. As mentioned, the core engine calls update on all scene children every frame.

For an example, take a look at Game_CharacterBase.prototype.updateAnimation (rmmz_objects.js).

[Edit: ...and Sprite_Character.prototype.updateBitmap (rmmz_sprites.js) which actually updates the bitmap based on the character. 😅]

Original thread: https://forums.rpgmakerweb.com/threads/pixi-animatedsprite-play-always-returns-typeerror-cannot-read-property-_uvs-of-undefined.165382/