Cocos Creator3.8 の実際的な問題 (2) Cocos Creator エディターのバインディング イベントによって引き起こされるバグ


問題の説明:

エディター内の複数のボタンが同じスクリプトにバインドされ、イベントで構成されています。スクリプト内に他のメッセージ リスナーがあるため、論理的な混乱が発生します。


問題の原因:

エディタ内の複数のボタンは同じスクリプトにバインドされています。ボタン イベントがバインドされるたびに、スクリプトは 1 回ロードされます。複数のボタン イベントが上記のようにバインドされている場合、スクリプトは複数回ロードされ、その結果インターフェイスが初期化されます。 :onload などは複数回実行されます。このとき、onload には他のメッセージ リスナーが存在し、同じメッセージが複数回実行されるため、論理的な混乱が発生します。


問題の解決策:

エディターでボタン イベントを削除し、スクリプト コードにボタン プロパティを追加するように調整して、イベントを手動でバインドします。

   @property(Button)
    btAccountLogin:Button;

    @property(Button)
    btGustLogin:Button;

    @property(Button)
    btGoogleLogin:Button;

    @property(Button)
    btFaceBookLogin:Button;

    @property(Button)
    btWhatsLogin:Button;

    @property(Button)
    btTelegramLogin:Button;

    onLoad () {
        this.btAccountLogin.node.on(Button.EventType.CLICK, this.onAccountLogin, this);
        this.btGustLogin.node.on(Button.EventType.CLICK, this.onGuestLogin, this);
        this.btGoogleLogin.node.on(Button.EventType.CLICK, this.onGoogleLogin, this);
        this.btFaceBookLogin.node.on(Button.EventType.CLICK, this.onFaceBookLogin, this);
        this.btWhatsLogin.node.on(Button.EventType.CLICK, this.onWhatsAppLogin, this);
        this.btTelegramLogin.node.on(Button.EventType.CLICK, this.onTelegramLogin, this);
    }

おすすめ

転載: blog.csdn.net/lizhong2008/article/details/133297211