How do I do smooth sliding effects for objects when I hit a direction button?
In example: In Zelda II: Adventure of Link, if you hit right (on overworld) Link moves right 16 spaces. If one continues holding right, he always goes on to keep that 16 space constant. I've failed at all attempts at doing this and need some help.
The reason it happens this way in Adventure of Link(Awesome game) is because the map is designed on a grid, and Link is literally just moving into the next square when the button is pressed, but since he doesn't pause between squares when the button is held, he moves across multiple squares flawlessly, giving the appearance that he's walking normally. This is used in a few games, some titles to name would be Pokémon and Final Fantasy Mystic Quest.
The grid movement works like this(Lets just say your grid is 16x16):
Give your character(let's say it's Link) two internal values, one for column(horizontal square), one for row(vertical square). Let's say these are Value A for column and Value B for row. Give him a third value which will store how many pixels he has to move to reach the next square. We'll say that one's Value C.
Firstly we add the events for each direction. Let's start with right. Add the event "Repeat while user presses right"(arrow keys, joystick, whichever you choose), and add with that "+ Value C = 0". Then in the actions, add 1 to Value A and set Value C to 16.
Do the same for left, and the same again for up and down only adding 1 to value B instead. This will then remember which square Link is in rather than his actual co-ordinates.
Now to make him actually move to the square. This is where Value C comes in. The reason we set Value C to 16 is because it's going to count down as Link moves a pixel. Then he will only move if Value C is 0 because that way it can only tell him to move to the next square once he has reached the current square. Make sense? Here's what we do to actually move him:
X position of Link < Value A * 16 (You may want to add -8 or a similiar value depending on Link's size and hot spot position to the end of this to ensure he's in the middle of the square rather than the top-left corner of it.) + Value C > 0: Set X position of Link to X position of Link + 1, Subtract 1 from Value C.
X position of Link > Value A * 16 + Value C > 0: Set X position of Link to X position of Link - 1, Subtract 1 from Value C.
Y position of Link < Value B * 16 + Value C > 0: Set Y position of Link to Y position of Link + 1, Subtract 1 from Value C.
Y position of Link > Value B * 16 + Value C > 0: Set Y position of Link to Y position of Link - 1, Subtract 1 from Value C.
I hope you can make sense of that. Basically it works out Link's actual X/Y co-ordinates by multiplying his actual grid co-ordinates by the size of the grid squares, and if he's not at those co-ordinates, it will move him to them. Value C ensures that he doesn't move another square across until he's reached the current one, but since the event is "repeat while button pressed" rather than "button pressed" he will continue to move the instant he reaches the square, creating the "sliding" motion you wished for. Also, this way, Link's grid co-ordinates are conveniently stored in two of his values should you need to retrieve them later.
A nice effect in this kind of engine is to then have invisible detectors which sit around Link, in just the right position so they're one square away from him, and to then not allow him to move another square if overlapping an obstacle.