mirror of
https://github.com/flame-engine/flame.git
synced 2025-11-04 04:47:13 +08:00
Modify examples to override createBody
This commit is contained in:
@ -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<Offset> 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);
|
||||
}
|
||||
}
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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
|
||||
|
||||
Reference in New Issue
Block a user