Skip to content

Latest commit

 

History

History
146 lines (111 loc) · 3.78 KB

input.md

File metadata and controls

146 lines (111 loc) · 3.78 KB

Input

Gestures

Inside package:flame/gestures.dart you can find a whole set of mixin which can be included on your game class instance to be able to receive touch input events. Bellow you can see the full list of these mixins and its 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
  - onScaleEnd

Many of these detectors can conflict with each other, for example, you can't register both Vertical and Horizontal drags, so not all of then can be used together.

All of these methods are basically a mirror from the callbacks available on the GestureDetector widget, you can also read more about Flutter's gestures here.

Example

class MyGame extends Game with TapDetector {
  // Other methods ommited

  @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}");
  }
}

You can also check a more complete example here.

Tapable components

Flame also offers a simple helper to make it easier to handle tap events on PositionComponent, by using the mixin Tapable your components can override the following methods, enabling easy to use tap events on your Component.

  void onTapCancel() {}
  void onTapDown(TapDownDetails details) {}
  void onTapUp(TapUpDetails details) {}

Minimal component example:

import 'package:flame/components/component.dart';
import 'package:flame/components/mixins/tapable.dart';

class TapableComponent extends PositionComponent with Tapable {

  // update and render omitted

  @override
  void onTapUp(TapUpDetails details) {
    print("tap up");
  }

  @override
  void onTapDown(TapDownDetails details) {
    print("tap down");
  }

  @override
  void onTapCancel() {
    print("tap cancel");
  }
}

Keyboard

Flame provides a simple way to access Flutter's features regarding accessing Keyboard input events.

To use it just add the KeyboardEvents mixin to your game class. By doing so you will need to implement the onKeyEvent method, this method is called everytime a keyboard event happens, and it receives an instance of the Flutter class RawKeyEvent, which can be used to get information about the event, like if it was a key down or key up event, the pressed key and etc.

Minimal example:

import 'package:flame/game.dart';
import 'package:flame/keyboard.dart';
import 'package:flutter/services.dart';

class MyGame extends Game with KeyboardEvents {
  // update and render omitted

  @override
  void onKeyEvent(e) {
    final bool isKeyDown = e is RawKeyDownEvent;
    print(" Key: ${e.data.keyLabel} - isKeyDown: $isKeyDown");
  }
}

You can also check a more complete example here.