diff --git a/doc/examples/box2d/contact_callbacks/lib/boundaries.dart b/doc/examples/box2d/contact_callbacks/lib/boundaries.dart index 4dce6fb59..91658d1c5 100644 --- a/doc/examples/box2d/contact_callbacks/lib/boundaries.dart +++ b/doc/examples/box2d/contact_callbacks/lib/boundaries.dart @@ -27,9 +27,7 @@ class Wall extends BodyComponent { final Vector2 start; final Vector2 end; - Wall(this.start, this.end, Box2DGame game) : super(game) { - _createBody(start, end); - } + Wall(this.start, this.end, Box2DGame game) : super(game); @override void renderPolygon(Canvas canvas, List coordinates) { @@ -38,7 +36,8 @@ class Wall extends BodyComponent { canvas.drawLine(start, end, paint); } - void _createBody(Vector2 start, Vector2 end) { + @override + Body createBody() { final PolygonShape shape = PolygonShape(); shape.setAsEdge(start, end); @@ -52,6 +51,6 @@ class Wall extends BodyComponent { ..position = Vector2.zero() ..type = BodyType.STATIC; - body = world.createBody(bodyDef)..createFixtureFromFixtureDef(fixtureDef); + return world.createBody(bodyDef)..createFixtureFromFixtureDef(fixtureDef); } } diff --git a/doc/examples/box2d/contact_callbacks/lib/main.dart b/doc/examples/box2d/contact_callbacks/lib/main.dart index 13b42a32b..f25fc5777 100644 --- a/doc/examples/box2d/contact_callbacks/lib/main.dart +++ b/doc/examples/box2d/contact_callbacks/lib/main.dart @@ -19,12 +19,13 @@ void main() async { class Ball extends BodyComponent { Paint originalPaint, currentPaint; bool giveNudge = false; + final double _radius = 5.0; + Vector2 _position; Ball(Vector2 position, Box2DGame box2d) : super(box2d) { originalPaint = _randomPaint(); currentPaint = originalPaint; - final worldPosition = viewport.getScreenToWorld(position); - _createBody(5.0, worldPosition); + _position = viewport.getScreenToWorld(position); } Paint _randomPaint() { @@ -39,9 +40,10 @@ class Ball extends BodyComponent { ).paint; } - void _createBody(double radius, Vector2 position) { + @override + Body createBody() { final CircleShape shape = CircleShape(); - shape.radius = radius; + shape.radius = _radius; final fixtureDef = FixtureDef() ..shape = shape @@ -52,10 +54,10 @@ class Ball extends BodyComponent { final bodyDef = BodyDef() // To be able to determine object in collision ..setUserData(this) - ..position = position + ..position = _position ..type = BodyType.DYNAMIC; - body = world.createBody(bodyDef)..createFixtureFromFixtureDef(fixtureDef); + return world.createBody(bodyDef)..createFixtureFromFixtureDef(fixtureDef); } @override diff --git a/doc/examples/box2d/simple/lib/main.dart b/doc/examples/box2d/simple/lib/main.dart index 756b01faa..70e74d7e6 100644 --- a/doc/examples/box2d/simple/lib/main.dart +++ b/doc/examples/box2d/simple/lib/main.dart @@ -23,13 +23,12 @@ class MyPlanet extends BodyComponent { // components that are added to the same Box2DGame/Box2DComponent. // After 20 seconds the circle will be removed, to show that we don't get // any concurrent modification exceptions. - MyPlanet(Box2DGame game) : super(game) { - _createBody(50.0, Vector2.zero()); - } + MyPlanet(Box2DGame game) : super(game); - void _createBody(double radius, Vector2 position) { + @override + Body createBody() { final CircleShape shape = CircleShape(); - shape.radius = radius; + shape.radius = 50.0; final fixtureDef = FixtureDef(); // To be able to determine object in collision @@ -40,11 +39,11 @@ class MyPlanet extends BodyComponent { fixtureDef.friction = 0.1; final bodyDef = BodyDef(); - bodyDef.position = position; + bodyDef.position = Vector2.zero(); bodyDef.angularVelocity = 4.0; bodyDef.type = BodyType.DYNAMIC; - body = world.createBody(bodyDef)..createFixtureFromFixtureDef(fixtureDef); + return world.createBody(bodyDef)..createFixtureFromFixtureDef(fixtureDef); } @override