Rookie needs help

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

Hi @Jdrj - there is no function called Drawblocks() defined. If you comment that out in the code above it seems to work as you describe. If you have this class but haven’t included the code in this post then the problem may be in there.

BTW - I would suggest separating the touch handling and the drawing. That will make your App easier to code and maintain. draw() gets called about 60 times a second while a touch is event based. Use a state machine to keep track of what you want to draw each frame.

@Reefwing
Thanks. Drawblocks is a separate function I wrote to draw some caves on the screen. Like you said, editing it out and things go well. Here is the code for Drawblocks (). I’ll keep looking for errors.

Drawblocks = class()

function Drawblocks:init(x)

    xpos=WIDTH/8
    ypos=HEIGHT-50
    size = 80
    hidden = {}
    for i = 1,9 do
        for a = 1,8 do
            cave = {sprite("Small World:Monster Den",xpos,ypos)}
            xpos = xpos + size
        end
        xpos= WIDTH/8
        ypos= ypos - size
    end
    
end

thanks

@Jdrj - you need to delete the line: hidden = {} in Drawblocks

A more object oriented approach would be to create a Cave class which knows what it contains and whether it has been touched.

@Simeon has written a simple button class which is included in one of the Codea sample projects (Sounds Plus). Your cave is in effect an extended button. You could set up a hit function for your cave similar to the one in the Button class.

Have a look at the tutorial at http://codeatuts.blogspot.com.au/2012/06/tutorial-3-simple-button-class.html

Now that you point it out it’s so insanely obvious. Thanks for the help.
-J