@@ -25,6 +25,8 @@ class World {
2525 late ContactManager contactManager;
2626 final List <Body > bodies = < Body > [];
2727 final List <Joint > joints = < Joint > [];
28+ final List <Body > bodiesToCreate = < Body > [];
29+ final List <Body > bodiesToDestroy = < Body > [];
2830
2931 final Vector2 _gravity;
3032
@@ -133,8 +135,11 @@ class World {
133135 ///
134136 /// Warning: This function is locked during callbacks.
135137 Body createBody (BodyDef def) {
136- assert (! isLocked);
137138 final body = Body (def, this );
139+ if (isLocked) {
140+ bodiesToCreate.add (body);
141+ return body;
142+ }
138143 bodies.add (body);
139144 return body;
140145 }
@@ -145,8 +150,10 @@ class World {
145150 /// Warning: This automatically deletes all associated shapes and joints.
146151 /// Warning: This function is locked during callbacks.
147152 void destroyBody (Body body) {
148- assert (bodies.isNotEmpty);
149- assert (! isLocked);
153+ if (isLocked) {
154+ bodiesToDestroy.add (body);
155+ return ;
156+ }
150157
151158 // Delete the attached joints.
152159 while (body.joints.isNotEmpty) {
@@ -297,6 +304,16 @@ class World {
297304
298305 flags & = ~ locked;
299306
307+ for (final body in bodiesToCreate) {
308+ bodies.add (body);
309+ }
310+ bodiesToCreate.clear ();
311+
312+ for (final body in bodiesToDestroy) {
313+ destroyBody (body);
314+ }
315+ bodiesToDestroy.clear ();
316+
300317 _profile.step.record (_stepTimer.getMilliseconds ());
301318 }
302319
0 commit comments