android 如何使用Kivy修复双击速度距离?

9bfwbjaz  于 2023-01-11  发布在  Android
关注(0)|答案(1)|浏览(136)

我试图创建一个单击和双击输入,我希望发生的是,当我双击它不获得单击的值。
这就是我所做的。我试着输入“if not touch.is_double_tap:“,但它仍然显示单击值。我甚至试着用open('save_file. csv','a',newline ='')将“注解为f:print(f“Made,{x},{y}",file=f)”在if not touch.is_double_tap下方。仍在获取该值。

def on_touch_down(self, touch):
        d = 10

        if self.collide_point(*touch.pos):
            self.canvas.add(Color(rgb=(46 / 255.0, 172 / 255.0, 88 / 255.0)))
            self.canvas.add(Ellipse(pos=(touch.x - d / 2, touch.y), size=(d, d)))

            x = touch.x - self.pos[0]
            y = self.size[1] - touch.y + self.pos[1]  # modified line

            # Don't write to the file if the touch event is a double tap
            if not touch.is_double_tap:
                with open('save_file.csv', 'a', newline='') as f:
                    print(f"Made,{x},{y}", file=f)
                # Increment the shots made counter
                self.shots_made += 1

            if touch.is_double_tap:
                self.canvas.add(Color(rgb=(220 / 255.0, 8 / 255.0, 8 / 255.0)))
                d = 10
                self.canvas.add(Ellipse(pos=(touch.x - d / 2, touch.y), size=(d, d)))
                self.shots_missed += 1
                with open('save_file.csv', 'a', newline='') as f:
                    print(f"Missed,{x},{y}", file=f)

可能的解决方案是什么?

fjaof16o

fjaof16o1#

问题是双击由两次单击组成,而在第一次单击时,你不知道是单击还是双击,所以你无法对第一次单击立即做出React。
您可以在注册单次点击时计划事件(do_your_single_tap_thing),以便在未来1秒内执行。如果在1秒内没有注册第二次点击事件,则将执行此事件。如果在1秒内有第二次点击,则删除计划事件并执行do_your_double_tap_thing

scheduled_event = None
def on_touch_down(self, touch):
    if touch.is_double_tap:
        if self.scheduled_event is not None:
            # delete the single tap event so it is never executed
            self.scheduled_event.cancel()                 
            self.scheduled_event = None
            do_your_double_tap_thing()
    else:
        double_tap_time_sec = 1 
        # alternatively, you can pull the double tap wait time 
        # from the config: Config.getint('postproc', 'double_tap_time')/1000.
        self.scheduled_event = Clock.schedule_once(do_your_single_tap_thing, double_tap_time_sec)

def do_your_single_tap_thing(self, *args):
    print("Single tap")

def do_your_double_tap_thing(self, *args):
    print("Double tap")

相关问题