Howdy, Stranger!

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

About touched(t) for button in table

Hi sorry to trouble all dear.

I hv a seriers button , same as button class iin cargobot, i put them in a table ,wchich is button24

I init each button and insert them into button by order. In. For...in ipair ....
Also i sprite the button img for each button in draw. Until now ,all can work well.

Also there is a single button named buttonA,not belong to the table above .

In bottom, function touched(t)
I pass t to buttonA by: buttonA:touched(t)
In draw i make its function: buttonA:onBegan =function(buttonA,t)
This button can work well .

Also in touched(t)
I do :
For i,v in ipair(button24)
button24[i]:touched(t) end. ( I don't know the problem is fm here or the following...)

In draw, i do:
For i,v in ipair(button24)
button24[i].onBegan = function(button24[i],t) ????
sound(asoundhere in pack) end

Red error always show the line number on the line with ????

Where am i wrong? Or cannot pass t to the button in table ? Or how to pass in ???

Thanks a lot.

Tagged:

Comments

  • dave1707dave1707 Mod
    Posts: 6,486

    @totorofat The variable t is only usable in the function touched the way it’s being used. The variable t that you’re using in draw isn’t defined so that’s probably why you’re getting an error. That’s what I see from the amount of code you’re showing.

  • thanks dave... finially I solved it in midnight.
    in this line : button24[i].onBegan = function(button24[i],t) ????

    as I suddenly found I wrote button24[i]:onBegan
    it is : , not a dot.

    But I wrote the dot in this post. so in the post it is right.

    the small dot stopped my working since 4pm to midnight, the crazy dot.

    I think , if the error in red shows only a number in red tag , it will means the format of this line has problem, such as : to ., missing [ or ], missing or exceeded end, missing something in format , right ?

  • edited December 2017 Posts: 93

    I have a little PGM here, and i want to change the touch function from touch.screen to touch.button, please help anyone. Once i figure this out i will be able to apply it all of my PGMs.. Below is a sample in which i have already drawn the button, i just need to change screen.touch to button.touch...

    -- Shuffle PGM
    
    displayMode(FULLSCREEN)
    supportedOrientations(LANDSCAPE_ANY)
    
    function setup()
       s={" ♥️"," ♠️"," ♦️"," ♣️"}
       v={" 2"," 3"," 4"," 5"," 6"," 7"," 8"," 9","10"," J"," Q"," K"," A"}
       deck={}
       for z=0,51 do
           table.insert(deck,v[z%13+1]..s[z//13+1])
       end
    end
    
    function draw()
       background(28, 32, 139, 255)
       font("Georgia-BoldItalic")
       fontSize(40)
       fill(255)
       text("Tap Button to shuffle",WIDTH/2,HEIGHT-50)
       for z=0,51 do
           text(deck[z+1],275+(z//13)*150,50+(z%13)*50)
       end
        stroke(255, 0, 8, 255)
        strokeWidth(4)
        ellipse(100, 400,150)
        font("AmericanTypewriter-Bold")
        fill(0, 0, 0, 255)
        fontSize(35)
        text("Shuffle", 100, 400)
    end
    
    
    function shuffle()
       for q=1,100 do
           a,b=math.random(52),math.random(52)
           deck[a],deck[b]=deck[b],deck[a]
       end
    end
    
    function touched(t)
       if t.state==BEGAN then
         sound("Game Sounds One:Bell 2")
           shuffle()
       end
    end
    
  • dave1707dave1707 Mod
    Posts: 6,486

    @kendog400 Since you already have the button drawn, all you need to do in the touched function (BEGAN) is to check if the t.x and t.y values are within the circle. Instead of writing code to check a circle, you can imagine a square that covers some of the circle and check if t.x and t.y are within the square.

  • Could you hit me with a quick example, i look on the fourm, i see a lot of posts, I'm still having trouble comprehending, i'll be ok once i get my roll on...
    Thanks in advanced !.....ps : (this is my first button)

  • dave1707dave1707 Mod
    edited December 2017 Posts: 6,486

    @kendog400 Here’s the touched function. I’m thinking of a square inside the circle and using the size of the square for the Button.

    PS. You can use a calculation for a circle to use the full size of the circle, but using a square inside the circle is good enough for now.

    function touched(t)
        if t.state==BEGAN then
            if t.x>45 and t.x<155 and t.y>345 and t.y<455 then
                sound("Game Sounds One:Bell 2")
                shuffle()
            end
        end
    end
    
  • Got it now, thank you again !

Sign In or Register to comment.