All,
I’m trying to make a simple game for my kid and learn some programming basics while I’m at it. The game is quite simple. Select a cave to explore by pressing on a cave on the screen. Inside there is either gold a monster or a sword. If there is a monster inside and you have a sword you defeat the monster otherwise game over. If there is gold inside, you collect it.
I fill the caves by doing a for loop on a table (array?) and assigning a random number of 1, 2 or 3 to each slot in the table
I’m running into a problem though. When a cave is clicked a value is set depending on what cave is pressed and then should return what is at cave[value] but is instead returning nil value. This has me totally stumped. Please review the code below and tell me where I’ve gone wrong so that I’m getting nil back instead of a 1 2 or 3
-- Use this function to perform your initial setup
function setup()
value=99
end
-- This function gets called once every frame
function draw()
-- This sets a dark background color
background(40, 40, 50)
Drawblocks()
-- make stuff
if value==99 then
hidden = {}
for i =1,72 do
hidden[i]=math.random(1,3)-- fill the caves
end
end
value = 0
if CurrentTouch.state == BEGAN then
sprite("Planet Cute:Star",CurrentTouch.x, CurrentTouch.y)
vx=(CurrentTouch.x/8)
vy=(CurrentTouch.y/8)
xint=10
yint=90
count = 0
for i = 1,9 do -- first row, top left
for j = 1,8 do -- cycle across
count = count + 1
if vx > (xint-3) and vx < (xint+3) and vy > (yint - 3) and vy < (yint+3) then
value = count
print(hidden[value])--why does this return nil?
end
xint = xint + 10
end
yint=yint-10 -- move down a row
xint=10 -- reset xint
end
end
if CurrentTouch.state == ENDED then
xint = 10
yint = 90
end
end
Any help is appreciated.
J