Skip to main content
added 454 characters in body
Source Link
House
  • 73.3k
  • 17
  • 185
  • 273

No problem with being new to game development. You just need to make sure to provide enough information so that we can properly help you. Your on the right track with your code and it looks like your isMoveable() function is sound. I'm not sure what the difference between motion.Y and position.y are and why you're setting one vs the other depending on position.Y > screenHeight / 4, so the following maybe isn't what you want to achieve. However, I would suggest this simple structure for moving your character around.

// current position of the turtle on the tiles
int mapX = (int)(position.X / scalingFactor);
int mapY = (int)(position.Y / scalingFactor);
if (keyboardState.IsKeyDown(Keys.Up))
{
    if (isMovable(mapX, mapY-1, map))
    {
        position.Y = position.Y - scalingFactor;
    }
} else if (keyboardState.IsKeyDown(Keys.Down))
{
    if (isMovable(mapX, mapY+1, map))
    {
        position.Y = position.Y + scalingFactor;
    }
} else if (keyboardState.IsKeyDown(Keys.Right))
{
    if (isMovable(mapX+1, mapY, map))
    {
        position.X = position.X + scalingFactor;
    }
} else if (keyboardState.IsKeyDown(Keys.Left))
{
    if (isMovable(mapX-1, mapY, map))
    {
        position.X = position.X - scalingFactor;
    }
}

EDIT

This could be because of the way you're indexing into the map. Note orientation for your map as defined in code is not the way it will display on screen. You're indexing into it like this:

int tile = map[ mapX , mapY ];

So mapX is referencing the rows of your array and mapY is referencing the columns. So the top row of tiles on your screen from left to right are referencing the last column in your array from top to bottom.

No problem with being new to game development. You just need to make sure to provide enough information so that we can properly help you. Your on the right track with your code and it looks like your isMoveable() function is sound. I'm not sure what the difference between motion.Y and position.y are and why you're setting one vs the other depending on position.Y > screenHeight / 4, so the following maybe isn't what you want to achieve. However, I would suggest this simple structure for moving your character around.

// current position of the turtle on the tiles
int mapX = (int)(position.X / scalingFactor);
int mapY = (int)(position.Y / scalingFactor);
if (keyboardState.IsKeyDown(Keys.Up))
{
    if (isMovable(mapX, mapY-1, map))
    {
        position.Y = position.Y - scalingFactor;
    }
} else if (keyboardState.IsKeyDown(Keys.Down))
{
    if (isMovable(mapX, mapY+1, map))
    {
        position.Y = position.Y + scalingFactor;
    }
} else if (keyboardState.IsKeyDown(Keys.Right))
{
    if (isMovable(mapX+1, mapY, map))
    {
        position.X = position.X + scalingFactor;
    }
} else if (keyboardState.IsKeyDown(Keys.Left))
{
    if (isMovable(mapX-1, mapY, map))
    {
        position.X = position.X - scalingFactor;
    }
}

No problem with being new to game development. You just need to make sure to provide enough information so that we can properly help you. Your on the right track with your code and it looks like your isMoveable() function is sound. I'm not sure what the difference between motion.Y and position.y are and why you're setting one vs the other depending on position.Y > screenHeight / 4, so the following maybe isn't what you want to achieve. However, I would suggest this simple structure for moving your character around.

// current position of the turtle on the tiles
int mapX = (int)(position.X / scalingFactor);
int mapY = (int)(position.Y / scalingFactor);
if (keyboardState.IsKeyDown(Keys.Up))
{
    if (isMovable(mapX, mapY-1, map))
    {
        position.Y = position.Y - scalingFactor;
    }
} else if (keyboardState.IsKeyDown(Keys.Down))
{
    if (isMovable(mapX, mapY+1, map))
    {
        position.Y = position.Y + scalingFactor;
    }
} else if (keyboardState.IsKeyDown(Keys.Right))
{
    if (isMovable(mapX+1, mapY, map))
    {
        position.X = position.X + scalingFactor;
    }
} else if (keyboardState.IsKeyDown(Keys.Left))
{
    if (isMovable(mapX-1, mapY, map))
    {
        position.X = position.X - scalingFactor;
    }
}

EDIT

This could be because of the way you're indexing into the map. Note orientation for your map as defined in code is not the way it will display on screen. You're indexing into it like this:

int tile = map[ mapX , mapY ];

So mapX is referencing the rows of your array and mapY is referencing the columns. So the top row of tiles on your screen from left to right are referencing the last column in your array from top to bottom.

Source Link
House
  • 73.3k
  • 17
  • 185
  • 273

No problem with being new to game development. You just need to make sure to provide enough information so that we can properly help you. Your on the right track with your code and it looks like your isMoveable() function is sound. I'm not sure what the difference between motion.Y and position.y are and why you're setting one vs the other depending on position.Y > screenHeight / 4, so the following maybe isn't what you want to achieve. However, I would suggest this simple structure for moving your character around.

// current position of the turtle on the tiles
int mapX = (int)(position.X / scalingFactor);
int mapY = (int)(position.Y / scalingFactor);
if (keyboardState.IsKeyDown(Keys.Up))
{
    if (isMovable(mapX, mapY-1, map))
    {
        position.Y = position.Y - scalingFactor;
    }
} else if (keyboardState.IsKeyDown(Keys.Down))
{
    if (isMovable(mapX, mapY+1, map))
    {
        position.Y = position.Y + scalingFactor;
    }
} else if (keyboardState.IsKeyDown(Keys.Right))
{
    if (isMovable(mapX+1, mapY, map))
    {
        position.X = position.X + scalingFactor;
    }
} else if (keyboardState.IsKeyDown(Keys.Left))
{
    if (isMovable(mapX-1, mapY, map))
    {
        position.X = position.X - scalingFactor;
    }
}