[英]Def mouseClicked() in draw not working, how do I fix it?
I am making a dnd application for fun, the char class is what each character is, but I can't seem to get to work我正在制作一个有趣的 dnd 应用程序,char 类是每个字符的内容,但我似乎无法开始工作
I have tried commenting out sections of code, yet it seems to be a problem with the init function我试过注释掉部分代码,但似乎是init函数的问题
This is my code:这是我的代码:
global mouseY
global x
x = 0
chars =[]
def setup():
global tsize
tsize = 50
def draw():
global x
if x == 0:
def mouseon(x,y,xs,ys):
return(mouseX <= xs and mouseX >= x and mouseY <= ys and mouseY >= y)
class char:
def __init__(self, name, HP, img, CLASS, SIZE, id):
self.xp = 0
self.yp = 0
self.SIZE = SIZE
self.name = name
self.HP = HP
self.img = img
self.CLASS = CLASS
self.id = id
self.Hover = False
chars.append(self)
dog = char("john", 100, "x.png", "archer", 50, 0)
print("l")
x = 2
else:
fill(255)
background(255)
stroke(0)
strokeWeight(1)
for i in range(height/50):
for j in range(width/50):
rect(i*50,j*50,49.5,49.5)
for i in chars:
def mouseClicked():
if mouseon(i.xp,i.yp,i.xp+i.SIZE,i.yp+i.SIZE):
i.Hover = True
print('h')
else:
print("n")
i.Hover = False
def mouseReleased():
print("o")
i.Hover = False
if i.Hover:
fill(0)
rect(i.xp+1, i.yp+1, i.xp+i.SIZE+1, i.yp+i.SIZE+1)
fill(0,255,0)
strokeWeight(0)
rect(i.xp, i.yp, i.xp+i.SIZE, i.yp+i.SIZE)
expected result: when I press the mouse, I should get h or n printed to the console, and when I release it, I should get o预期结果:当我按下鼠标时,我应该将 h 或 n 打印到控制台,当我释放它时,我应该得到 o
Actual results: nothing实际结果:无
EDIT: to clarify,编辑:澄清,
Draw is called once every frame Draw 每帧调用一次
setup once at the beginning一开始就设置一次
mouseClicked when the mouse is clicked mouseClicked 当鼠标被点击时
and mouseReleased when the mouse is released和 mouseReleased 当鼠标被释放
okay, so I had to put the mouse clicked and released functions at the bottom and put the for loops in those, but it solved my problem好的,所以我不得不将鼠标单击和释放的功能放在底部并将 for 循环放在其中,但它解决了我的问题
the updated code is更新后的代码是
global mouseX
global mouseY
global x
x = 0
chars =[]
def setup():
global tsize
tsize = 50
def draw():
global x
if x == 0:
global mouseon
def mouseon(x,y,xs,ys):
return(mouseX <= xs and mouseX >= x and mouseY <= ys and mouseY >= y)
class char:
def __init__(self, name, HP, img, CLASS, SIZE, id):
self.xp = 0
self.yp = 0
self.SIZE = SIZE
self.name = name
self.HP = HP
self.img = img
self.CLASS = CLASS
self.id = id
self.Hover = False
chars.append(self)
dog = char("john", 100, "x.png", "archer", 50, 0)
print("l")
x = 2
else:
fill(255)
background(255)
stroke(0)
strokeWeight(1)
for i in range(height/50):
for j in range(width/50):
rect(i*50,j*50,49.5,49.5)
for i in chars:
if i.Hover:
fill(0)
rect(i.xp+1, i.yp+1, i.xp+i.SIZE+1, i.yp+i.SIZE+1)
fill(0,255,0)
strokeWeight(0)
rect(i.xp, i.yp, i.xp+i.SIZE, i.yp+i.SIZE)
def mouseClicked():
for i in chars:
if mouseon(i.xp,i.yp,i.xp+i.SIZE,i.yp+i.SIZE):
i.Hover = True
print('h')
else:
print("n")
i.Hover = False
def mouseReleased():
for i in chars:
print("o")
i.Hover = False
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.