-2
\$\begingroup\$

I'm creating a Java game similar to Mario Bros. I'm using Tiled Map Editor to create the map, inside the map there is water. I want to make my character swims inside water, for instance, changing the animation and make it floats. But, my character just sinks. I don't know how to implement these things inside my program, although, I succeed with collision.

Can anyone help me please?

\$\endgroup\$
1
  • \$\begingroup\$ Could you provide some details on how you are detecting whether the player is in water at the moment? Is Tiled Map Edtiro just a graphics tool, or does it add meta-data on which tiles should are "swimmable" as well? If it's just a graphics tool, I suspect you might be lacking code to specify those tiles are for swimming and not for walking. \$\endgroup\$ Commented Dec 10, 2017 at 14:36

1 Answer 1

1
\$\begingroup\$

You could have a boolean inWater; for your player and every update it checks if he is colliding with a water tile. Then in your update / tick method where you calculate your logic you just base your logic on the inWater variable.

For example:

if(inWater) {
    gravity = -0.5f;
} else if(!inWater) {
    gravity = -0.9f;
}

Same goes for your animations. When applying the current animation check the inWater variable and if it is true, set the moveLeft and moveRight animations to swimRight and swimLeft etc.

And to get the tile, just do what you do for collision, but instead apply it to check for water.

This is a really simple way of doing it, but googling a little won't hurt you.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .