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:
https://gist.github.com/4051433
The Slinger finds some minutes to answer a question …
Simple answer:
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.
@the_dude
@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.
supportedOrientations(PORTRAIT)
displayMode(FULLSCREEN)
function setup()
radius=100
selected=false
current=0
c1=color(255,255,255)
c2=color(255,0,0)
c3=color(0,255,0)
c4=color(0,0,255)
ctab={}
ctab[1]=circle(200,200,c1) -- white
ctab[2]=circle(200,400,c2) -- red
ctab[3]=circle(200,600,c3) -- green
ctab[4]=circle(200,800,c4) -- blue
end
function draw()
background(40, 40, 50)
for z=1,#ctab do
ctab[z]:draw() -- draws the 4 circles
end
if current>0 then
ctab[current]:draw() -- re-draws only the circle being moved
end
end
function touched(t)
for z=1,#ctab do
ctab[z]:touched(t,z) -- pass t and the circle number z
end
end
circle = class()
function circle:init(x,y,c)
self.x=x
self.y=y
self.color=c
self.selected=false
end
function circle:draw()
fill(self.color)
ellipse(self.x,self.y,radius*2,radius*2)
end
function circle:touched(t,z)
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
self.selected=true
selected=true
current=z -- the number of the circle being moved
end
if t.state==MOVING and self.selected then
self.x=t.x
self.y=t.y
end
if t.state==ENDED then
self.selected=false
selected=false
current=0
end
end
end
@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?