Terraria

Hello guys, I need a little help with my 2D game. I want to place a block (rectangle) by touching the screen. If I touch, the block will appear, if I touch somewhere else, a new block will appear, depending how many times do I touch. Something like terraria (placing blocks on the ground). Any ideas?

Create the function touched(t). When you touch the screen, save the t.x and t.y values in a table. In the draw function, loop thru the table and draw a rectangle at the x,y coordinates.

The probelm is, I do not know how to use tables.

Here’s an example. Learn how to use tables because you’ll use them a lot. The sooner you learn, the better off you’ll be.

function setup()
    rectMode(CENTER)
    tab={}
end

function draw()
    background(40, 40, 50)
    fill(255)
    for a,b in pairs(tab) do
        rect(b.x,b.y,50,50)
    end
end

function touched(t)
    if t.state==BEGAN then
        table.insert(tab,vec2(t.x,t.y))
    end
end

Thank you ver much! What does the a,b means?

This is the point where I tell you to read the documentation and the online Lua programming books. At the top of the forum is a link Wiki that has a lot of information for you. We’re here to help you, not teach you. You’ll hear that a lot from the MODS here.

Ok, thank you very much!

@erninger Welcome to the forum. You’re starting a very exciting experience. The more you put into it, the more you’ll get out of it and we’re here to help.

@erninger - it’s really important to learn about tables because they are very powerful, and you’ll get stuck without them.

You can google for “Lua tables”, or you can read what I wrote when I learned about them, here and here.