0
\$\begingroup\$

I have a simple question. Some explination first, however:

I am using libGDX and making a Legend of Zelda-type top-down game and I'm having an issue with map transitions. When I made the tilemap and added the (currently crappy) collision detection to it, I noticed that the IDE threw an error when the character walks off the map (where there are no tiles to test the collision possibility against). I thought, cool, I can use that.

Knowing that the program would throw an exception when the player left the screen, I caught that exception and used it to decide to which map the player was trying to transition and switch accordingly. I played a little transition from the scene2d actions and to the next map we went.

BUT, I got to thinking that while catching that exception is a great thing, I probably shouldn't rely on it for map transitions. I'm betting you all will agree: handle the exception just in case, but don't rely on it.

SO, this means that I need to handle a transition before the user gets off the map. I tried using transition tiles from within the collision detection:

If the tile had the transition property, figure out where the user is attempting to transfer and then play the transition effect and on to the next map. The only problem with this system is that it doesn't look right. The user transitions just as collision is made while there is still room left in the tile to transverse.

My question to you all, then, is this: Is there a middle ground between the two? Is there a way to tell end of map or last tile, allow the user to transverse the tile just prior to transitioning? Or are my two ways the only ones?

\$\endgroup\$
2
\$\begingroup\$

Yikes. Don't use exception handling as part of the normal flow of code. Exceptions are called exceptions because they're anomalous or exceptional events, they shouldn't be part of the standard flow of things.

Simply do a check to see if the tile the character is moving to is going to be outside the bounds of the map. It could be as simple as something like:

if((newPosition.x < worldBoundsMin.x || newPosition.x > worldBoundsMax.x) ||
   (newPosition.y < worldBoundsMin.y || newPosition.y > worldBoundsMax.y))
      //newPosition is out of bounds
\$\endgroup\$
0
\$\begingroup\$

I completely agree with the above answer.

However, it's not always possible to know the new position of the player after a movement has been made (For example, Box2D).

If this is the case, I'd use the edge tiles as the transition hotspot.

if(
   (player.x <= 0 || player.x >= map.getWidth() ) || //Are we on the left or right edge tile?
   (player.y <= 0 || player.y >= map.getHeight()) //Are we on the top or bottom edge tile?
)
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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