首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Cocos v3.4中创建二维接触物理时的未明误差破坏()

Cocos v3.4中创建二维接触物理时的未明误差破坏()
EN

Stack Overflow用户
提问于 2022-02-01 16:26:35
回答 1查看 173关注 0票数 0

我使用可可仪表板1.02和可可创建者v3.4。添加以下代码时出错: this.node.destroy();以删除预置文件。

(我试图在coscos:https://docs.cocos.com/creator/manual/en/physics-2d/physics-2d-contact-callback.html?h=contact中使用DOC )

请帮帮我。

我的代码:

代码语言:javascript
运行
复制
@ccclass('Ball')
export class Ball extends Component {
    start () {
        // Registering callback functions for a single collider
        let collider = this.getComponent(Collider2D);
        if (collider) {
            collider.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);
        }
    }
    onBeginContact (selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
        // will be called once when two colliders begin to contact
        this.node.destroy();//// ERROR WHEN I ADD THIS LINE
        console.log('onBeginContact');
    }
}

还有我的游戏经理

代码语言:javascript
运行
复制
@ccclass('GameManager')
export class GameManager extends Component {

    @property({type: Prefab})
    public ballPrfb: Prefab|null = null;

    @property({type: Node})
    public Player: Node|null = null;

    @property({type: CCInteger})
    public _spawnTime: number = 2;

    private TimeFalling : number;

    start () {
       this.TimeFalling = 0;
    }
    update (deltaTime: number) {
        this.TimeFalling = this.TimeFalling - deltaTime;
        let block = instantiate(this.ballPrfb);
        
        if(this.TimeFalling <=0){
        this.node.removeAllChildren();
        this.node.addChild(block);
        block.setPosition(Math.random() * 800, 0, 0);
        this.TimeFalling = this._spawnTime;
        }
    }

在我的控制台里:

代码语言:javascript
运行
复制
ErrorEventisTrusted: truebubbles: falsecancelBubble: falsecancelable: truecolno: 85composed: falsecurrentTarget: Window {window: Window, self: Window, document: document, name: '', location: Location, …}defaultPrevented: falseerror: Error
    at b2Body.SetActive (http://localhost:7456/scripting/engine/bin/.cache/dev/preview/external/%2540cocos/box2d.js:1518:91)
    at b2RigidBody2D.setActive (http://localhost:7456/scripting/engine/bin/.cache/dev/preview/bundled/index.js:176503:22)
    at b2RigidBody2D.onDisable (http://localhost:7456/scripting/engine/bin/.cache/dev/preview/bundled/index.js:176353:16)
    at RigidBody2D.onDisable (http://localhost:7456/scripting/engine/bin/.cache/dev/preview/bundled/index.js:257598:24)
    at ComponentScheduler.disableComp (http://localhost:7456/scripting/engine/bin/.cache/dev/preview/bundled/index.js:150494:20)
    at NodeActivator._deactivateNodeRecursively (http://localhost:7456/scripting/engine/bin/.cache/dev/preview/bundled/index.js:151568:48)
    at NodeActivator.activateNode (http://localhost:7456/scripting/engine/bin/.cache/dev/preview/bundled/index.js:151420:18)
    at Node.set (http://localhost:7456/scripting/engine/bin/.cache/dev/preview/bundled/index.js:149928:52)
    at Node.destroy (http://localhost:7456/scripting/engine/bin/.cache/dev/preview/bundled/index.js:149621:25)
    at Ball.onBeginContact (http://localhost:7456/scripting/x/mods/fs/0/assets/script/Ball.js:47:21)eventPhase: 0filename: "http://localhost:7456/scripting/engine/bin/.cache/dev/preview/external/%2540cocos/box2d.js"lineno: 1518message: "Uncaught Error"path: [Window]returnValue: truesrcElement: Window {window: Window, self: Window, document: document, name: '', location: Location, …}target: Window {window: Window, self: Window, document: document, name: '', location: Location, …}timeStamp: 18327.200000047684type: "error"[[Prototype]]: ErrorEvent
(ẩn danh) @ index.js:1
box2d.umd.js:7181 Uncaught Error
    at b2Body.SetActive (box2d.umd.js:7181:15)
    at b2RigidBody2D.setActive (rigid-body.ts:202:21)
    at b2RigidBody2D.onDisable (rigid-body.ts:63:14)
    at RigidBody2D.onDisable (rigid-body-2d.ts:516:24)
    at ComponentScheduler.disableComp (component-scheduler.ts:439:22)
    at NodeActivator._deactivateNodeRecursively (node-activator.ts:326:50)
    at NodeActivator.activateNode (node-activator.ts:197:18)
    at Node.set (base-node.ts:172:54)
    at Node.destroy (base-node.ts:1199:13)
    at Ball.onBeginContact (Ball.ts:30:19)

EN

回答 1

Stack Overflow用户

发布于 2022-03-28 14:36:55

代码语言:javascript
运行
复制
setTimeout(() => {
    this.node.destroy();
}, 1);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70944031

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档