Log In  

In tab 4, the argument item2 returns nil and i dont know why.
Any ideas?

code:
tab 1:
-- init --

function _init()
snd={
pickup=0,
tick=1,
lose=2
}
p={
sp=6, -- also works as hp
x=63,
y=63,
spd=2,
mdl=30,
dl=30,
score=0
}
ens={}
end

tab 2:
-- update --

function _update()

-- movement --

if btn(➡️) then
p.x+=p.spd
end if btn(⬅️) then
p.x-=p.spd
end if btn(⬇️) then
p.y+=p.spd
end if btn(⬆️) then
p.y-=p.spd end

-- respecting boundarys --

p.x=mid(0,p.x,120)
p.y=mid(0,p.y,120)

-- ticking --

if p.dl==0 then
p.dl=p.mdl
p.sp-=1
p.score+=1
spn(⬅️,63,{gob=true,sp=64})
-- true:good;false:bad
else
p.dl-=1
end

if #ens>0 then
for i=0,#ens,1 do

if coll(p,ens[i]) then
del(ens,ens[i])
p.sp=6
else
ens[i].x+=ens[i].vx
ens[i].y+=ens[i].vy
end
end

end

tab 3:
-- draw --

function _draw()

cls()

-- background --

for i=0,120,8 do
for j=0,120,8 do
spr(16,i,j,1,1)
end
end

-- sprites --

spr(p.sp,p.x,p.y,1,1)

-- collectables --

if #ens>0 then
for i=0,#ens,1 do

spr(ens[i].sp,ens[i].x,ens[i].y)
end
end

end

tab 4:
-- collision --

function coll(item1,item2)

rx=abs(item1.x-item2.x)
ry=abs(item1.y-item2.y)

if rx < 9 and ry < 9 then
return true
else
return false
end

end

tab 5:
-- spawn colls --

function spn(dir,pos,inf)
local dx=0 local px=0 local ex=0
local dy=0 local py=0 local ey=0

if dir==⬇️ then
dy=-2
py=128
px=pos
ey=0
elseif dir==⬆️ then
dy=2
py=0
px=pos
ey=128
elseif dir==⬅️ then
dx=2
px=0
py=pos
ex=128
elseif dir==➡️ then
dx=-2
px=128
py=pos
ex=0
end

add(ens,{x=px,y=px,vx=dx,vy=dy,
st=inf.gob,sp=inf.sp})

end

P#129711 2023-05-14 08:31

lua arrays are 1-based, not 0
fix:

-- invalid start index
-- for i=0,#ens,1 do
-- fix: start at 1, no need to specify default increment of 1
for i=1,#ens do
...
P#129714 2023-05-14 08:50 ( Edited 2023-05-14 08:50)

[Please log in to post a comment]