我使用可可仪表板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 )
请帮帮我。
我的代码:
@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');
}
}
还有我的游戏经理
@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;
}
}
在我的控制台里:
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)
发布于 2022-03-28 14:36:55
setTimeout(() => {
this.node.destroy();
}, 1);
https://stackoverflow.com/questions/70944031
复制相似问题