Log In  


So I'm currently working on a little practise game about a duck in a pond. In the game you have to avoid slices of bread which damage your health if you eat them.
Until recently I just manually added 3 slices of bread as individual objects. But I'm now wanting to add multiple levels, where the amount of bread slices resets and increases to make the game more difficult, so I've been trying to learn more about tables to make this happen.
My goal with this code was to be able to add a different number of slices to the bread table each time the game state is triggered, based on the level number. So the 'for i=0,(level+2)' is meant to make it so that the number of slices is the level number plus two, so 3 for level one, 4 for level two, and so on.
But when I run the code below, I don't get any errors, but the bread doesn't even appear. I've definitely added these custom functions to the main code, so I'm not sure where I'm going wrong. Any guidance greatly appreciated as the concepts here are very new to me!

function initbreadslice()
bread={}
end

function breadset()
add(bread,{
sp=42,
x=20+rnd(75),
y=20+rnd(75),
act=true,
dis=4,
draw=function(self)
spr(self.sp,self.x,self.y)
end,
update=function(self)
if self.act==true then
if abs(plr.x-self.x)<=self.dis
and abs(plr.y-self.y)<=self.dis
then
self.act=false
end
end
end,

})

end

function updatebreadslice()
if state==game then
for i=0,(level+2) do
breadset()
end
end

for slice in all(bread) do
slice:update()

end
end

function drawbreadslice()
for slice in all(bread) do
slice:draw()
end
end



Looks alright to me, but is breadset() in updatebreadslice() being called at all? Throwing a few asserts around should help with debugging. Ex:

function updatebreadslice()
 if state==game then
  for i=0,(level+2) do
   breadset()
   assert(false,"breadset called")
  end
 end

 ...

If the program stops at the assert, then you know breadset was called at least once. You can also check after breadset is called if the first element of bread is a table representing a slice like this:
assert(type(bread[1]) == "table", type(bread[1]))


function updatebreadslice()
	level=0
	state='game'
	game='game'
	if state==game then
		for i=0,(level+2) do
			breadset()
		end
	end
	cls()
	stop(#bread)
end

Try rewriting updatebreadslice() with this code and running it.
Since the number of elements in the bread table is now 3, 3 should be displayed.
If this is not displayed, it may be that updatebreadslice() has not been called.
If 3 is displayed, it may be that the following variables have not been assigned appropriate values.

level state game



[Please log in to post a comment]