Howdy, Stranger!

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

Need help with tables

AnatolyAnatoly Mod
in Questions Posts: 848

This is my code:

CreateTopicPage = class()

function CreateTopicPage:init()
    self.text = {title = "Title here", message = "Message is here and\nnew line testnction"}
    self.buttons = {"q","w","e","r","t","y","u","i","o","p","a","s","d","f","g","h","j","k","l","z","x","c","v","b","m",",",".","?", "1", "2","3","4","5","6","7", "8", "9", "0"}
    self.xy = 64
end

function CreateTopicPage:draw()
    fill(textcolor)
    font("Arial-BoldMT")
    text(self.text.title, 500, 625)
    for x = 1, 14 do
        self.buttons[x] = {}
        for y = 1, 3 do
            self.buttons[x][y] = {}
            strokeWidth(0)
            fill(255, 255, 255, 255)
            rect(x * self.xy, y * self.xy, self.xy, self.xy)
            fill(textcolor)
            text(#self.buttons, x * self.xy + self.xy/2, y * self.xy + self.xy/2)
        end
    end
end

function CreateTopicPage:touched(t)

end

function CreateTopicPage:keyboard(key)

end

I want to know how to place instead text(#self.buttons, x * self.xy + self.xy/2, y * self.xy + self.xy/2) the letters that are called in the table self.buttons = {"q","w","e","r","t","y","u","i","o","p","a","s","d","f","g","h","j","k","l","z","x","c","v","b","m",",",".","?", "1", "2","3","4","5","6","7", "8", "9", "0"}

Tagged:

Comments

  • IgnatzIgnatz Mod
    Posts: 5,396

    try this

    function CreateTopicPage:draw()
        fill(textcolor)
        font("Arial-BoldMT")
        text(self.text.title, 500, 625)
        local n=0  --<<<<<
        for x = 1, 14 do
            self.buttons[x] = {}
            for y = 1, 3 do
                n=n+1  --<<<<
                --self.buttons[x][y] = {}  --<<<<
                strokeWidth(0)
                fill(255, 255, 255, 255)
                rect(x * self.xy, y * self.xy, self.xy, self.xy)
                fill(textcolor)
                text(self.buttons[n], x * self.xy + self.xy/2, y * self.xy + self.xy/2)
            end
        end
    end
    
  • AnatolyAnatoly Mod
    Posts: 848

    @Ignatz it is returning now this error:

    CreatePost:23: bad argument #1 to 'text' (string expected, got table)
    stack traceback:
        [C]: in function 'text'
        CreatePost:23: in method 'draw'
        Main:38: in function 'draw'
    
  • IgnatzIgnatz Mod
    Posts: 5,396

    @TokOut - you have to learn to debug code, yourself. Try to work out what is going wrong,

  • AnatolyAnatoly Mod
    Posts: 848

    What I understand: You gave me an example where self.buttons[n] is a table but a string is expected

  • IgnatzIgnatz Mod
    Posts: 5,396

    The reason is that you have TWO (2) tables with the name self.buttons. One has keyboard characters, the other is a table of x,y positions.

  • AnatolyAnatoly Mod
    Posts: 848

    It works now, how to make now the touch? @Ignatz, Thx for help!

    CreateTopicPage = class()
    
    function CreateTopicPage:init()
        self.text = {title = "Title here", message = "Message is here and\nnew line testnction"}
        self.buttons = {}
        self.letters = {"q","w","e","r","t","y","u","i","o","p","a","s","d","f","g","h","j","k","l","z","x","c","v","b","m",",",".","!","1", "2","3","4","5","6","7", "8", "9", "0","?","RET","BACK",""}
        self.xy = 64
    end
    
    function CreateTopicPage:draw()
        fill(textcolor)
        font("Arial-BoldMT")
        text(self.text.title, 500, 625)
        local n=0
        for y = 1, 3 do
            self.buttons[y] = {}
            for x = 1, 14 do
                n=n+1
                strokeWidth(0)
                fill(255, 255, 255, 255)
                rect(x * self.xy, y * self.xy, self.xy, self.xy)
                fill(textcolor)
                text(self.letters[n], x * self.xy + self.xy/2, y * self.xy + self.xy/2)
            end
        end
    end
    
    function CreateTopicPage:touched(t)
    
    end
    
    function CreateTopicPage:keyboard(key)
    
    end
    
  • IgnatzIgnatz Mod
    Posts: 5,396

    What don't you understand about touch?

  • AnatolyAnatoly Mod
    Posts: 848

    I mean how to specify the buttons action I know that you can make them with for x = the same as in draw, but how to make the action of them?

  • IgnatzIgnatz Mod
    Posts: 5,396

    I've explained this in blog posts and my ebooks, and there are lots of posts about touch.

    Please do some reading before asking someone to write the code for you. We don't have time to explain everything to every new person. We expect you to learn what you can, then try to write the code.

  • AnatolyAnatoly Mod
    Posts: 848

    Ok, where is it located?

  • IgnatzIgnatz Mod
    Posts: 5,396

    I've told you more than once where my blog posts and my ebooks are

    You can also search the forum

  • AnatolyAnatoly Mod
    Posts: 848

    @Ignatz, this error appears by using your ebook:

    Access to this link has been disabled. Please ask the owner of the shared link to send a new link to access the file or the folder.
    
  • AnatolyAnatoly Mod
    edited January 2016 Posts: 848

    So I made a the touch function, but how to make instead if a==7 and b==2 then

    Plz help, Thx!

    function CreateTopicPage:touched(t)
        for y = 1, 3 do
            self.buttons[y] = {}
            for x = 1, 14 do
                if t.x>x*self.xy and t.x<x*self.xy+self.xy and t.y>y*self.xy and t.y<y*self.xy+self.xy then
                    local a = x
                    local b = y
                    print(a .. "," .. b)
                end
            end
        end
    end
    
  • AnatolyAnatoly Mod
    Posts: 848

    Help plz!

  • dave1707dave1707 Mod
    Posts: 7,807

    It's time you start helping yourself. There's tutorials on buttons, tutorials on touch, plus plenty of examples of both of them on the forum. You've been on the forum for 2 months, and everything you're asking about is just basic programming. You didn't make a touch function, you just put a bunch of code in a function called touched(). How does the code in the question you asked relate to anything in the code you posted for touch. You want help, READ THE TUTORIALS, and practice with small programs. Once you understand the basics, you can write the programs you want without asking for help.

  • AnatolyAnatoly Mod
    Posts: 848

    I am pretty sure this could be make shorter. I made it myself ;-(

    function CreateTopicPage:touched(t)
        local n = 0
        for y = 1, 3 do
            self.buttons[y] = {}
            for x = 1, 14 do
                n = n + 1
                if t.x>x*self.xy and t.x<x*self.xy+self.xy and t.y>y*self.xy and t.y<y*self.xy+self.xy and t.state == BEGAN then
                    local y = y
                    print(x .. "," .. y)
                    if x == 14 and y == 3 then
                        -- Post
                        self:post()
                    elseif x == 13 and y == 3 then
                        -- Back
                        if self.selected == 1 then
                            if string.len(self.text.title)>=1 then
                                self.text.title = string.sub(self.text.title, 1, string.len(self.text.title)-1)
                            end
                        else
                            if string.len(self.text.message)>=1 then
                                self.text.message = string.sub(self.text.message, 1, string.len(self.text.message)-1)
                            else
                                self.selected = 1
                            end
                        end
                    elseif x == 12 and y == 3 then
                        -- Return
                        if self.selected == 1 then
                            self.selected = 2
                        else
                            self.text.message = self.text.message .. "\n"
                        end
                    else
                        if self.selected == 1 then
                            self.text.title = self.text.title .. self.letters[n]
                        else
                            self.text.message = self.text.message .. self.letters[n]
                        end
                    end
                end
            end
        end
    end
    

    The nice part is : end end end end end end (6x)

  • dave1707dave1707 Mod
    Posts: 7,807

    Why do you keep posting chunks of code that doesn't mean anything to anyone except you. If the code can be made shorter than make it shorter. I don't think anyone is going to waste their time trying to do it for you.

  • Posts: 154

    @TokOut, it looks like you answered your own question about a=7,b=2 - which is good.

    Now, making things shorter and better is a skill that can only be acquired with practice. Try it, and then when you fail, try again. At some point, if you are serious about learning to program, you will succeed.

  • AnatolyAnatoly Mod
    Posts: 848

    The nice part is here: end end end end end end

    Maybe something like
    self.letter[n]

  • AnatolyAnatoly Mod
    Posts: 848

    @mentioned {Will mention everyone}

    How to create objects? I already have the class and all the data, but how to create new objects?

  • Posts: 82
    @TokOut I'm starting to wonder if you even read the ebooks everyone constantly links you to. They're very clear, and answer a lot of the simple questions you keep posting here.

    Once more: the ebook 'Lua for beginners' by Ignatz, chapter 11, tells you all the basic stuff about classes you need to know. It can be found in the wiki or here:

    https://www.dropbox.com/sh/mr2yzp07vffskxt/AABlplSGGFBTu8FzQy94lteua/Lua for beginners.pdf
  • AnatolyAnatoly Mod
    Posts: 848

    I haven't dropbox

  • IgnatzIgnatz Mod
    Posts: 5,396

    you don't need dropbox.

    you can ignore the login message

  • edited January 2016 Posts: 2,020

    Though Dropbox is by far the easiest way of managing your Codea assets IMO, so should be something every Codea user has.

  • AnatolyAnatoly Mod
    Posts: 848

    @yojimbo2000, @Ignatz, markdown and GitHub - better then drop

  • IgnatzIgnatz Mod
    edited January 2016 Posts: 5,396

    @TokOut - of course, you would know, being such a Codea expert

  • AnatolyAnatoly Mod
    edited January 2016 Posts: 848

    Keyboard class: < Codea Ralk is better! >

    KeyBoard = class()
    
    function KeyBoard:init()
        -- You can accept and set parameyers here
        -- To make the key visible if you have in setup "k = KeyBoard()" then use "k.visible = true"
        self.visible = false
        self.textSize = 25
        self.xy = 68
        self.letters = {"1","z","a","q","2","x","s","w","3","c","d","e","4","v","f","r","5","b","g","t","6","n","h","y","7","m","j","u","8", ",","k","i","9",".","l", "o", "0", "!","?","p","_",'"',"(","BCK",";","/",")","RET"}
        self.positions = {}
    end
    
    function KeyBoard:draw()
        local n = 0
        if self.visible then
            for x = 1, 12 do
                self.positions[x] = {}
                for y = 1, 4 do
                    n = n + 1
                    fill(255, 255, 255, 255)
                    rect(x * self.xy + self.xy/2, y * self.xy + self.xy/2, self.xy, self.xy)
                    fill(0, 0, 0, 255)
                    if self.bolden then font("Arial-BoldMT") else font("ArialMT") end
                    fontSize(self.textSize)
                    text(self.letters[n], x * self.xy + self.xy, y * self.xy + self.xy)
                end
            end
        end
    end
    
    function KeyBoard:touched(t)
        if self.visible then
            local n = 0
            for x = 1, 12 do
                self.positions[x] = {}
                for y = 1, 4 do
                    n = n + 1
                    local y = y
                    if t.x>self.xy*x+self.xy/2 and t.x<self.xy*x+self.xy*1.5 and t.y>self.xy*y+self.xy/2 and t.y<self.xy*y+self.xy*1.5 and t.state == BEGAN then
                        print(self.letters[n])
                    end
                end
            end
        end
    end
    
  • IgnatzIgnatz Mod
    Posts: 5,396

    Shouldn't the BCK key delete the previous letter?

    And shouldn't the class return the whole string?

  • AnatolyAnatoly Mod
    Posts: 848

    That's just an example! How to use it will be from you. This could be also used as calculator buttons. But that's just an example, which means only the start code

Sign In or Register to comment.