Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion

Disable Other Buttons For A Duration

in General Posts: 5

Once again, title pretty much says it all. I have three buttons, and I want it so that when i press “Attack”, “defend” will be disabled until “Attack” returns to its blue state, and vice versa. Also, Heals can only be pressed when both are useable

function setup()

    buttonTab = {

    DelayButton(WIDTH/2, HEIGHT - 60, WIDTH, 50, "Attack", function() bfunc("Atk") end, 1),
    DelayButton(WIDTH/2, HEIGHT - 120, WIDTH, 50, "Defend", function() bfunc("Def") end, 3),
    DelayButton(WIDTH/2, HEIGHT - 180, WIDTH, 50, "Heal", function() bfunc("HP") end, 9),

    }

end

function draw()

    background(40, 40, 50)

    for t = 1,#buttonTab do

        buttonTab[t]:draw()

    end

end

function touched(t)

    for w = 1,#buttonTab do

        buttonTab[w]:touched(t)

    end

end

function bfunc(id)

    print("Pressed button "..id)

    if id == 1 then

        buttonTab[2].canPress = false

    end

end

--[[break]]--

DelayButton = class()

function DelayButton:init(x,y,w,h,name,func,delaytime)

    self.x,self.y,self.w,self.h,self.name,self.func = x,y,w,h,name,func

    self.c = color(255, 255, 255, 255)

    self.isClicked = false
    self.fs = 15

    self.canPress = true
    self.showline = true

    self.delaytime = delaytime or 0

    self.bardims = {w = self.w - 25, h = 3,r = 0,g = 127,b = 255}

    color(0, 98, 255, 255)

end

function DelayButton:draw()

    pushStyle()

    if self.showline then

        stroke(self.c)
        fontSize(15)
        font("AmericanTypewriter")
        strokeWidth(.75)
        lineCapMode(ROUND)

        line(self.x-self.w/2,self.y-self.h/2,self.x-self.w/2,self.y+self.h/2)
        line(self.x+self.w/2,self.y-self.h/2,self.x+self.w/2,self.y+self.h/2)

        line(self.x-self.w/2+7.5,self.y-self.h/2,self.x+self.w/2-7.5,self.y-self.h/2)
        line(self.x-self.w/2+7.5,self.y+self.h/2,self.x+self.w/2-7.5,self.y+self.h/2)

    end

        fontSize(self.fs)
        fill(255)

    text(self.name, self.x, self.y)

    if self.isClicked then

        pushStyle()

        rectMode(CENTER)
        fill(255)
            rect(self.x,self.y,self.w + 2,self.h + 3)

            fill(0)
            fontSize(self.fs)
            text(self.name,self.x,self.y)

            popStyle()

        end

        rectMode(CENTER)
        fill(self.bardims.r, self.bardims.g, self.bardims.b)
        rect(self.x, self.y-self.h/3,self.bardims.w,self.bardims.h)

    popStyle()

end

function DelayButton:touched(t)

    if t.x > self.x-self.w/2 and

    t.x < self.x+self.w/2 and

    t.y > self.y-self.h/2 and

    t.y < self.y+self.h/2 and

    t.state == BEGAN and self.canPress then

        self.isClicked = true

    end

    if t.x > self.x-self.w/2 and

    t.x < self.x+self.w/2 and

    t.y > self.y-self.h/2 and

    t.y < self.y+self.h/2 and

    t.state == ENDED and self.canPress then

        self.isClicked = false
        self.canPress = false

        tween.delay(self.delaytime, function() self.canPress = true end)
        tween(0.001, self.bardims, {w=0, r = 127, g = 127, b = 127},tween.easing.bounceIn, function()

            self.func()

            tween(self.delaytime, self.bardims, {w = self.w - 30}, tween.easing.quadInOut, function()

                self.canPress = true
                self.bardims.r = 0
                self.bardims.g = 127
                self.bardims.b = 255

            end)
        end)
    end
end

function DelayButton:UpdateName(Value)

    local NewName = Value

    self.name = NewName

end

Comments

  • dave1707dave1707 Mod
    Posts: 7,836

    Here’s a quick example of disabling buttons for a given time.

    function setup()
        rectMode(CENTER)
        a=button(WIDTH/2,400,"Active")
        d=button(WIDTH/2,300,"Defend")
        h=button(WIDTH/2,200,"Heal")  
        timer=0  
    end
    
    function draw()
        background(0)
        a:draw()
        d:draw()
        h:draw() 
        if timer>0 then
            timer=timer-1
            if timer==0 then
                a.active=true
                d.active=true
                h.active=true
            end
        end
        text("Timer "..timer,WIDTH/2,HEIGHT-200)
    end
    
    function touched(t)
        a:touched(t)
        d:touched(t)
        h:touched(t)        
    end
    
    button=class()
    
    function button:init(x,y,n)
        self.x=x
        self.y=y
        self.name=n
        self.active=true
    end
    
    function button:draw()
        if self.active then
            fill(0,255,0)
        else
            fill(255,0,0)
        end
        rect(self.x,self.y,100,50)
        fill(255)
        text(self.name,self.x,self.y)
    end
    
    function button:touched(t)
        if t.state==BEGAN then
            if t.x>self.x-50 and t.x<self.x+50 and t.y>self.y-25 and t.y<self.y+25 then
                if self.name=="Active" and self.active then
                    timer=300
                    d.active=false
                    h.active=false
                end
                if self.name=="Defend" and self.active then
                    timer=300
                    a.active=false
                    h.active=false
                end
                if self.name=="Heal" and self.active then
                    timer=300
                    a.active=false
                    d.active=false
                end
            end
        end
    end
    
Sign In or Register to comment.