The erase function of my class below doesn’t seem to work. It worked fine before I moved everything into a class.
I can’t find what’s wrong.
Map = class()
function Map:init(x,y,w,h)
self.img = image(w,h)
self.x = x
self.y = y
end
function Map:draw()
sprite(self.img, self.x, self.y)
end
function Map:erase(xy, radius)
--print(self.img)
col = color(0, 0, 0, 0)
for y=-radius, radius do
for x=-radius, radius do
if x*x+y*y <= radius*radius then
self.img:set(xy.x+x, xy.y+y, col)
end
end
end
end
function Map:makeTerrain(wallCol, edgeCol)
setContext(self.img)
local walls = {}
for i=1, 50 do
table.insert(walls,
{x=math.random()*WIDTH, y=math.random()*HEIGHT, rad=math.random()*300})
end
fill(edgeCol)
for i=1, #walls do
ellipse(walls[i].x, walls[i].y, walls[i].rad+5)
end
fill(wallCol)
for i=1, #walls do
ellipse(walls[i].x, walls[i].y, walls[i].rad)
end
setContext()
end
@Kirl Your erase function does work. Run this and tap the screen to call erase. I added an image in Map:init to have something to erase.
function setup()
m=Map(WIDTH/2,HEIGHT/2,50,50)
end
function draw()
background(0)
m:draw()
end
function touched(t)
if t.state==BEGAN then
m:erase(vec2(200,200),100)
end
end
Map = class()
function Map:init(x,y,w,h)
--self.img = image(w,h)
self.img = readImage("Cargo Bot:Codea Icon")
self.x = x
self.y = y
end
function Map:draw()
sprite(self.img, self.x, self.y)
end
function Map:erase(xy, radius)
--print(self.img)
col = color(0, 0, 0, 0)
for y=-radius, radius do
for x=-radius, radius do
if x*x+y*y <= radius*radius then
self.img:set(xy.x+x, xy.y+y, col)
end
end
end
end
function Map:makeTerrain(wallCol, edgeCol)
setContext(self.img)
local walls = {}
for i=1, 50 do
table.insert(walls,
{x=math.random()*WIDTH, y=math.random()*HEIGHT, rad=math.random()*300})
end
fill(edgeCol)
for i=1, #walls do
ellipse(walls[i].x, walls[i].y, walls[i].rad+5)
end
fill(wallCol)
for i=1, #walls do
ellipse(walls[i].x, walls[i].y, walls[i].rad)
end
setContext()
end