Howdy, Stranger!

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

How can I enable/desable a button?

edited June 2014 in Questions Posts: 211

Guys

I'm trying to do a simple game using buttons, but, when I tap the screen on button.x and button.y, it runs another thing

How can I desable the button?

Tagged:

Comments

  • Jmv38Jmv38 Mod
    Posts: 3,297

    give the button a enabled property:

    function Button:setup()
    ....
    self.enabled = false
    ....
    

    and when you check if this button is touched, add at the beginning:

    function Button:touched(touch)
    if not self.enabled then return end
    .....
    
  • edited June 2014 Posts: 211

    @Jmv38 here is my code:

    -- AmarganA - Musical
    displayMode(FULLSCREEN)
    supportedOrientations(LANDSCAPE_ANY)
    -- Use this function to perform your initial setup
    function setup()
    
        objectTable = {}
        lastGenerated = ElapsedTime
        interval = 0.5
        num = 5
        table.insert(objectTable,Objects(math.random(1,WIDTH-1),math.random(1,4),num))
    
        status = 0
    
        et=ElapsedTime+1
        delay=5
    
        img = readImage("Documents:AmarganA_Header")
        button = vec2(WIDTH - 890, HEIGHT - 60)
    
        img1 = readImage("Cargo Bot:Menu Button")
        button1 = vec2(WIDTH - 890, HEIGHT - 45)
    
        img2 = readImage("Documents:Botao_Facil")
        button2 = vec2(120, 350)
    
        img3 = readImage("Documents:Botao_Medio")
        button3 = vec2(500, 350)
    
        img4 = readImage("Documents:Botao_Dificil")
        button4 = vec2(900, 350)
    
        img5 = readImage("Platformer Art:Hill Tall")
        button5 = vec2(500, 500)
    
    end
    
    -- This function gets called once every frame
    function draw()
    
        if status == 0 then
            t=et+delay-ElapsedTime
            if t>1 then
                -- This sets a dark background color
                background(255, 255, 255, 255)
                -- sprite("Documents:AmarganA_Header", WIDTH/2, HEIGHT/2, WIDTH*0.5, HEIGHT)
    
                pushStyle()
                spriteMode(CENTER)
                randomObjectGenerate()
                popStyle()
    
                sprite("Documents:Owl", 200, HEIGHT - 500)
                font("HoeflerText-Black")
                fontSize(130)
                textAlign(CENTER)
                -- text("\n\n\n\nMusical", WIDTH/2, HEIGHT/2)
                text("AmarganA\n\n\n\nMusical", WIDTH/2, HEIGHT/2)
                font("HelveticaNeue-UltraLight")
                fontSize(15)
                fill(0, 0, 0, 255)
                text("© 2014 Erick Mitsugui Yamato", 100, HEIGHT-750)
                else
                if t==0 then
                    sprite("Documents:Blackboard", WIDTH/2, HEIGHT/2)
                    else
                    status = 1
                end
            end
        end
    
        if status == 1 then
            background(255, 255, 255, 255)
            sprite("Documents:Blackboard_Owl", WIDTH/2, HEIGHT-500)
            -- sprite(img, button.x, button.y)
            sprite(img2, button2.x, button2.y)
            sprite(img3, button3.x, button3.y)
            sprite(img4, button4.x, button4.y)
            font("Baskerville-Italic")
            fontSize(40)
            textAlign(CENTER)
            fill(255, 255, 255, 255)
            text("Selecione o Nível", WIDTH/2, 600)
            font("HelveticaNeue-UltraLight")
            fontSize(15)
            fill(0, 0, 0, 255)
            text("© 2014 Erick Mitsugui Yamato", 100, HEIGHT-750)
        end
    
    
        if buttonPressed2 == true then
            status = 2
            background(0, 0, 0, 255)
            sprite("Documents:Fase1", WIDTH/2, 550, WIDTH, HEIGHT/2)
            sprite("Cargo Bot:Level Select Frame", WIDTH/2, 550, WIDTH,HEIGHT/2)
            sprite(img1, button1.x, button1.y)
            sprite(img5, button5.x, button5.y)
            font("HelveticaNeue-UltraLight")
            fontSize(15)
            fill(0, 0, 0, 255)
            text("© 2014 Erick Mitsugui Yamato", 100, HEIGHT-750)
            else
            if buttonPressed1 == true then
                status = 1
            end
        end
    
    end
    
    function touched(touch)
    
        --[[
        if touch.state==ENDED then
            buttonPressed=false    -- set to false
            if touch.x>button.x-img.width/2 and touch.x<button.x+img.width/2 and
            touch.y>button.y-img.height/2 and touch.y<button.y+img.height/2 then
                buttonPressed=true     -- button was pressed, set to true
                close()
            end
        end
    
          ]]
    
        if touch.state==ENDED then
            buttonPressed1=false   -- set to false
            if touch.x>button1.x-img1.width/2 and touch.x<button1.x+img1.width/2 and
            touch.y>button1.y-img1.height/2 and touch.y<button1.y+img1.height/2 then
                buttonPressed1=true     -- button was pressed, set to true
                sound("Game Sounds One:Menu Select")
            end
        end
    
    
        if touch.state==ENDED then
            buttonPressed2=false   -- set to false
            if touch.x>button2.x-img2.width/2 and touch.x<button2.x+img2.width/2 and
            touch.y>button2.y-img2.height/2 and touch.y<button2.y+img2.height/2 then
                buttonPressed2=true     -- button was pressed, set to true
                sound("Game Sounds One:Menu Select")            
            end
        end
    
        if touch.state==ENDED then
            buttonPressed3=false   -- set to false
            if touch.x>button3.x-img3.width/2 and touch.x<button3.x+img3.width/2 and
            touch.y>button3.y-img3.height/2 and touch.y<button3.y+img3.height/2 then
                buttonPressed3=true     -- button was pressed, set to true
                sound("Game Sounds One:Menu Select")            
            end
        end
    
        if touch.state==ENDED then
            buttonPressed4=false   -- set to false
            if touch.x>button4.x-img4.width/2 and touch.x<button4.x+img4.width/2 and
            touch.y>button4.y-img4.height/2 and touch.y<button4.y+img4.height/2 then
                buttonPressed4=true     -- button was pressed, set to true
                sound("Game Sounds One:Menu Select")            
            end
        end
    
        if touch.state==ENDED then
            buttonPressed5=false   -- set to false
            if touch.x>button5.x-img5.width/2 and touch.x<button5.x+img5.width/2 and
            touch.y>button5.y-img5.height/2 and touch.y<button5.y+img5.height/2 then
                buttonPressed5=true     -- button was pressed, set to true
                music("Dropbox:C2")
            end
        end
    
    end
    
    function randomObjectGenerate()
        for k,v in pairs(objectTable) do
            v:draw()
        end
        if ElapsedTime - lastGenerated > interval then
            lastGenerated = ElapsedTime
            num = num + 1
            table.insert(objectTable,Objects(math.random(1,WIDTH-1),math.random(1,4),num))
        end
        for k,v in pairs(objectTable) do
            if v.a.y < 0 or (v.a.x > WIDTH or v.a.x< 0) or v.a.info.id == 0 then
                table.remove(objectTable,k)
                v.a:destroy()
                v.a = nil
                v=nil
            end
        end
    end
    
    
    --Objects
    
    Objects = class()
    
    function Objects:init(x,cat,id)
        -- you can accept and set parameters here
    
        self.a = physics.body(CIRCLE,20)
        self.a.x = x
        self.a.y = HEIGHT
        --self.a.gravityScale = 1
        self.a.gravityScale = 1
        self.a.restitution = 0.0
        self.a.interpolate = true
        self.asleepingAllowed = true
        self.flag = true
        self.category = cat
        self.a.info = {}
        self.a.info.id = id
    
    end
    
    function Objects:draw()
        pushMatrix()
        translate(self.a.x, self.a.y)
        if self.category == 1 then
            --sprite("Cargo Bot:Condition Blue",0,0,50,50)
            --sprite("Documents:Nota",0,0,50,50)
            font("AppleColorEmoji")
            fontSize(72)
            text("
    
    
    
  • Jmv38Jmv38 Mod
    Posts: 3,297

    @erickyamato sorry but your code is incomplete and i cant run it...

  • Posts: 2,051

    @erickyamato, please please please learn how to format code

  • edited June 2014 Posts: 223
    The user and all related content has been deleted.
  • edited June 2014 Posts: 2,051

    @NatTheCoder, great example, except he can't see what you typed to get that to show.

    Here's how it is done:

    ~~~

    The Code

    ~~~

    To get:

    The Code
    
  • edited June 2014 Posts: 1,976

    You can post code like this:

    ~~~

    Your Code Here

    ~~~

    Edit: I posted this about 15 seconds after @JakAttak...

  • dave1707dave1707 Mod
    Posts: 10,052

    @erickyamato You were missing the 3 ~'s at the end of your code, so it didn't format correctly. That probably happened because your code was too long and the end of the code didn't post. I added the 3 ~'s at the end to format what was there. You could post just the missing code if you want to.

  • The user and all related content has been deleted.
  • Posts: 211

    Sorry guys!

    @Dave1707 was right! The code is too long...

    Thank you @Dave1707

  • Posts: 1,976

    @NatTheCoder Long press on the ' (apostrophe) key, and choose the far left sub-key.

    It's a shortcut for the <.code> tag <./code> (Without the .s)

Sign In or Register to comment.