【发布时间】:2014-10-14 13:16:45
【问题描述】:
我要制作一个隐藏按钮,当您在它上方徘徊时会显示它,然后在短按它时亮起(在我的脚本 atm 中为 2 秒),然后运行一个函数。我实施了一种对我来说似乎合乎逻辑的机制来实现这一目标。我得到一个变量引用错误。
这是我的按钮类:
class Button:
def __init__(self, imgInAc, imgAc, posX, posY, width, height, function, gameInstance, hiddenButton):
self.imgInAc = pygame.image.load(imgInAc)
self.imgAc = pygame.image.load(imgAc)
self.posX = posX
self.posY = posY
self.w = width
self.h = height
self.function = function
self.gameInstance = gameInstance
self.hiddenButton = hiddenButton
# Make clickDump false, to make sure, refTiem gets measured only once after the button press,
# because otherweise it would get overwritten over and over again while the game loop is running
self.clickDump = False
def button(self):
# Measure the current time to later run the button function while displaying the active image before doing so.
curTime = time.clock()
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
if self.posX+self.w > mouse[0] > self.posX and self.posY+self.h > mouse[1] > self.posY:
if self.hiddenButton == False:
self.gameInstance.gameDisplay.blit(self.imgAc,(self.posX,self.posY))
if click[0] == 1:
buttonFunctions.buttonFunctions(self.function)
# If the button is a ahidden one, go through teh following protocol
elif self.hiddenButton == True:
# Display the off version if the mouse hovers over it
self.gameInstance.gameDisplay.blit(self.imgInAc,(self.posX,self.posY))
if click[0] == 1:
# if you click on it, display teh active version
self.gameInstance.gameDisplay.blit(self.imgAc,(self.posX,self.posY))
###print curTime
# If clickDump is false, defien teh reference time relative to teh current time.
# Then make it true, so the ref time doesn't get changed again, while the game loop is running
if self.clickDump == False:
refTime = time.clock()+2
###print refTime
self.clickDump = True
# If ClickDump is true, check if teh time condition is satiesfied. Note, that this check is run outside the "if click[0] == 1"-scope,
# to make sure, it also gets checked if the click happened in a previoues cycle.
if self.clickDump == True:
# If the time interval between the click event and the current time has elapsed
# set clickDUmp back to flase, so it can be used for teh next button press protocol
# and finally run the button function.
if curTime > refTime:
self.clickDump = False
buttonFunctions.buttonFunctions(self.function)
else:
if self.hiddenButton == False:
self.gameInstance.gameDisplay.blit(self.imgInAc,(self.posX,self.posY))
错误是:
"...第 296 行,在按钮中
如果 curTime > refTime:
UnboundLocalError: 在赋值之前引用了局部变量 'refTime'"
我真的不明白这一点,因为我认为我只执行如果 refTime 已初始化,则比较时间变量的部分,因为条件或运行比较 onyl 在 refTime 被分配其值的部分得到满足.
抱歉,我的评论中可能存在大量拼写错误……我只是为自己写的。
所以……我很困惑。
【问题讨论】:
-
只有在
self.clickDump == False时设置refTime(严格来说,应该是if not self.clickDump:),并且只有在@987654325 时才get @(同样,应该是if self.clickDump:)。他们不可能都是真的。 -
但这就是我想做的。 ...?不是吗。我的逻辑错误在哪里?
-
通常,局部变量在函数结束时不再存在。如果您在第一次调用
button时创建了refTime,那么当您再次调用button时它就不会存在了。 -
您应该使用
if self.clickDump:而不是if self.clickDump == True:。False也是如此:if not self.clickDump:。 -
啊!一定是这样。谢谢!
标签: python class variables time variable-assignment