Flame Forge2D 缩放构造函数未按预期工作

问题描述 投票:0回答:1

我在下面有一个简单的项目,我在屏幕中央创建了一个物理主体矩形。以下文档指出,当使用 Forge2D 特定的 FlameGame 类 Forge2DGame 时,您可以在构造函数中使用 super(zoom: yourZoom) 来放大世界,而无需更改物理对象的尺寸。我想这样做是因为当我向这个世界添加更多对象时,我不希望事物那么快达到最终速度。

但是,当我更改缩放值时,什么也没有发生。我在这里缺少什么?我可能很愚蠢,但非常感谢任何帮助。

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: 100,
        );

  @override
  Future<void> onLoad() async {
    super.onLoad();
    add(JarComponent(position: Vector2(200, 400), size: Vector2(40, 80)));
  }
}

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()
      ..type = BodyType.static;

    return world.createBody(bodyDef)..createFixtureFromShape(shape);
  }
}
flutter dart flame forge2d
1个回答
0
投票

事实证明我有点愚蠢。我将组件直接添加到游戏中,而不是添加到相机正在查看的世界中。所以只需要做 world.add 即可。谢谢 Spydon 帮助我解决这个问题:) 下面是有效的完整代码:

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;
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.