79179050

Date: 2024-11-11 20:36:02
Score: 0.5
Natty:
Report link

Turns out I was being slightly dumb. I was adding adding the components directly to the game rather than to the world which the camera is viewing. So just have to do world.add instead. Thank you Spydon for helping me out with this one :) Below full code that works:

import 'package:flame/game.dart';
import 'package:flame_forge2d/flame_forge2d.dart';
import 'package:flutter/material.dart';

void main() {
  runApp(GameWidget(game: SimpleGame()));
}

class SimpleGame extends Forge2DGame {
  SimpleGame()
      : super(
          gravity: Vector2(0, 10),
          zoom: 2,
        );

  @override
  Future<void> onLoad() async {
    super.onLoad();

    // Add the component to the world for camera settings to apply
    world.add(JarComponent(position: Vector2(1, 2), size: Vector2(20, 40)));
  }
}

class JarComponent extends BodyComponent {
  final Vector2 position;
  final Vector2 size;

  JarComponent({required this.position, required this.size});

  @override
  Body createBody() {
    final shape = PolygonShape()
      ..setAsBox(size.x, size.y, position, 0);

    final bodyDef = BodyDef()
      ..position = position
      ..type = BodyType.static;

    final body = world.createBody(bodyDef);
    body.createFixtureFromShape(shape);
    return body;
  }
}
Reasons:
  • Blacklisted phrase (0.5): Thank you
  • Long answer (-1):
  • Has code block (-0.5):
  • Self-answer (0.5):
  • Low reputation (1):
Posted by: Bob E