如何完全删除Pixi渲染器,舞台和资产?

dag*_*oin 2 javascript reactjs pixi.js

我正在尝试使用React安装/取消Pixi舞台(我现在还不想使用react-pixi)

重新安装组件时出现错误:

Uncaught Error: Resource with name "cupCake.png" already exists
i.add.i.enqueueapp.js
componentDidMountmodules.js
_assign.notifyAllmodules.js
ON_DOM_READY_QUEUEING.closemodules.js
Mixin.closeAllmodules.js
Mixin.performmodules.js
Mixin.performmodules.js
_assign.performmodules.js
flushBatchedUpdatesmodules.js
Mixin.closeAllmodules.js
Mixin.performmodules.js
ReactDefaultBatchingStrategy.batchedUpdatesmodules.js
batchedUpdatesmodules.js
ReactEventListener.dispatchEvent
Run Code Online (Sandbox Code Playgroud)

我尝试使用:PIXI.TextureCache['cupCake.png'].destroy(true);但是错误仍然存​​在。

这是我的组件:

class MapOverlay extends React.Component{

  constructor(props) {
    super(props);
  }

  componentDidMount() {
    this.renderer = PIXI.autoDetectRenderer(256, 256, {transparent: true});
    this.refs.gameCanvas.appendChild(this.renderer.view);

    // create the root of the scene graph
    this.stage = new PIXI.Container();
    this.stage.width = 1366;
    this.stage.height = 768;

    PIXI.loader
      .add("cupCake.png")
      .load(()=> {

        //Create the `cat` sprite from the texture
        var cat = new PIXI.Sprite(
          PIXI.loader.resources["cupCake.png"].texture
        );

        //Add the cat to the stage
        this.stage.addChild(cat);

        //Render the stage
        this.renderer.render(this.stage);
      });
  }

  componentWillUnmount() {
    this.refs.gameCanvas.removeChild(this.renderer.view);
    PIXI.TextureCache['cupCake.png'].destroy(true);
  }

  render() {
    return (
      <div className="game-canvas-container" ref="gameCanvas"></div>
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

那么,如何才能完全重置/删除状态和资产?

小智 6

答案是使用最新的PIXI v4版本

去除纹理的最佳方法是调用

this.stage.destroy(true);
this.stage = null;
Run Code Online (Sandbox Code Playgroud)

这将破坏舞台及其所有子级,以及任何子级正在使用的纹理。

在做完之后

this.refs.gameCanvas.removeChild(this.renderer.view);
Run Code Online (Sandbox Code Playgroud)

添加行

this.renderer.destroy( true );
this.renderer = null;
Run Code Online (Sandbox Code Playgroud)