I just started writing a simple checkers program - you can only move the red pieces, though. The problem is, once you start dragging a piece over other pieces that are drawn after it in the draw() function, it appears behind it. How can I ensure that the piece being touched will always have a zLevel higher than that of the rest?
Here’s the source code:
The Slinger finds some minutes to answer a question …
Remember the piece that is currently touched and draw it at the the end of draw(). This means for the sake of code simplicity that the piece is drawn twice, for the first time in the general draw phase and for a second time when it is explicitly drawn on top of everything else.
Not so simple answer:
This is not a real answer to your question, rather an observation from reading your code. You seem to avoid loops. The function CheckeredBoard:draw() would gain much readability from using loops.
Then there are tables. Your pieces are named piece1, piece2, piece3 and so on. You’d better create a table “pieces” and then loop over the table.
Why? Why all this? Well, I don’t know how my work you spent on your code, but much effort was not spent well. Depending on how exactly you want to solve your original problem you may need to spend a lot of additional word to make the piece appear on top or you just add some lines of code.
My advice: Learn loops. Rewrite your code. Learn tables. Rewrite your code. Solve your problem.
@Codeslinger is correct with his response. There are easier ways to write your code. But if you’re just starting out, you’re not going to know how. The best way to learn is to look at an example and see how it’s done. Here is an example showing 4 circles that use tables and loops. Also, I added code that draws the moving circle on top of the other ones. If the circles overlap when you stop moving them, they are drawn in the order they were created, white, red, green, blue with white on the bottom and blue on the top. I had another version that kept a table of the order as they were moved, so if you overlapped the circles, the latest ones moved overlapped the ones before it. Your checker program doesn’t need anything like that, so I’m not including that here. You should be able to modify your code using this and reduce the size.
ctab=circle(200,200,c1) -- white
ctab=circle(200,400,c2) -- red
ctab=circle(200,600,c3) -- green
ctab=circle(200,800,c4) -- blue
background(40, 40, 50)
for z=1,#ctab do
ctab[z]:draw() -- draws the 4 circles
if current>0 then
ctab[current]:draw() -- re-draws only the circle being moved
for z=1,#ctab do
ctab[z]:touched(t,z) -- pass t and the circle number z
circle = class()
if (t.y-self.y)^2/radius^2+(t.x-self.x)^2/radius^2 <= 1 then
if t.state==BEGAN and not selected then
current=z -- the number of the circle being moved
if t.state==MOVING and self.selected then
if t.state==ENDED then
@Codeslinger thanks so much for your input. I certainly consider myself a beginner, especially in Lua. Indeed much time and effort was put into this #:-s
@dave1707 this is an interesting approach, thank you.
So does this mean that function zLevel is useless/obsolete?