mirror of
https://github.com/cocos/cocos4.git
synced 2026-11-20 23:52:40 +08:00
055c70f8d2
Clear cached debug graphics instead of detaching the debug draw node when debugDrawFlags is set to None. This keeps the cached node in a valid scene state and prevents null parent access when debug draw is enabled again. Add a regression test for toggling Box2D debug draw flags.
38 lines
1.2 KiB
TypeScript
38 lines
1.2 KiB
TypeScript
import * as physics2d from "../../exports/physics-2d-framework";
|
|
|
|
/**
|
|
* 验证 Box2D 后端关闭调试绘制时保留调试节点,避免再次开启时父节点为空。
|
|
*/
|
|
export default function DebugDrawTest (): void {
|
|
if (physics2d.selector.id === 'builtin') {
|
|
return;
|
|
}
|
|
|
|
const parent = {};
|
|
const clear = jest.fn();
|
|
const debugGraphics = {
|
|
clear,
|
|
node: {
|
|
parent,
|
|
},
|
|
};
|
|
const physicsWorld = physics2d.PhysicsSystem2D.instance.physicsWorld as unknown as {
|
|
_debugGraphics: typeof debugGraphics | null;
|
|
};
|
|
|
|
physicsWorld._debugGraphics = debugGraphics;
|
|
|
|
try {
|
|
physics2d.PhysicsSystem2D.instance.debugDrawFlags = physics2d.EPhysics2DDrawFlags.Shape;
|
|
physics2d.PhysicsSystem2D.instance.debugDrawFlags = physics2d.EPhysics2DDrawFlags.None;
|
|
expect(clear).toHaveBeenCalledTimes(1);
|
|
expect(debugGraphics.node.parent).toBe(parent);
|
|
|
|
physics2d.PhysicsSystem2D.instance.debugDrawFlags = physics2d.EPhysics2DDrawFlags.Shape;
|
|
expect(debugGraphics.node.parent).toBe(parent);
|
|
} finally {
|
|
physics2d.PhysicsSystem2D.instance.debugDrawFlags = physics2d.EPhysics2DDrawFlags.None;
|
|
physicsWorld._debugGraphics = null;
|
|
}
|
|
}
|