iT邦幫忙

2024 iThome 鐵人賽

DAY 10
0
Mobile Development

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

繼續測試Forge2D的基本特性

  • 分享至 

  • xImage
  •  

(週末,做的事情比較簡單。)


update(double dt) {
    super.update(dt);

    Vector2 g = world.gravity;
    g.rotate(dt/2);
  }

  List<Component> createBoundaries() {
    final visibleRect = camera.visibleWorldRect;
    final topLeft = visibleRect.topLeft.toVector2();
    final topCenter = visibleRect.topCenter.toVector2() + Vector2(0, 10);
    final topRight = visibleRect.topRight.toVector2();
    final bottomRight = visibleRect.bottomRight.toVector2();
    final bottomCenter = visibleRect.bottomCenter.toVector2() + Vector2(0, -10);
    final bottomLeft = visibleRect.bottomLeft.toVector2();

    return [
      Wall(topLeft, topCenter),
      Wall(topCenter, topRight),
      Wall(topRight, bottomRight),
      Wall(bottomLeft, bottomCenter),
      Wall(bottomCenter, bottomRight),
      Wall(topLeft, bottomLeft),
    ];
  }

這個範例設定為「重力方向每秒會旋轉90度」。
(rotate這個函數輸入的數值是以「180度」為「1單位」,而每次轉動完,BodyComponent的角度會維持在轉動後的數值,也就是說「會累積」。如果用dt作為轉動數值輸入,那每秒就會轉一圈,將輸入的數值除以二,則會變成每兩秒轉一圈。)

然後修改為兩顆球,同時遊戲邊界不是個正四邊行,而是朝中央突起的六邊型。

果然,重力也可以隨時在遊戲過程中修改,而不是只能受限於初始值。

(這個特性可以拿來做出「創意撞球遊戲」。)


上一篇
Forge2D的一些用法...
下一篇
如何新增和消除一個BodyComponent?(如何偵測與其他BodyComponent發生接觸?)
系列文
用Flutter Flame做遊戲!Live!26
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言