Hello, I’m just working on an “four wins” ( or four in a row, or however you call it ) And the main field is ready, but I have a problem with the draw function of my coin. I have one class for my coin that should work for both players but when I call the draw function of my coin it only draws one coin at the time so the recent coin before doesn’t exist anymore. I just got this function that the coin should change the color before the draw function, so you have two colors for two players. It’s a school project I’m working on and we have to present it.
It would be very nice, if somebody can help me with my problem maybe its just simple but I just don’t understand it
Thank you very much
Tocoin = class()
function Tocoin:init(x)
-- you can accept and set parameters here
self.x = x
self.col = gelb
self.player = 1
end
function Tocoin:draw()
if self.player == 1 then
fill(229, 232, 49, 255)
self.col = gelb
self.player = 2
elseif self.player == 2 then
fill(214, 72, 35, 255)
self.col = rot
self.player = 1
end
-- Codea does not automatically call this method
end
function Tocoin:touched(touch)
-- Codea does not automatically call this method
end
@hallomio77 How well do you understand classes. A class shouldn’t have code dependent on what player it is. A class is generic and its instance should be dependent on the player. The coins init function should define the coins information, it’s x,y position, size, color, and any other information about it. The coins draw function should draw the coin depending on the init information. There shouldn’t be any if statements as to which player it is. Each player should have a table that contains the coins to be drawn. That table can be defined in the coin init function or an external table for that player.
@hallomio77 Here’s a very simple example. Tap the screen to place a circle at that position. The color alternates depending on the players turn.
displayMode(FULLSCREEN)
function setup()
player1={}
player2={}
pl=1
end
function draw()
background(40, 40, 50)
for a,b in pairs(player1) do
b:draw()
end
for a,b in pairs(player2) do
b:draw()
end
fontSize(40)
if pl==1 then
fill(255,0,0)
str="PLAYER 1"
else
fill(0,255,0)
str="PLAYER 2"
end
text(str,WIDTH/2,HEIGHT-50)
end
function touched(t)
if t.state==BEGAN then
if pl==1 then
table.insert(player1,coin(t.x,t.y,color(255,0,0)))
pl=2
else
table.insert(player2,coin(t.x,t.y,color(0,255,0)))
pl=1
end
end
end
coin=class()
function coin:init(x,y,c)
self.x=x
self.y=y
self.size=80
self.col=c
end
function coin:draw()
fill(self.col)
ellipse(self.x,self.y,self.size)
end
Ok thank you very much I’ll try that out