@Kempoman, in walls you need to do “self.i = i”, not “self.i = I” because variables are case-sensitive. Here is working code:
--# Main
-- Level Making
displayMode(FULLSCREEN)
supportedOrientations(CurrentOrientation)
function setup()
walls = {}
levels = {
{{2,1,1,1,3,3,3,1,1,2,1},{1,1,3,1,1,1,2,1,3,2,2},{1,3,1,3,1,1,1,2,1,3,1}, {2,1,1,1,3,3,3,1,1,2,1},{1,1,3,1,1,1,2,1,3,2,2},{1,3,1,3,1,1,1,2,1,3,1},{2,1,1,1,3,3,3,1,1,2,1},{1,1,3,1,1,1,2,1,3,2,2},{2,2,2,2,2,1,1,1,1,1,2}},
{{1,1,2,1,3,2,3,1,2,2,3}, {1,1,3,2,1,3,1,1,3,2,1}, {2,3,2,3,1,3,1,1,1,3,2},{1,1,1,1,3,3,2,1,2,2,1}, {2,1,3,1,3,1,1,1,3,1,2}, {1,3,1,3,1,3,1,2,1,2,1}, {2,3,1,1,2,3,2,1,3,2,1}, {1,2,3,1,3,1,2,1,2,2,2}, {2,3,2,3,2,1,2,1,2,1,2}}
} -- Creates a table of levels with two levels that have 9 rows and 11 collumns, each number 1-3 represents a different tile
level = 1 -- Select the first level
makeWalls() -- Call the function to draw the tiles
end
function makeWalls()
for i = 1, #levels[level] do --rows
for j = 1, #levels[level][i] do --columns
if j > 0 then -- If the tile type isnt 0 make a tile
table.insert(walls,wall((j * 101 - 50.5),HEIGHT - (i*85 - 50), levels[level][i][j]))
end
end
end
end
function createRandomLevel()
table.insert(levels, level, {}) -- Create a table in our levels table for the new level
for i = 1, 9 do
table.insert(levels[level], {}) -- Create 9 rows
for j = 1,11 do
table.insert(levels[level][i], math.random(1,3)) -- Insert random tile values into the rows
end
end
makeWalls() -- Have it make the blocks for the level we just created
end
function draw()
background(0, 0, 0, 255) -- Set a black background
for i = 1, #walls do -- For every block in the table walls
walls[i]:draw() -- draw the correct sprite
end
fill(255, 0, 0, 255)
rect(WIDTH/2 - 50, HEIGHT-20, 100,20)
fill(255, 255, 255, 255)
text("FPS:"..1/DeltaTime, WIDTH/2, HEIGHT - 10)
end
function touched(touch)
if CurrentTouch.state == ENDED then -- If touch is over then
level = level + 1 -- change level
for i = 1, #walls do
table.remove(walls, i) -- Remove the old blocks from the table
end
if levels[level] == nil then -- If that level doesn't exist then
createRandomLevel() -- Call the function to make a new level
else -- if a level already exists then
makeWalls() -- Have it make the blocks for the next level
end
end
end
--# wall
wall = class()
function wall:init(x,y,i)
-- you can accept and set parameters here
self.pos = vec2(x,y) -- Set the position based on the parameter given
self.size = vec2(101,171) -- Size of the blocks
self.i = i
end
function wall:draw()
-- Codea does not automatically call this method
if self.i == 1 then
sprite("Planet Cute:Grass Block",self.pos.x,self.pos.y,self.size.x,self.size.y) -- Draw the grass block at the correct position and size specified above
end
if self.i == 2 then
sprite("Planet Cute:Stone Block",self.pos.x,self.pos.y,self.size.x,self.size.y) -- Draw the stone block at the correct position and size specified above
end
if self.i == 3 then
sprite("Planet Cute:Dirt Block",self.pos.x,self.pos.y,self.size.x,self.size.y) -- Draw the dirt block at the correct position and size specified above
end
end
function wall:touched(touch)
-- Codea does not automatically call this method
end
EDIT: added comments and changed it to tile the whole screen, also added a random level generator