簡體   English   中英

繪制中的 Def mouseClicked() 不起作用,我該如何解決?

[英]Def mouseClicked() in draw not working, how do I fix it?

我正在制作一個有趣的 dnd 應用程序,char 類是每個字符的內容,但我似乎無法開始工作

我試過注釋掉部分代碼,但似乎是init函數的問題

這是我的代碼:

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)


預期結果:當我按下鼠標時,我應該將 h 或 n 打印到控制台,當我釋放它時,我應該得到 o

實際結果:無

編輯:澄清,
Draw 每幀調用一次
一開始就設置一次
mouseClicked 當鼠標被點擊時
和 mouseReleased 當鼠標被釋放

好的,所以我不得不將鼠標單擊和釋放的功能放在底部並將 for 循環放在其中,但它解決了我的問題

更新后的代碼是

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM