Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

What I want to do:

I simply want the have a 2D array to represent my game map.

I want a player sprite and I want that sprite to be able to move around my map freely using the keyboard and also have collisions with certain tiles of my map array.

I want to use very large maps so I need a viewport.

What I have:

I have a loop to load the tile images into an array:

//Loop to load tile images into an array
var mapTiles = [];

for (x = 0; x <= 256; x++) {
    var imageObj = new Image(); // new instance for each image
    imageObj.src = "images/prototype/"+x+".jpg";
    mapTiles.push(imageObj);
}

I have a 2D array for my game map:

//Array to hold map data
var board =   [
    [1,2,3,4,3,4,3,4,5,6,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
    [17,18,19,20,19,20,19,20,21,22,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
    [33,34,35,36,35,36,35,36,37,38,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
    [49,50,51,52,51,52,51,52,53,54,1,1,1,1,1,1,1,1,1,1,1,1,1,197,198,199,1,1,1,1],
    [65,66,67,68,146,147,67,68,69,70,1,1,1,1,1,1,1,1,216,217,1,1,1,213,214,215,1,1,1,1],
    [81,82,83,161,162,163,164,84,85,86,1,1,1,1,1,1,1,1,232,233,1,1,1,229,230,231,1,1,1,1],
    [97,98,99,177,178,179,180,100,101,102,1,1,1,1,59,1,1,1,248,249,1,1,1,245,246,247,1,1,1,1],
    [1,1,238,1,1,1,1,239,240,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
    [216,217,254,1,1,1,1,255,256,1,204,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
    [232,233,1,1,1,117,118,1,1,1,220,1,1,119,120,1,1,1,1,1,1,1,1,1,1,1,119,120,1,1],
    [248,249,1,1,1,133,134,1,1,1,1,1,1,135,136,1,1,1,1,1,1,59,1,1,1,1,135,136,1,1],
    [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
    [1,1,216,217,1,1,1,1,1,1,60,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
    [1,1,232,233,1,1,1,1,1,1,1,1,1,1,1,1,1,1,204,1,1,1,1,1,1,1,1,1,1,1],
    [1,1,248,249,1,1,1,1,1,1,1,1,1,1,1,1,1,1,220,1,1,1,1,1,1,216,217,1,1,1],
    [1,1,1,1,1,1,1,1,1,1,1,1,149,150,151,1,1,1,1,1,1,1,1,1,1,232,233,1,1,1],
    [12,12,12,12,12,12,12,13,1,1,1,1,165,166,167,1,1,1,1,1,1,119,120,1,1,248,249,1,1,1],
    [28,28,28,28,28,28,28,29,1,1,1,1,181,182,183,1,1,1,1,1,1,135,136,1,1,1,1,1,1,1],
    [44,44,44,44,44,15,28,29,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
    [1,1,1,1,1,27,28,29,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
    [1,1,1,1,1,27,28,29,1,1,1,1,1,1,1,1,1,59,1,1,197,198,199,1,1,1,1,119,120,1],
    [1,1,1,1,1,27,28,29,1,1,216,217,1,1,1,1,1,1,1,1,213,214,215,1,1,1,1,135,136,1],
    [1,1,1,1,1,27,28,29,1,1,232,233,1,1,1,1,1,1,1,1,229,230,231,1,1,1,1,1,1,1],
    [1,1,1,1,1,27,28,29,1,1,248,249,1,1,1,1,1,1,1,1,245,246,247,1,1,1,1,1,1,1],
    [1,1,1,197,198,199,28,29,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
    [1,1,1,213,214,215,28,29,1,1,1,1,1,60,1,1,1,1,204,1,1,1,1,1,1,1,1,1,1,1],
    [1,1,1,229,230,231,28,29,1,1,1,1,1,1,1,1,1,1,220,1,1,1,1,119,120,1,1,1,1,1],
    [1,1,1,245,246,247,28,29,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,135,136,1,1,60,1,1],
    [1,1,1,1,1,27,28,29,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
    [1,1,1,1,1,27,28,29,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]
];

I have my loop to place the correct tile sin the correct positions:

//Loop to place tiles onto screen in correct position
for (x = 0; x <= viewWidth; x++){
    for (y = 0; y <= viewHeight; y++){
        var width = 32;
        var height = 32;
        context.drawImage(mapTiles[board[y+viewY][x+viewX]],x*width, y*height);
    }
}

I have my player object :

//Place player object
    context.drawImage(playerImg, (playerX-viewX)*32,(playerY-viewY)*32, 32, 32);

I have my viewport setup:

//Set viewport pos
viewX = playerX - Math.floor(0.5 * viewWidth);
    if (viewX < 0) viewX = 0;
    if (viewX+viewWidth > worldWidth) viewX = worldWidth - viewWidth;


    viewY = playerY - Math.floor(0.5 * viewHeight);
    if (viewY < 0) viewY = 0;
    if (viewY+viewHeight > worldHeight) viewY = worldHeight - viewHeight;

I have my player movement:

canvas.addEventListener('keydown', function(e) {
    console.log(e);
    var key = null;
    switch (e.which) {
        case 37:
            // Left
            if (playerY > 0) playerY--;
            break;
        case 38:
            // Up
            if (playerX > 0) playerX--;
            break;
        case 39:
            // Right
            if (playerY < worldWidth) playerY++;
            break;
        case 40:
            // Down
            if (playerX < worldHeight) playerX++;
            break;
    }

My Problem:

I have my map loading an it looks fine, but my player position thinks it's on a different tile to what it actually is.

So for instance, I know that if my player moves left 1 tile, the value of that tile should be 2, but if i print out the value it should be moving to (2), it comes up with a different value.

How I've tried to solve the problem:

I have tried swap X and Y values for the initialization of my player, for when my map prints.

If I swap the X and Y values in this part of my code:

context.drawImage(mapTiles[board[y+viewY][x+viewX]],x*width, y*height);

The map doesnt get draw correctly at all and tiles are placed all in random positions or orientations

If I swap the X and Y values for my player in this line :

context.drawImage(playerImg, (playerX-viewX)*32,(playerY-viewY)*32, 32, 32);

The players movements are inversed, so up and down keys move my player left and right viceversa.

My question:

Where am I going wrong in my code, and how do I solve it so I have my map looking like it should and my player moving as it should as well as my player returning the correct tileID it is standing on or moving too.

ALSO

Here is a link to my whole code:

prototype

share|improve this question
    
So... what's your question? –  Anko Dec 11 '12 at 12:32
    
Where am i going wrong with my player movement or map? –  Tom Burman Dec 11 '12 at 12:33
    
These are the problems I'm having reading your question: You've posted a lot of messy code with no comments. I don't know what you want your code to do. I don't know how you've tried to solve the problem. These make it very difficult for me to answer. –  Anko Dec 11 '12 at 12:39
    
ok ill post these details –  Tom Burman Dec 11 '12 at 12:41
    
Please look at my response below Anko, Thankyou very much –  Tom Burman Dec 11 '12 at 13:14
show 2 more comments

1 Answer

up vote 2 down vote accepted

This code....

switch (e.which) {
        case 37:
            // Left
            if (playerY > 0) playerY--;
            break;
        case 38:
            // Up
            if (playerX > 0) playerX--;
            break;
        case 39:
            // Right
            if (playerY < worldWidth) playerY++;
            break;
        case 40:
            // Down
            if (playerX < worldHeight) playerX++;
            break;
    }

Should be:

     switch (e.which) {
        case 37:
            // Left
            if (playerX > 0) playerX--;
            break;
        case 38:
            // Up
            if (playerY > 0) playerY--;
            break;
        case 39:
            // Right
            if (playerX < worldWidth) playerX++;
            break;
        case 40:
            // Down
            if (playerY < worldHeight) playerY++;
            break;
    }

You got Up and Down mixed up with Left and right (And I'm assuming your comment are correct here), also worldHeight and worldWidth have been corrected.

I see no code that enquires what tile the player is currently under so unable to fix anything related to that part of the question.

The following should work:

 function getPlayerTile(x, y) {
     return board[y][x];
 }

 console.log(getPlayerTile(playerX, playerY));
share|improve this answer
    
Thankyou very much it seems to be working fine now! –  Tom Burman Dec 12 '12 at 3:32
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.