flutter - 如何检测带有颤动/火焰的游戏中的滑动
问题描述
我想创造一个充满火焰的游戏。对于这个游戏,我想检测滑动。
我可以在教程的帮助下实现点击识别。但是我无法通过滑动检测来实现它。
我使用 Taprecognition 的主要功能如下所示: 我的主要功能是
void main() async{
Util flameUtil = Util();
await flameUtil.fullScreen();
await flameUtil.setOrientation(DeviceOrientation.portraitUp);
GameManager game = GameManager();
runApp(game.widget);
TapGestureRecognizer tapper = TapGestureRecognizer();
tapper.onTapDown = game.onTapDown;
flameUtil.addGestureRecognizer(tapper);
}
在我的 GameManager 课程中,我确实有:
class GameMAnager extends Game{
// a few methods like update, render and constructor
void onTapDown(TapDownDetails d) {
if (bgRect.contains(d.globalPosition)) { //bgRect is the background rectangle, so the tap works on the whole screen
player.onTapDown();
}
}
我的播放器类包含:
void onTapDown(){
rotate();
}
现在我想将其更改为沿滑动方向而不是 onTapDown 旋转。我试图以某种方式添加
GestureDetector swiper = GestureDetector();
swiper.onPanUpdate = game.onPanUpdate;
对我的主要和
void onPanUpdate() {
}
到我的gameManager类。但我找不到任何类似于 TapDownDetails 的平移。
对此有何建议?
我看到了一些帮助,将小部件包装在 GestureDetector 中并像这样使用它:
GestureDetector(onPanUpdate: (details) {
if (details.delta.dx > 0) {
// swiping in right direction
}
});
但我无法让它在我的项目中发挥作用。
解决方案
您可以使用 HorizontalDragGestureDetector (或 PanGestureRecognizer 如果您需要两个轴)在您的主要方法中使用以下内容
HorizontalDragGestureRecognizer tapper = HorizontalDragGestureRecognizer();
tapper.onUpdate = game.dragUpdate;
然后在您的 GameManager 中执行以下操作
void dragUpdate(DragUpdateDetails d) {
// using d.delta you can then track the movement and implement your rotation updade here
}
那应该可以解决问题:D
推荐阅读
- swift - 自动登录用户 - Swift Firebase
- r - 如何提取在一个字符之后和 R 中另一个字符最后一次出现之前发生的所有内容?
- qliksense - Qlik Sense Extension - 获取变量值
- r - 尝试使用 ggplot 主题时出现“只能合并同一类的元素”错误
- weka - 在 Weka 中仅输出问号
- javascript - React Hooks 状态没有更新为正确的输出
- c# - 将 Razor 字段对象引用到因子 C# (MS-MVC 5)
- variables - 在 Applescript 中使用自动机变量
- python - 将 API 信息写入 CSV 会丢失信息
- azure - 在创建新的 AD 本地帐户之前,使用外部 IDP 的 REST API 在 Azure B2C 中的 JIT 迁移中验证用户是否存在