iT邦幫忙

2024 iThome 鐵人賽

DAY 24
0
Mobile Development

用Flutter Flame做遊戲!Live!系列 第 26

週末,罷工一下....MouseJoint的垃圾官方範例...

  • 分享至 

  • xImage
  •  

以下是官方關於「MouseJoint」的範例


import 'package:examples/stories/bridge_libraries/flame_forge2d/revolute_joint_with_motor_example.dart';
import 'package:examples/stories/bridge_libraries/flame_forge2d/utils/balls.dart';
import 'package:examples/stories/bridge_libraries/flame_forge2d/utils/boundaries.dart';
import 'package:flame/components.dart';
import 'package:flame/events.dart';
import 'package:flame_forge2d/flame_forge2d.dart';

class MouseJointExample extends Forge2DGame {
  static const description = '''
    In this example we use a `MouseJoint` to make the ball follow the mouse
    when you drag it around.
  ''';

  MouseJointExample()
      : super(gravity: Vector2(0, 10.0), world: MouseJointWorld());
}

class MouseJointWorld extends Forge2DWorld
    with DragCallbacks, HasGameReference<Forge2DGame> {
  late Ball ball;
  late Body groundBody;
  MouseJoint? mouseJoint;

  @override
  Future<void> onLoad() async {
    super.onLoad();
    final boundaries = createBoundaries(game);
    addAll(boundaries);

    final center = Vector2.zero();
    groundBody = createBody(BodyDef());
    ball = Ball(center, radius: 5);
    add(ball);
    add(CornerRamp(center));
    add(CornerRamp(center, isMirrored: true));
  }

  @override
  void onDragStart(DragStartEvent info) {
    super.onDragStart(info);
    final mouseJointDef = MouseJointDef()
      ..maxForce = 3000 * ball.body.mass * 10
      ..dampingRatio = 0.1
      ..frequencyHz = 5
      ..target.setFrom(ball.body.position)
      ..collideConnected = false
      ..bodyA = groundBody
      ..bodyB = ball.body;

    if (mouseJoint == null) {
      mouseJoint = MouseJoint(mouseJointDef);
      createJoint(mouseJoint!);
    }
  }

  @override
  void onDragUpdate(DragUpdateEvent info) {
    mouseJoint?.setTarget(info.localEndPosition);
  }

  @override
  void onDragEnd(DragEndEvent info) {
    super.onDragEnd(info);
    destroyJoint(mouseJoint!);
    mouseJoint = null;
  }
}

這段範例被我評為「垃圾」,因為「createJoint」和「destroyJoint」已經不再由「Forge2DGame」提供了,必須要經由「Forge2DWorld」取得。
也就是說「這段範例無法運作」,而且官方SDK是直接報錯、完全不提供修正指引或相容版本,即使官方文件上仍舊提供這段程式碼。
反正,在這裡指出修正辦法。


另外,這段範例設計上奇妙的地方是「點擊滑鼠不要放開,目標圓球會往滑鼠游標移動,直到圓球到達定位點或點擊放開。」
但「圓球到達定位點」和「點擊放開」是有不同結果的。
前者,圓球會停留在定位點上,但如果在這之前放開點擊,圓球會維持著一個慣性動能。

所以如果要用這個範例來製作「點擊、物件往點擊處移動」的效果,基本上是不夠的。(不是不行,而是還要加很多功。)

(因為該死的鐵人賽規則,所以這篇文就這樣發出去了。)


上一篇
用「拖曳物件到螢幕邊緣」來移動camera
系列文
用Flutter Flame做遊戲!Live!26
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言