My code works fine on my PC but when I run it on my Android it crashes when its checking tiles to place new tree.
I suspected the problem was because of difference of screen ratio or something and tried some size changes but didn't help any.
I attach my example code and error appears like this on mobile.
Please let me know if someone know what the problem is!
Code: Select all
function love.load()
math.randomseed (os.clock ())
screensizex=love.graphics.getWidth()
screensizey=love.graphics.getHeight()
tilesize=16
map={}
mapsizex = screensizex/tilesize --30 tiles
mapsizey = screensizey/tilesize --20tiles
TreeNums=50
objects={}
trees={}
MakeNewMap()
generateTrees()
end
function love.draw()
if trees then
for i,v in ipairs(trees) do
love.graphics.rectangle("fill",v.x,v.y,v.w,v.h)
end
end
end
function generateTrees()
for i=1,TreeNums do
local tilex=math.random (1, mapsizex)
local tiley=math.random (1, mapsizey)
--x=clamp(0,x,screensizex)
--y=clamp(0,y,screensizey)
print("random x:"..tilex.." random y:"..tiley)
if CheckTileByTilesize(tilex,tiley,0) then
trees[i]={
x=(tilex*tilesize)-tilesize,
y=(tiley*tilesize)-tilesize,
w=tilesize,
h=tilesize
}
--tree[i]:SetCollision(true) --set circle collision
table.insert(objects, #objects + 1, tree[i])
map[tiley][tilex]=2
end
end
end
function MakeNewMap()
for y = 1,mapsizey do
map[y] = {}
for x = 1,mapsizex do
map[y][x]= 0
end
end
end
function CheckTileByTilesize(x,y,num)
if map[y][x] == num then
return true
else
return false
end
end
function GetTilePos(x,y)
local tilex=math.floor((x+tilesize)/tilesize)
local tiley=math.floor((y+tilesize)/tilesize)
tilex=clamp(1,tilex,mapsizex)
tiley=clamp(1,tiley,mapsizey)
local pos={x=tilex,y=tiley}
return pos
end
function PrintMap()
for i = 1, #map do
local s = ""
for j= 1, #map[i] do
s = (s .. " " .. map[i][j])
end
print(s.." "..i)
end
for k= 1, #map do
local m = ""
for l= 1, #map[k] do
m = (m.. " "..l)
end
if k==#map then print(m) end
end
print(" ")
end
function love.keypressed(key)
if key=="space" then
PrintMap()
end
if key=="g" then
MakeNewMap()
generateTrees()
PrintMap()
end
if key=="n" then
MakeNewMap()
end
end
Code: Select all
Error
main.lua:69: attempt to index a nil value
Traceback
main.lua:69: in function 'CheckTileByTilesize'
main.lua:38: in function 'generateTrees'
main.lua:14: in function 'load'
[C]: in function 'xpcall'
[C]: in function 'xpcall'