我正在制作游戏,我想要做的是在移动角色时检查角色位置是否接近任何对象位置,此时我想触发我的对话窗口弹出。我遇到的问题是,由于“检查字符位置是否接近对象位置”被绑定到我的key_press,它会运行这一千次,并在按住键时多次调用该函数。我无法想出解决方案。我试图做的是在函数外部添加一个计数器,然后在调用函数时添加一个计数器,并说明计数器是否为< 2然后继续。此外,我甚至不确定检查窗口小部件位置是否彼此接近的实际代码是否正确。
有问题的代码一直在底部'for list in listofwidgets':
class MoveableImage(Image):
def __init__(self, **kwargs):
super(MoveableImage, self).__init__(**kwargs)
self._keyboard = Window.request_keyboard(None, self)
if not self._keyboard:
return
self._keyboard.bind(on_key_down=self.on_keyboard_down)
self._keyboard.bind(on_key_up=self.on_keyboard_up)
self.y = (Window.height/2.1)
self.app = App.get_running_app()
def on_keyboard_down(self, keyboard, keycode, text, modifiers):
if keycode[1] == 'left':
self.source = 'selectionscreen/left.zip'
if self.x < (Window.width * .25):
if any(self.collide_widget(i) for i in listofwidgets):
bglayout.x +=0
else:
bglayout.x += 4
else:
if any(self.collide_widget(i) for i in listofwidgets):
self.x -=0
else:
self.x -=6
for i in listofwidget:
if i.x - self.x < 10: #if image x location and self x location are within 100 pixels
calldialoguebox()
counter=0
def calldialoguebox(*args):
counter+=1
if counter < 2:
Clock.schedule_interval(typenewmessage.example, .5) #types message in dialogue box word by word every interval
else:
pass