The Input Input


Gestures Gestures


In the package: flame/gestures. Dart files, you can find all that can be included in your game class instance receives all touch the set of input events mixins. Here are all mixins and their methods:

- TapDetector - onTap - onTapCancel - onTapDown - onTapUp - SecondaryTapDetector - onSecondaryTapDown - onSecondaryTapUp  - onSecondaryTapCancel - DoubleTapDetector - onDoubleTap - LongPressDetector - onLongPress - onLongPressStart - onLongPressMoveUpdate - onLongPressUp - onLongPressEnd - VerticalDragDetector - onVerticalDragDown - onVerticalDragStart  - onVerticalDragUpdate - onVerticalDragEnd - onVerticalDragCancel - HorizontalDragDetector - onHorizontalDragDown - onHorizontalDragStart - onHorizontalDragUpdate - onHorizontalDragEnd - onHorizontalDragCancel - ForcePressDetector - onForcePressStart - onForcePressPeak - onForcePressUpdate - onForcePressEnd - PanDetector - onPanDown - onPanStart - onPanUpdate - onPanEnd - onPanCancel - ScaleDetector - onScaleStart - onScaleUpdate - onScaleEndCopy the code

Many of these detectors will conflict with each other, for example, you can’t register Vertical and Horizontal drags at the same time, so not everything can be used at the same time.

All of these methods are a mirror image from the GesutreDetector widget, so you can learn more about Flutter gestures here.

Example Example

    class MyGame extends Game with TapDetector {
        
        @override
        void onTapDown(TapDownDetails details){
            print("Player tap down on ${details.globalPosition.dx} - ${details.globalPosition.dy}")
        }
        
        @override
        void onTapUp(TapUpDetails details) {
            print("Player tap up on ${details.globalPosition.dx} - ${details.globalPosition.dy}"); }}Copy the code

You can learn more in this example.

Tapable Components Components that can be clicked


Flame also provides a simple helper class that makes it easier for PositionComponent to handle touch events. By using Mixin Tapable, your component can implement the following methods to make it easy to use touch events.

    void onTapCancel() {}
    void onTapDown(TapDownDetails details) {}
    void onTapUp(TapUpDetails details) {}
Copy the code

Minimum component use example:

    import 'package:flame/components/component.dart';
    import 'package:flame/components/mixins/tapable.dart';
    
    class TapableComponent extends PositionComponent with Tapable {
        
        @override
        void onTapUp(TapUpDetails details){
            print("tap up");
        }
        
        @override
        void onTapDown(TapDownDetails details){
            print("tap down");
        }
        
        @override
        void onTapCancel() {
            print("tap cancel"); }}Copy the code

The rid_device_info_keyboard Keyboard


Flame provides a simple way to access Flutter’s functions about accessing keyboard input events.

Just add the KeyboardEvents Mixin to your game class to use it. This method is called every time a keyboard event occurs. It receives an instance of the Flutter RawKeyEvent class. It can be used to get information about the event, such as a key press and release event, a key press, etc.

The simplest example:

    import 'package:flame/game.dart';
    import 'package:flame/keyboard.dart';
    import 'package:flutter/services.dart';
    
    class MyGame extends Game with KeyboardEvents {
        
        @override
        void onKeyEvent(e){
            final bool isKeyDown = e is RawKeyDownEvent;
            print(" Key: ${e.data.keyLabel} - isKeyDown: $isKeyDown"); }}Copy the code

You can see a more complete example here.