You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

切换方向后按键输入冻结问题排查及解决方案咨询

问题分析与解决方案

核心结论

这个现象不是Flame的既定特性,根源是操作系统的按键重复默认延迟(初始约0.5秒),加上依赖onKeyEvent的被动事件触发机制,不匹配游戏的实时按键检测需求。

问题原因

  • 操作系统默认行为:长按按键时,首次触发后会有0.5秒左右的延迟才开始持续发送重复事件,这是系统级的通用设定,和Flame无关。
  • onKeyEvent是被动接收系统推送的按键事件,无法绕过系统的重复延迟,同时多按键切换时可能出现短暂的按键状态空窗。

解决办法

游戏开发需要实时检测按键状态,而非依赖系统的重复事件触发。利用Flame的KeyboardEvents混入提供的keysPressed集合,在update方法中每帧主动检测当前按下的按键,就能实现无延迟的快速响应。

修改后的代码示例:

import 'package:flame/components.dart';
import 'package:flame/events.dart';
import 'package:flame/game.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

void main() {
  runApp(const FooApp());
}

class FooApp extends StatelessWidget {
  const FooApp({super.key});

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      debugShowCheckedModeBanner: false,
      home: GameWidget.controlled(gameFactory: FooFlameGame.new),
    );
  }
}

class FooFlameGame extends FlameGame with KeyboardEvents {
  late final TextComponent textComponent;
  final stopwatch = Stopwatch();

  @override
  Future<void> onLoad() async {
    super.onLoad();
    stopwatch.start();
    textComponent = TextComponent(
      position: Vector2(size.x * 0.5, size.y * 0.5),
      anchor: Anchor.center,
    );
    await add(textComponent);
  }

  @override
  void update(double dt) {
    super.update(dt);
    // 每帧主动获取当前所有按下的按键
    final debugNames = keysPressed.map((e) => e.debugName);
    final debugText = "${stopwatch.elapsed}: $debugNames";
    textComponent.text = debugText;
    print(debugText);
  }

  @override
  KeyEventResult onKeyEvent(
    KeyEvent event,
    Set<LogicalKeyboardKey> keysPressed,
  ) {
    return KeyEventResult.ignored;
  }
}

效果说明

  • 无论长按单键还是多键切换,都能在每帧实时获取准确的按键状态,完全避开系统的按键重复延迟。
  • 多按键操作时的状态切换会更流畅,不会出现原代码中的空按键状态输出问题。

内容的提问来源于stack exchange,提问作者shingo.nakanishi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 17:04:51