TOP(About this memo)) > Flutter > 一覧(Flame) > カメラ・ワールド
このコンポーネントの主な機能は、通常のレンダリングを無効にし、 CameraComponentを通じてのみレンダリングできるようにすることです。
更新は、通常どおりワールド ツリーを通じて進行します。
import 'dart:async';
import 'package:flame/components.dart';
import 'package:flame/events.dart';
import 'package:flame/game.dart';
import 'package:flutter/material.dart';
void main() {
runApp(
SafeArea(child: GameWidget(game: Example2())),
);
}
class Example2 extends FlameGame with DragCallbacks, ScaleDetector {
Example2()
: super(
world: _MyWorld(),
) {
debugMode = true;
}
final cameraPosition = Vector2.zero();
final scale = 1.0;
late final TextComponent text;
@override
FutureOr<void> onLoad() {
add(text = TextComponent(text: "", position: Vector2(0, 0)));
}
@override
void update(double dt) {
text.text =
"camera pos: ${camera.viewfinder.position.x.ceil()}, ${camera.viewfinder.position.y.ceil()}\ncamera zoom: ${camera.viewfinder.zoom}";
super.update(dt);
}
@override
void onDragUpdate(DragUpdateEvent event) {
cameraPosition.add(event.canvasDelta);
camera.viewfinder.position = cameraPosition;
}
late double startZoom;
@override
void onScaleStart(ScaleStartInfo info) {
print("onScaleStart: ${info.raw}");
startZoom = camera.viewfinder.zoom;
}
@override
void onScaleUpdate(ScaleUpdateInfo info) {
// iOSで試したところ、scaleがほとんど0.0となってしまう。
// 繰り返し操作をしているとscaleが取得できて想定通りズームできるときもある。
// 現状は解決手段が分からない。
print("onScaleUpdate: ${info.scale.global}");
if (info.scale.global.y == 0.0) return;
final currentScale = info.scale.global.y.clamp(0.5, 3.0);
camera.viewfinder.zoom = startZoom * currentScale;
}
}
class _MyWorld extends World with HasGameReference {
@override
Future<void> onLoad() async {
add(TextComponent(text: "on World", position: Vector2(0, 0)));
}
}