-1

I'm trying to display the levels I'll have using this code...

levelArray[0] = ["player", "empty", "empty", "empty", "wall", "wall", "empty",
    "empty", "wall", "wall", "empty", "empty", "empty","empty",
    "empty", "empty", "empty", "wall", "empty", "wall", "wall",
    "wall", "wall", "empty", "wall", "empty", "wall", "empty",
    "box", "wall", "empty", "wall", "empty", "empty", "empty",
    "empty", "empty", "empty", "wall", "wall", "wall", "empty",
    "wall", "wall", "empty", "empty", "ghost", "wall","ghost"];

for(var i = 0; i < edge; i++)
{
    for(var j = 0; j < edge; j++)
    {
        switch(levelArray[i])
        {
            case "empty":   // empty location
                ctx.drawImage(emptyTile, currentX, currentY);
                break;
            case "wall":    // wall block
                ctx.drawImage(wallTile, currentX, currentY);
                break;
            case "box":     // box block
                ctx.drawImage(boxTile, currentX, currentY);
                break;
            case "ghost":   // enemy sprite
                ctx.drawImage(ghostTile, currentX, currentY);
                break;
            case "player":  // player sprite
                ctx.drawImage(playerTile, currentX, currentY);
                break;
        }
        currentX += elementEdge;
    }
    currentY += elementEdge;
}

however I receive an error on the switch line "Uncaught TypeError: Cannot read property '0' of undefined" and I don't really understand it.

Edit: edge is defined previously like this

edge = Math.sqrt(levelArray.length)
4
  • is edge defined somewhere? Commented Aug 13, 2013 at 14:43
  • Where's edge defined? And you're using it for both for loops? Shouldn't it be levelArray.length and levelArray[i].length? Commented Aug 13, 2013 at 14:44
  • 1
    levelArray[i] will never be equal to a string. Commented Aug 13, 2013 at 14:44
  • updated with edge definition Commented Aug 13, 2013 at 15:11

2 Answers 2

0

You're not using the inner loop. I believe you want this instead:

switch(levelArray[i][j])

In your source code, that snippet is inside a setupCanvas function, with takes levelArray as an argument. Since you're not passing anything when you call setupCanvas, it's considered undefined. Hence the errors you're getting.

Sign up to request clarification or add additional context in comments.

7 Comments

Make sure edge is defined, and levelArray is properly initialized (var levelArray = []) somewhere
Edge is defined and levelArray is properly initialised
@Cody Could you setup a jsfiddle that reproduces the problem?
@Cody That's too much code for me to debug right now. But I ran it, and didn't get any errors.
Click on the output, I just threw it all in so it'd be a proper representation of what I have
|
0

Try to access your array of levelArray like this:

switch(levelArray[0][i])

, because this levelArray[i] will return array in case i=0 for sure and other cases of i if you have placed more arrays!

1 Comment

What is the value of edge? Try instead of edge, 48 and remove one of the two for loops.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.