MZ Plotting a sprite over the window.

Hi, I'm working on a plugin. The idea is to show an enemy picture in the little window on the top left part of the screen.

I tried using "$gameScreen.showPicture(1, enemyInfoData[ind].name, 0, 130, 40, 100, 100, 255, 0);"
where "enemyInfoData[ind].name" stores enemy's name, corresponding to the file name of the picture in "img/pictures/...". It worked but the picture was shown below the window and not on the foreground of it, so I decided to give a try to sprites.

The code I'm using to print a sprite rather than a picture is the following, but I get the error: "Cannot read property 'addChild' of undefined". Anyway from the command window "this._image1" seems not to be undefined. What am I doing wrong?


JavaScript:
 Scene_Battle.prototype.refreshImage = function (pictureName) {
        // create new Sprite
        this._image1 = new Sprite();
        this._image1._bitmap = ImageManager.loadPicture(pictureName.name);
      
        // add new Sprite as a child to the window's background
        this.addChild(this._image1);

        console.log("pictureName.name");
        console.log(pictureName.name);
        console.log("this._image1");
        console.log(this._image1);
    
        this.contents.addChild(this._image1); // this is the error row
    };

    Scene_Battle.prototype.createBattleEnemyPictureWindow = function (pictureName) {
        this._BattleEnemyPictureWindow = new Window_Base(new Rectangle(0, 0, 100, 100)); // Riduciamo la larghezza della finestra
        this._BattleEnemyPictureWindow.x = 100;
        this._BattleEnemyPictureWindow.y = 0;
        this.addWindow(this._BattleEnemyPictureWindow);
        this.refreshImage(pictureName);
    };

Screen1.PNG



Screen2.png.jpg


PS. I have no need to use sprite at all, it would be ok, for me, to just print a picture if any of you know how to plot it over the window and not on the background. But accordly to what I have read in other threads it seems to be impossible since pictures are plotted just over the map. Is it correct?

Thanks.
Looking it up in rmmz_core, the Sprite constructor expects you to be passing the bitmap into it as an argument.

Then, you're using addChild() incorrectly - that's a method of windows, not scenes.
That console output shows you that this._image1 is a Sprite. Twice, in fact. That's not the problem.

Sprites can be initialized without a bitmap, that's OK.

addChild is inherited from PIXI.Container: it works for scenes, sprites, and windows.

You add the sprite as a child of the scene, which is OK:
JavaScript:
        this.addChild(this._image1);
But then you try to add it again as a child of the scene's contents property:
JavaScript:
        this.contents.addChild(this._image1); // this is the error row
Scenes do not normally have a contents property, hence the error: "addChild of undefined" => this.contents is undefined.

(Note that for a game window, this.contents returns a Bitmap, which exists but does not have an addChild method. Just use this.addChild.)
addChild is inherited from PIXI.Container: it works for scenes, sprites, and windows.
My bad, I misread - the method defined for windows is addChildToBack() 🙄

Original thread: https://forums.rpgmakerweb.com/threads/plotting-a-sprite-over-the-window.159834/