Player out of tile map

Started by
13 comments, last by DKdowneR 6 years, 6 months ago
6 hours ago, DKdowneR said:

Also I don't think that I should check for x is negative, because it's always positive

What values can posX have?

If posX can be greater than 32, getStartBlockX will return a negative number.


return (int)(-posX + (int)posX % 32) / 32;

//Assuming posX = 33:
//Ignoring the int-casts to show it simpler.
return (-33 + 33 % 32) / 32;
return (-33 + 1) / 32;
return -32 / 32;
return -1;

 

Hello to all my stalkers.

Advertisement

posX is a value that decrease if position X of Player is higher than or equal (1280/2) - playerWidth and if Player moves right. if moves left and position X of Player is lower than or equal (1280/2) - playerWidth, posX increase.

The player and map moving formula are :
 


//moving right
if (posX >= (System::getFM()->getW() / 2) - getHitBoxX() && System::getSM()->GetGame()->getMoveMap())// && System::getSM()->GetGame()->getPosX() >= -1760)
{
	System::getSM()->GetGame()->MoveMap(-moveSpeed, 0);
}
	// player posX
else posX += moveSpeed;

moveAnim = true;


// moving left
if (posX <= (System::getFM()->getW() / 2) - getHitBoxX() && System::getSM()->GetGame()->getMoveMap() && System::getSM()->GetGame()->getPosX() < 0)
{
	System::getSM()->GetGame()->MoveMap(-moveSpeed, 0);
}
else if (posX - System::getSM()->GetGame()->getPosX() + moveSpeed >= 0 && posX >= 0)
{
  // player posX
	posX += moveSpeed;
}
else if (posX >= 0)
{
	UpdatePosX(moveSpeed + 1);
}
moveAnim = true;

Getgame returns GameplayScreen instace, move map is a method that add moveSpeed to posX of map. getMoveMap() returns boolean, if true, map can move, if false, it cannot.

LactoseposX is always negative or 0.

Yxjmir, that's right. I gave a bad example. It's 40 because if posX = 5, still I can see only 40 block and a piece of 41, and then I add 1 to get 1 more blocks. It returns ~41.

Untitled.png

as you can see, on the right site there are pieces of blocks.

Depending on your compiler, modulo of a negative number can result in undefined behavior.

Regardless, it should be fairly easy to track down where the error is in your case, since you can reproduce it easily:

For every loop, before using the x and y indices anywhere, print/log them.

When your application crashes, see what the x and y values are. Then, the next step is to find out why they are broken, and how to fix it.

Hello to all my stalkers.

x = 94. that's the problem. Map width, last block of the map got 94 index. As I said in a title, player runs outta map.

This topic is closed to new replies.

Advertisement