class SampleGame extends FlameGame with HasCollisionDetection, HasKeyboardHandlerComponents { @override Color backgroundColor() => const Color(0xffCCCCFF); @override Future<void> onLoad() async { await super.onLoad(); add(MySprite(Vector2(300, 300), true)); add(MySprite(Vector2(100, 100), false)); } } class MySprite extends CircleComponent with CollisionCallbacks, KeyboardHandler { var _size = 100.0; Vector2 _delta = Vector2.zero(); late Color _color; late Vector2 _position; late bool _stay; MySprite(this._position, this._stay): super(); @override Future<void> onLoad() async { await super.onLoad(); _color = _stay ? Colors.green : Colors.red; size = Vector2(_size, _size); position = _position; setColor(_color); CircleHitbox hitbox = CircleHitbox(); add(hitbox); } @override bool onKeyEvent( RawKeyEvent event, Set<LogicalKeyboardKey> keysPressed, ) { if (this._stay) { return false; } if (event is RawKeyUpEvent) { _delta = Vector2.zero(); } if (event.character == 'j') { _delta.x = -1; } if (event.character == 'l') { _delta.x = 1; } if (event.character == 'i') { _delta.y = -1; } if (event.character == 'k') { _delta.y = 1; } return true; } @override void update(double delta){ super.update(delta); position += _delta * delta * 100; } @override void onCollision(Set<Vector2> points, PositionComponent other) { setColor(Colors.yellow); } @override void onCollisionEnd(PositionComponent other){ setColor(_color); } }