TOP(About this memo)) > Flutter > 一覧(Flame) > PositionComponent
A Component implementation that represents an object that can be freely moved around the screen, rotated, and scaled.
// ~/.pub-cache/hosted/pub.dev/flame-1.18.0/lib/src/anchor.dart
class Anchor {
static const Anchor topLeft = Anchor(0.0, 0.0);
static const Anchor topCenter = Anchor(0.5, 0.0);
static const Anchor topRight = Anchor(1.0, 0.0);
static const Anchor centerLeft = Anchor(0.0, 0.5);
static const Anchor center = Anchor(0.5, 0.5);
static const Anchor centerRight = Anchor(1.0, 0.5);
static const Anchor bottomLeft = Anchor(0.0, 1.0);
static const Anchor bottomCenter = Anchor(0.5, 1.0);
static const Anchor bottomRight = Anchor(1.0, 1.0);
final double x;
final double y;
//...
}
// 公式ドキュメントから引用
final comp = PositionComponent(
size: Vector2.all(20),
anchor: Anchor.center,
);
// Returns (0,0)
// 生成時にアンカーをAnchor.centerとしているため、図形の真ん中が親のローカル原点の位置(この場合は親はGameサイズと同じ場合として(0,0))になる。
final p1 = component.position;
// Returns (10, 10)
// 図形の真ん中が(0,0)のため、図形の右下の座標は(10, 10)となる
final p2 = component.positionOfAnchor(Anchor.bottomRight);
@override
Future<void> onLoad() async {
add(RectangleComponent(size: Vector2(10, 10)));
}
// 例1
final parallax = Parallax(
//...
);
final parallaxComponent = ParallaxComponent(parallax: parallax);
add(parallaxComponent);
// 例2
class MyParallaxComponent extends ParallaxComponent {
@override
Future<void> onLoad() async {
parallax = await game.loadParallax(
[
ParallaxImageData('parallax/bg.png'),
//...
],
);
}
}
// flame-1.19.0/lib/src/cache/images.dart
final Map<String, _ImageAsset> _assets = {};
Future<Image> load(String fileName, {String? key}) {
return (_assets[key ?? fileName] ??=
_ImageAsset.future(_fetchToMemory(fileName)))
.retrieveAsync();
}
//...
Future<Image> _fetchToMemory(String name) async {
final data = await bundle.load('$_prefix$name');
final bytes = Uint8List.view(data.buffer);
return decodeImageFromList(bytes);
}
Flame.images.prefix = "assets/";
import 'package:http/http.dart';
//...
image = await Flame.images
.fetchOrGenerate("キャッシュ用の名前", () async {
final response = await get(Uri.parse("https://docs.flutter.dev/assets/images/dash/Dash.png"));
return decodeImageFromList(response.bodyBytes);
});
@override
Future<void> onLoad() async {
add(SpriteComponent(sprite: await game.loadSprite('xxx/yyy.png'), anchor: Anchor.center, size: Vector2.all(128.0), position: Vector2(10, 20), angle:pi/2));
}
final p = TextPaint(
style: const TextStyle(
fontSize: 32,
color: Colors.white,
),
);
final pSize = p.toTextPainter('Test').size;
print(pSize);
TextComponent(text: 'Test', textRenderer: p);
import 'dart:async';
import 'package:flame/components.dart';
import 'package:flame/game.dart';
import 'package:flame/input.dart';
import 'package:flutter/material.dart';
void main() {
runApp(
SafeArea(child: GameWidget(game: TextExample())),
);
}
class TextExample extends FlameGame {
TextExample() {
debugMode = true;
}
static final textPaint = TextPaint(
style: const TextStyle(
fontSize: 32,
color: Colors.white,
),
);
@override
Future<void> onLoad() async {
add(Child(position: Vector2(size.x / 2, size.y / 2)));
// buttonを引数として渡さない場合
add(_MyButton(
onPressed: () {
removeAll(children);
},
));
// buttonを引数として渡す場合
// こちらはsizeがコンストラクタ(Initializer)にて設定される
add(ButtonComponent(
position: Vector2(size.x / 2, 0),
button:
TextComponent(text: 'Remove2', textRenderer: TextExample.textPaint),
buttonDown: TextComponent(
text: 'Remove2',
textRenderer: TextExample.textPaint
.copyWith((style) => style.copyWith(color: Colors.black))),
onPressed: () {
removeAll(children);
},
));
}
}
class _MyButton extends ButtonComponent {
_MyButton({super.onPressed});
@override
FutureOr<void> onLoad() {
button =
TextComponent(text: 'Remove1', textRenderer: TextExample.textPaint);
buttonDown = TextComponent(
text: 'Remove1',
textRenderer: TextExample.textPaint
.copyWith((style) => style.copyWith(color: Colors.black)));
// sizeを設定しないとsizeがゼロの状態のためボタンが反応しない
//size = button!.size;
}
}
class Child extends SpriteComponent {
Child({super.position, super.key})
: super(
size: Vector2.all(100),
anchor: Anchor.center,
);
@override
Future<void> onLoad() async {
sprite = await Sprite.load('flame.png');
}
}