I'm using Create React App as framework and trying to render the GameObject new Phaser.Game(gameConfig) as React component. I am able to render but it creates a duplicate Canvas object as shown below: enter image description here

以下是我的游戏组件:

import Phaser from "phaser";
import React from "react";

export default class Game extends React.Component {
    componentDidMount() {
        const gameConfig: Phaser.Types.Core.GameConfig = {
            type: Phaser.AUTO,
            parent: 'game-container',
            backgroundColor: '#EBEBEB',
            width: 600,
            height: 800
        }

        new Phaser.Game(gameConfig);
    }

    shouldComponentUpdate() {
        return false;
    }

    render() {
        return <div id='game-container' />   
    }
}

这就是我要渲染的地方:

import React from 'react';
import ReactDOM from 'react-dom/client';
import './index.css';
import reportWebVitals from './reportWebVitals';
import Game from './game';

const root = ReactDOM.createRoot(
  document.getElementById('root') as HTMLElement
);
root.render(
  <React.StrictMode>
    <Game />
  </React.StrictMode>
);

reportWebVitals();

如何删除此副本?

推荐答案

我假设问题可能是,由于<React.StrictMode>标记,函数componentDidMount可能被删除两次.如果删除此标记,则函数将不会执行两次,因为每次调用componentDidMount都会创建一个新的相位器gameobject,这是由于调用new Phaser.Game(...).这反过来又创造了一幅新的画布.

由于生产中并不真正需要标签<React.StrictMode>,因此您可以移除它,而不会出现问题/更改您的用户界面输出.

<script src="https://cdn.jsdelivr.net/npm/phaser@3.55.2/dist/phaser.min.js"></script>
<script src="https://unpkg.com/react/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>

<div id="root"></div>

<script type="text/babel">
    class Game extends React.Component {
        componentDidMount() {
            const gameConfig = {
                type: Phaser.AUTO,
                parent: 'game-container',
                backgroundColor: '#EBEBEB',
                width: 300,
                height: 120,
                scene: {create(){ this.add.text(150,60, 'GAME').setFontFamily('Arial').setFontSize(80).setOrigin(.5).setColor('#000000')}}
            }

            new Phaser.Game(gameConfig);
        }

        shouldComponentUpdate() {
            return false;
        }

        render() {
            return <div id='game-container' /> 
        }
    }

    const root = ReactDOM.createRoot(document.getElementById('root'));
    // creates two canvas elements
    root.render( <React.StrictMode><Game /></React.StrictMode> );
    // creates one canvas elements
    root.render( <Game /> );
</script>

Javascript相关问答推荐

Express.js:使用Passport.js实现基于角色的身份验证时出现太多重定向问题

将数据从strapi提取到next.js,但响应延迟API URL

如何将连续的十六进制字符串拆分为以空间分隔的十六进制块,每个十六进制块包含32个二元组?

react—router v6:路由没有路径

并不是所有的iframe都被更换

如果Arrow函数返回函数,而不是为useEffect返回NULL,则会出现错误

确保函数签名中的类型安全:匹配值

如何在 Select 文本时停止Click事件?

将数组扩展到对象中

从另一个数组中的对应行/键值对更新数组中的键值对对象

TypeError:无法读取未定义的属性(正在读取';宽度';)

Cherrio JS返回父div的所有图像SRC

使用API调用的VUE 3键盘输入同步问题

select 2-删除js插入的项目将其保留为选项

如何将字符串拆分成单词并跟踪每个单词的索引(在原始字符串中)?

如何设置时间选取器的起始值,仅当它获得焦点时?

Firebase函数中的FireStore WHERE子句无法执行

如何处理不带参数的redux thunk payloadCreator回调函数?

REACT-本机错误:错误类型错误:无法读取未定义的容器的属性

Reaction-在同一页内滚动导航(下拉菜单)