I have a problem with my first isometric game. I do not know how to do to my player able to approach the edge of the wall. In this moment player maybe move about in green area.

My map:

int[,] map = new int[,] 
        {

            {1, 1, 1, 1, 1, 1, 1, 1},
            {1, 0, 0, 0, 0, 0, 0, 1},
            {1, 0, 0, 0, 0, 0, 0, 1},
            {1, 0, 0, 0, 0, 0, 0, 1},
            {1, 0, 0, 0, 0, 0, 0, 1},
            {1, 0, 0, 0, 0, 0, 0, 1},
            {1, 0, 0, 0, 0, 0, 0, 1},
            {1, 1, 1, 1, 1, 1, 1, 1}

        }; 

Variables:

int TileWidth = 50;
int TileHeight = 50;
int posX = 2; // map X position
int posY = 2; // map Y position
float playerX = 2 * 50; // player X position
float playerY = 2 * 50; // player Y position

Detect wall:

    public bool detectSolidTile(int x, int y)
    {

        if (map[y, x] == 1) return true;  else return false;

    }

Movemet:

posX = (int)(Math.Floor((playerX) / 50));
posY = (int)(Math.Floor(playerY / 50));

(...)

        if (slide == 1 && !detectSolidTile(posX + 1, posY))
        {
            playerX++;
        }
        if (slide == 2 && !detectSolidTile(posX - 1, posY))
        {
            playerX--;
        }

Image -> http://s16.postimg.org/cxkfomemd/tiles.jpg

What I need improve to be able to move from wall to wall?

best regards, Krzysiek

有帮助吗?

解决方案

Try making all the map where you can navigate 0, and then the map where you can not 1. When trying to make a new move check if the future position will be either a 1 or a 0. If it's 0 you let him move, otherwise you stop him.

    if (slide == 1 && !detectSolidTile(posX + 1, posY))
    {
        playerX++;
    }

This will detect the map with 1 as being outside the movable region, thus stopping it from going where you want.

Do you understand where the problem is now?

Another solution is to understand the size of the matrix, and as soon x or y reach the maximum value, you stop incrementing them. But if you want to add obstacles later on, keep with what you are doing now, but make sure 0 is for map, 1 is for outside the map.

So here is a edit for BartoszKP: You might be right that this "!" does not fix his code, I was just explaining how he should do it.

The code I would use for his issues is slightly different.

First of all drop the playerX and playerY since it is the exact same thing as posX and posY, you just do extra calculations. now this being said you movement algorithm would look something like this:

Variables:

int TileWidth = 50; 
int TileHeight = 50;
int posX = 2; // map X position - same thing as playerX 
int posY = 2; // map Y position - same thing as playerY 
//posX and posY will now hold the position of your player on the map since your conversion from playerX to playerY will only work if you increment a value by 50 or more.

Detect a wall: //if the tile at coordinates x and y is a wall I return true //else return false public bool detectSolidTile(int x, int y) {

    if (map[y, x] == 1) return true;
    else return false;
}

Movement:

posX = (int)(Math.Floor((playerX) / 50)); //drop this you don't need it
posY = (int)(Math.Floor(playerY / 50)); //drop this too, you are using posX and posY to store locations

(...)

        if (slide == 1 && !detectSolidTile(posX + 1, posY))
        {
            posX++;
        }
        if (slide == 2 && !detectSolidTile(posX - 1, posY))
        {
            posX--;
        }

        if (slide == 3 && !detectSolidTile(posX, posY+1))
        {
            posY++;
        }
        if (slide == 4 && !detectSolidTile(posX, posY-1))
        {
            posY--;
        }

This should work quite well if you use posX and posY as the position of the player on the map. Making 1 type of coordinate for the player and one type for the map just makes it more confusing at this point. But if you indeed need to use different coordinates for them, you should always refer to playerX and playerY when trying to calculate the movement like this:

if (slide == 1 && !detectSolidTile((int)(Math.Floor((playerX+1) / 50)) + 1, posY))
{
    playerX++;
} 

And this is because you are changing the value of playerX by 1 not the value of posX, and this will restrict you movement. If this is confusing let me know and I will try to explain this better.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top