TOP(About this memo)) > Flutter > 一覧(Flame) > 各種ユーティリティ(未整理)
class Rock extends SpriteComponent with TapCallbacks {
//...
@override
void onTapDown(_) {
add(
ScaleEffect.to(
Vector2.all(scale.x >= 2.0 ? 1 : 2),
EffectController(duration: 0.3),
),
);
}
}
// 実装
mixin HasVisibility on Component {
bool isVisible = true;
@override
void renderTree(Canvas canvas) {
if (isVisible) {
super.renderTree(canvas);
}
}
}
@override
void onTapDown(TapDownEvent event) {
player.add(
MoveToEffect(
event.localPosition,
EffectController(
duration: 1.0,
),
),
);
}
import 'dart:async';
import 'dart:math';
import 'package:flame/components.dart';
import 'package:flame/events.dart';
import 'package:flame/game.dart';
import 'package:flame/rendering.dart';
import 'package:flutter/material.dart';
void main() {
runApp(
SafeArea(child: GameWidget(game: DecoratorExample())),
);
}
class DecoratorExample extends FlameGame with DragCallbacks, ScaleDetector {
DecoratorExample() {
debugMode = true;
}
@override
FutureOr<void> onLoad() {
final p = Child(position: Vector2(size.x / 2, size.y / 2));
add(p);
}
}
class Child extends SpriteComponent with TapCallbacks {
Child({
super.position,
super.key,
}) : super(
size: Vector2.all(100),
anchor: Anchor.center,
);
final decoratorList = <Decorator>[
PaintDecorator.blur(3.0),
PaintDecorator.grayscale(opacity: 0.5),
PaintDecorator.tint(const Color(0xAAFF0000)),
Rotate3DDecorator(
center: Vector2(50, 50),
angleX: pi / 4,
perspective: 0.01,
),
Shadow3DDecorator(
base: Vector2(100, 150),
angle: -1.4,
xShift: 200,
yScale: 1.5,
opacity: 0.5,
blur: 1.5,
),
];
int index = 0;
@override
Future<void> onLoad() async {
sprite = await Sprite.load('flame.png');
}
@override
void onTapUp(TapUpEvent event) {
if (index > 0) decorator.removeLast();
decorator.addLast(decoratorList[(index++) % decoratorList.length]);
}
}
void main() {
runApp(
SafeArea(child: GameWidget(game: DecoratorExample())),
);
}
class DecoratorExample extends FlameGame with DragCallbacks, ScaleDetector {
DecoratorExample() {
debugMode = true;
}
@override
FutureOr<void> onLoad() {
for (final anchor in Anchor.values) {
add(AlignComponent(
child: TextComponent(text: anchor.toString()), alignment: anchor));
}
}
}