- Would like to associate a name to a rectangle.
I would think it would work like this:
x=rect(400,400,100,100)
So I can refer back to it
Doesn’t seem to work for me
I would think it would work like this:
x=rect(400,400,100,100)
So I can refer back to it
Doesn’t seem to work for me
@Itrync - in one word, you need tables.
They are part of the Lua language underlying Codea, and it’s important you get to know them. There is lots on the net, and I explain them in this ebook
https://www.dropbox.com/s/qh1e7ft4rvhlpt2/Lua%20for%20beginners.pdf
@Itrync Are you after something like this. Just tap a square. I didn’t put a lot into this because I don’t know what you’re trying to do.
EDIT: added parameter sliders to allow different colors and different square sizes. Just tap the upper left screen area to show parameter area.
displayMode(FULLSCREEN)
function setup()
w1=WIDTH -- save starting width
h1=HEIGHT -- save starting height
parameter.integer("size",20,50,50,resize) -- size parameter
parameter.color("colr",255,0,0) -- color to draw
tab={} -- table of touched squares
col={} -- color of touched square
end
function resize()
w=w1/size-1 -- number of horizontal squares
h=h1/size-1 -- number of vertical squares
end
function draw()
background(40,40,50)
for x=0,w do -- loop for horizontal squares
for y=0,h do -- loop for vertical squares
fill(255) -- start with white
for a,b in pairs(tab) do -- loop thru touched table
if x==b.x and y==b.y then -- does square match touched table
fill(col[a]) -- if yes, then color it
end
end
rect(x*size+10,y*size+10,size,size) -- draw square
end
end
end
function touched(t)
if t.state==BEGAN then -- screen was touched
x1=math.floor((t.x-10)/size) -- calculate x square position
y1=math.floor((t.y-10)/size) -- calculate y square position
for a,b in pairs(tab) do -- loop thru touched table
if b.x==x1 and b.y==y1 then -- check if square was touched
table.remove(tab,a) -- remove it from the table
table.remove(col,a) -- remove it from the table
return
end
end
table.insert(tab,vec2(x1,y1)) -- square touched, add to table
table.insert(col,colr) -- square touched, add color to table
end
end
– I want to make a grid of boxes on my screen. I want each one to have have a variable so I can easily get location. I am 100 % sure there is a shortcut. I originally wanted to do a do loop and join a letter with a number that increments.
-- grid
-- Use this function to perform your initial setup
function setup()
background(0, 0, 0, 255)
stroke(198, 198, 198, 255)
strokeWidth(1)
fill(255, 255, 255, 255)
a1=0 --x
a2=600 --y
a3=50 --width
a4=50 --height
b1=0
b2=0
b3=0
b4=0
cc=0
print(a1)
end
-- This function gets called once every frame
function draw()
box=50
b1=600
a1=0
a2=50
a3=100
a4=150
a5=200
a6=250
a7=300
a8=350
a9=400
a10=450
a11=500
a12=550
a13=600
a14=650
b2=550
b3=500
b4=450
b5=400
b6=350
rect(a1,b1,box,box)
rect(a2,b1,box,box)
rect(a3,b1,box,box)
rect(a4,b1,box,box)
rect(a5,b1,box,box)
rect(a6,b1,box,box)
rect(a6,b1,box,box)
rect(a7,b1,box,box)
rect(a8,b1,box,box)
rect(a9,b1,box,box)
rect(a10,b1,box,box)
rect(a11,b1,box,box)
rect(a12,b1,box,box)
rect(a13,b1,box,box)
rect(a14,b1,box,box)
--$$$$$$$$$$$$$$$$$$$$
rect(a1,b2,box,box)
rect(a2,b2,box,box)
rect(a3,b2,box,box)
rect(a4,b2,box,box)
rect(a5,b2,box,box)
rect(a6,b2,box,box)
rect(a6,b2,box,box)
rect(a7,b2,box,box)
rect(a8,b2,box,box)
rect(a9,b2,box,box)
rect(a10,b2,box,box)
rect(a11,b2,box,box)
rect(a12,b2,box,box)
rect(a13,b2,box,box)
rect(a14,b2,box,box)
--$$$$$$$$$$$$$$$$$
rect(a1,b3,box,box)
rect(a2,b3,box,box)
rect(a3,b3,box,box)
rect(a4,b3,box,box)
rect(a5,b3,box,box)
rect(a6,b3,box,box)
rect(a6,b3,box,box)
rect(a7,b3,box,box)
rect(a8,b3,box,box)
rect(a9,b3,box,box)
rect(a10,b3,box,box)
rect(a11,b3,box,box)
rect(a12,b3,box,box)
rect(a13,b3,box,box)
rect(a14,b3,box,box)
--$$$$$$$$$$$$$$$$$
rect(a1,b4,box,box)
rect(a2,b4,box,box)
rect(a3,b4,box,box)
rect(a4,b4,box,box)
rect(a5,b4,box,box)
rect(a6,b4,box,box)
rect(a6,b4,box,box)
rect(a7,b4,box,box)
rect(a8,b4,box,box)
rect(a9,b4,box,box)
rect(a10,b4,box,box)
rect(a11,b4,box,box)
rect(a12,b4,box,box)
rect(a13,b4,box,box)
rect(a14,b4,box,box)
--$$$$$$$$$$$$$$$$$$
rect(a1,b5,box,box)
rect(a2,b5,box,box)
rect(a3,b5,box,box)
rect(a4,b5,box,box)
rect(a5,b5,box,box)
rect(a6,b5,box,box)
rect(a6,b5,box,box)
rect(a7,b5,box,box)
rect(a8,b5,box,box)
rect(a9,b5,box,box)
rect(a10,b5,box,box)
rect(a11,b5,box,box)
rect(a12,b5,box,box)
rect(a13,b5,box,box)
rect(a14,b5,box,box)
--$$$$$$$$$$$$$$$$$$$$$
rect(a1,b6,box,box)
rect(a2,b6,box,box)
rect(a3,b6,box,box)
rect(a4,b6,box,box)
rect(a5,b6,box,box)
rect(a6,b6,box,box)
rect(a6,b6,box,box)
rect(a7,b6,box,box)
rect(a8,b6,box,box)
rect(a9,b6,box,box)
rect(a10,b6,box,box)
rect(a11,b6,box,box)
rect(a12,b6,box,box)
rect(a13,b6,box,box)
rect(a14,b6,box,box)
end
@Itrync - you are treating rect as an object, which is what you would expect if you come from another language.
But Codea doesn’t work like that. I suggest you start by reading the beginning of my ebook below, which tries to explain the difference.
https://www.dropbox.com/s/t5im6tl14ky5t08/Codea%20for%20beginners.pdf
I have lots more stuff here
http://coolcodea.wordpress.com/2013/06/19/index-of-posts/
Codea can be confusing at first, but it’s marvellous once you begin to understand it.
@Itrync - I should have at least explained that rect just draws something on your screen, like paint on a canvas. It’s not an object. If you want your rect to move, then next time Codea draws (in 1/60 second) you draw the rectangle in a different place.
If you want your rect to be an object, then you need to learn about sprites and/or meshes. If the tutorials and other material don’t make sense, just ask.
Ok thanks. I will read up
That was helpful.
Thanks a bunch for the example. The ebook stuff makes more since when I understand a few things. That code is pretty much what I was trying to accomplish.
I am trying to understand
for a,b in pairs(tab) do
I understand that (tab) is the name of your table
I understand what the for and do accomplish
I understand that you need a start and end for your do loop but not understand the a,b
Looks like “a” is removed from table but I don’t see anything added to “a”
@Itrync - all tables consist of “key-value” pairs, ie if you have T[1] =3, then the key is 1, and the value is 3.
If you have T[“a”]=45, then the key is “a” and the value is 45.
When you loop through using “for a,b in pairs(T) do”, Codea gives you each pair in turn, so if the first item in the table is T[1]=3, then a=1, b=3.
So neither a nor b is a counter. a is a key, b is a value.
Thanks my goal is to have a solid understanding of this by the weekend
I have seen for loops like
For I = 1,20 do
X=x+1
End
X is 20 when loop finishes
I see what you are saying about a,b being key and value. I guess I was getting mixed up
@Itrync - “for i=1,20 do” gives you all values for numeric keys
the pairs method gives you all key-value pairs in the table, but won’t necessarily give them to you in a particular order
so each has their uses
if still unclear, try googling “lua for loop”, there are lots of explanations out there
Ok
I am confused about this from the code above
If X== b.x and y==b.y then
End
Think I can answer my own question
It’s basically asking if the fake x == real x then … X being touch
@Itrync I added comments to the above code. Maybe that will help. I also added a “size” variable to change the size of the squares.
That does help. I’ve been working on this all morning. Think I am almost there