可以在节点脚本响应函数内,将节点put入nodePool吗?怎么直接崩溃?

如下图,put一运行页面就崩溃:


跟踪检查过,troopPool是正常实例化的

详细报错信息:
Uncaught Error
at b2Body.SetActive (box2d.umd.js:7181:15)
at b2RigidBody2D.setActive (rigid-body.ts:231:21)
at b2RigidBody2D.onDisable (rigid-body.ts:82:14)
at RigidBody2D.onDisable (rigid-body-2d.ts:543:24)
at ComponentScheduler.disableComp (component-scheduler.ts:438:22)
at NodeActivator._deactivateNodeRecursively (node-activator.ts:316:50)
at NodeActivator.activateNode (node-activator.ts:186:18)
at Node._onHierarchyChangedBase (node.ts:1323:46)
at Node._onHierarchyChanged (node.ts:1798:14)
at Node.setParent (node.ts:469:14)
SetActive @ box2d.umd.js:7181
setActive @ rigid-body.ts:231
onDisable @ rigid-body.ts:82
onDisable @ rigid-body-2d.ts:543
disableComp @ component-scheduler.ts:438
_deactivateNodeRecursively @ node-activator.ts:316
activateNode @ node-activator.ts:186
_onHierarchyChangedBase @ node.ts:1323
_onHierarchyChanged @ node.ts:1798
setParent @ node.ts:469
set @ node.ts:217
removeChild @ node.ts:734
removeFromParent @ node.ts:722
put @ node-pool.ts:130
onEndContact @ troopControlScript.ts:56
emit @ callbacks-invoker.ts:344
emit @ physics-contact.ts:241
_onEndContact @ physics-world.ts:404
EndContact @ physics-contact-listener.ts:70
Update @ box2d.umd.js:11806
Collide @ box2d.umd.js:12479
Step @ box2d.umd.js:19588
step @ physics-world.ts:161
postUpdate @ physics-system.ts:290
tick @ director.ts:724
_updateCallback @ game.ts:1015
updateCallback @ pacer-web.ts:89
requestAnimationFrame (async)
updateCallback @ pacer-web.ts:86
requestAnimationFrame (async)
updateCallback @ pacer-web.ts:86
requestAnimationFrame (async)
updateCallback @ pacer-web.ts:86
requestAnimationFrame (async)
updateCallback @ pacer-web.ts:86
requestAnimationFrame (async)
updateCallback @ pacer-web.ts:86
requestAnimationFrame (async)
updateCallback @ pacer-web.ts:86
requestAnimationFrame (async)
updateCallback @ pacer-web.ts:86
requestAnimationFrame (async)
updateCallback @ pacer-web.ts:86
requestAnimationFrame (async)
updateCallback @ pacer-web.ts:86
requestAnimationFrame (async)
updateCallback @ pacer-web.ts:86
requestAnimationFrame (async)
updateCallback @ pacer-web.ts:86
requestAnimationFrame (async)
updateCallback @ pacer-web.ts:86
requestAnimationFrame (async)
updateCallback @ pacer-web.ts:86
requestAnimationFrame (async)
updateCallback @ pacer-web.ts:86
requestAnimationFrame (async)
updateCallback @ pacer-web.ts:86
requestAnimationFrame (async)
updateCallback @ pacer-web.ts:86
requestAnimationFrame (async)
updateCallback @ pacer-web.ts:86
requestAnimationFrame (async)
updateCallback @ pacer-web.ts:86
requestAnimationFrame (async)
updateCallback @ pacer-web.ts:86
requestAnimationFrame (async)
updateCallback @ pacer-web.ts:86
requestAnimationFrame (async)
updateCallback @ pacer-web.ts:86
requestAnimationFrame (async)
updateCallback @ pacer-web.ts:86
requestAnimationFrame (async)
updateCallback @ pacer-web.ts:86
requestAnimationFrame (async)
updateCallback @ pacer-web.ts:86
requestAnimationFrame (async)
updateCallback @ pacer-web.ts:86
requestAnimationFrame (async)
updateCallback @ pacer-web.ts:86
requestAnimationFrame (async)
updateCallback @ pacer-web.ts:86
requestAnimationFrame (async)
updateCallback @ pacer-web.ts:86
requestAnimationFrame (async)
updateCallback @ pacer-web.ts:86
requestAnimationFrame (async)
updateCallback @ pacer-web.ts:86
requestAnimationFrame (async)
updateCallback @ pacer-web.ts:86
requestAnimationFrame (async)
updateCallback @ pacer-web.ts:86
index.js:83 WebSocket is already in CLOSING or CLOSED state.
(anonymous) @ index.js:83
e.encodePacket @ index.js:83
(anonymous) @ index.js:83
r.write @ index.js:83
r.send @ index.js:83
r.flush @ index.js:83
r.sendPacket @ index.js:83
r.ping @ index.js:83
(anonymous) @ index.js:83

Box2d报错了,别在碰撞回调里处理,会造成野指针报错

1赞

建议放在下一帧来操作,this.scheduleOnce()

1赞

谢谢,下面的给nodeInfo.nodeType中的枚举变量赋值怎么会报错?

定义:
interface IPlayer {

playerName:String;

playerColor:Color;

}

export const enum enumNodeType {

Troop,

City

}

interface nodeInfo {

playerInfo:IPlayer;

nodeType:enumNodeType

}

在函数中使用:
let nodeInfo:nodeInfo;
nodeInfo.nodeType = enumNodeType.City

        nodeInfo.playerInfo.playerName = cityScript.playerName

        nodeInfo.playerInfo.playerColor = cityScript.playerColor

nodeInfo初始化一下
let nodeInfo:nodeInfo = {
playerInfo: undefined,
nodeType: undefined
};

1赞

初始化了nodeinfo,这个没报错,可下面这个又报错了:

一样的
let nodeInfo:nodeInfo = {
playerInfo: {
playerName:undefined,

playerColor:undefined},
nodeType: undefined
};

1赞

typescript使用个结构变量,要这么麻烦? :sweat:
谢谢!

使用scheduleOnce,也会报错:

就跟踪进下面这个函数报错:(这个函数还是节点的脚本内,这个没问题吧?)
image

报错信息:

ErrorEvent {isTrusted: true, message: “Uncaught TypeError: Cannot read properties of undefined (reading ‘uuid’)”, filename: ‘http://localhost:7456/scripting/engine/bin/.cache/dev/preview/bundled/index.js’, lineno: 13224, colno: 33, …}isTrusted: truebubbles: falsecancelBubble: falsecancelable: truecolno: 33composed: falsecurrentTarget: Window {window: Window, self: Window, document: document, name: ‘’, location: Location, …}defaultPrevented: falseerror: TypeError: Cannot read properties of undefined (reading ‘uuid’)
at Scheduler.schedule (http://localhost:7456/scripting/engine/bin/.cache/dev/preview/bundled/index.js:13224:33)
at troopControlScript.schedule (http://localhost:7456/scripting/engine/bin/.cache/dev/preview/bundled/index.js:45882:21)
at troopControlScript.scheduleOnce (http://localhost:7456/scripting/engine/bin/.cache/dev/preview/bundled/index.js:45904:16)
at troopControlScript.onBeginContact (http://localhost:7456/scripting/x/chunks/c8/c86f941b2f7567ccd66ee16cfd7ee16bdc2713c8.js:90:18)
at CircleCollider2D.emit (http://localhost:7456/scripting/engine/bin/.cache/dev/preview/bundled/index.js:150296:28)
at PhysicsContactListener.emit (http://localhost:7456/scripting/engine/bin/.cache/dev/preview/bundled/index.js:308861:77)
at http://localhost:7456/scripting/engine/bin/.cache/dev/preview/bundled/index.js:308830:21
at Map.forEach ()
at PhysicsContactListener.finalizeContactEvent (http://localhost:7456/scripting/engine/bin/.cache/dev/preview/bundled/index.js:308820:46)
at b2PhysicsWorld.finalizeContactEvent (http://localhost:7456/scripting/engine/bin/.cache/dev/preview/bundled/index.js:216820:33)eventPhase: 0filename: "http://localhost:7456/scripting/engine/bin/.cache/dev/preview/bundled/index.js"lineno: 13224message: "Uncaught TypeError: Cannot read properties of undefined (reading ‘uuid’)"returnValue: truesrcElement: Window {window: Window, self: Window, document: document, name: ‘’, location: Location, …}target: Window {window: Window, self: Window, document: document, name: ‘’, location: Location, …}timeStamp: 81585.09999990463type: “error”[[Prototype]]: ErrorEvent
(anonymous) @ index.js:1
scheduler.ts:573 Uncaught TypeError: Cannot read properties of undefined (reading ‘uuid’)
at Scheduler.schedule (scheduler.ts:573:33)
at troopControlScript.schedule (component.ts:444:19)
at troopControlScript.scheduleOnce (component.ts:461:14)
at troopControlScript.onBeginContact (troopControlScript.ts:33:18)
at CircleCollider2D.emit (callbacks-invoker.ts:344:34)
at PhysicsContactListener.emit (physics-contact-listener.ts:127:24)
at physics-contact-listener.ts:97:22
at Map.forEach ()
at PhysicsContactListener.finalizeContactEvent (physics-contact-listener.ts:88:44)
at b2PhysicsWorld.finalizeContactEvent (physics-world.ts:382:31)

试试这个写法 this.scheduleOnce(()=>{this.putToNodePool()})

1赞