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

Can't restart Phaser State when using DragonBones #60

Closed
Nodragem opened this issue Mar 2, 2018 · 2 comments
Closed

Can't restart Phaser State when using DragonBones #60

Nodragem opened this issue Mar 2, 2018 · 2 comments

Comments

@Nodragem
Copy link

Nodragem commented Mar 2, 2018

Hello,

I am using DragonBones to animate some of my characters (e.g. wasps).
I would like to simply restart my game after the player killed all the "wasps" on the screen. What I do for now is simply:

    public onWaspDeath () {
        console.log("Dead Wasps: " + this._wasps.countDead());
        if(this._wasps.countLiving() <= 1){ // the currently killed wasp is not dead yet
            console.log("You killed all the Wasps!")
            this.game.sound.play("congrats");
            this.game.state.restart();
        }
    }

That lead to this error in Chrome:

Uncaught TypeError: Failed to execute 'drawImage' on 'CanvasRenderingContext2D': The provided value is not of type '(CSSImageValue or HTMLImageElement or SVGImageElement or HTMLVideoElement or HTMLCanvasElement or ImageBitmap or OffscreenCanvas)'
    at i.Image.s.Sprite._renderCanvas (phaser.js:15696)
    at a.s.Sprite._renderCanvas (phaser.js:15701)
    at i.Group.s.DisplayObjectContainer._renderCanvas (phaser.js:15072)
    at i.World.s.DisplayObjectContainer._renderCanvas (phaser.js:15072)
    at i.Stage.s.DisplayObjectContainer._renderCanvas (phaser.js:15072)
    at s.CanvasRenderer.renderDisplayObject (phaser.js:20563)
    at s.CanvasRenderer.render (phaser.js:20488)
    at Game.updateRender (phaser.js:36186)
    at Game.update (phaser.js:36102)
    at i.RequestAnimationFrame.updateRAF (phaser.js:64166)

I added a conditional breakpoint before the problematic function call, so that I can check the parameter passed to that function when the game restarts.

opera_2018-03-02_11-08-04

So basically, with this method, I could see what this.texture.baseTexture.source is returning just after I called this.game.state.restart().

Note that the problematic function renderSession.context.drawImage() in part of a loop that iterates over the objects to display.

Here the results:

opera_2018-03-02_11-08-43

At the third iteration, for some reason this.texture.baseTexture.source is null. That means that my third object to be displayed is null.

After trying to find out which object was that third object, I start to think that it might be the wasp that I animated with DragonBones... So basically the wasp would load correctly when running the game, but it would fail to load when restarting the current phaser state.

Any body has already encountered a similar problem with DragonBones and Phaser?

Thank you very much for your help.

@Nodragem
Copy link
Author

Nodragem commented Mar 2, 2018

Ok so it seems that to write this issue helped me to find a solution :)

I just needed to add one line of code to reset the DragonBones factory:

    public onWaspDeath () {
        console.log("Dead Wasps: " + this._wasps.countDead());
        if(this._wasps.countLiving() <= 1){ 
            console.log("You killed all the Wasps!")
            this.game.sound.play("congrats");
            this._db_factory.clear(); // IMPORTANT CHANGE
            this.game.state.restart();
        }
    }

@Nodragem Nodragem closed this as completed Mar 2, 2018
@marcobneves
Copy link

marcobneves commented Jul 23, 2019

Hello, I have the same problem.
when restart my scene, my animation with Dragonbone not reset.
this.dragonbone.factory.clear() not work for me,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants