So a key hold will give you something like this: turn |0| 1| 2| 3| 4| 5| 6| 7| The right amount to move on the press is going to depend on the speed of your main loop: here you're moving 30 units on every game update, which might be a lot or a little (hard to tell without more context).įor the sake of argument, let's say your tiles are 100 units across, and we'll keep the move speed to 30 units per tick (totally made up numbers - just for illustration). You're adding units to the position on every key press. Leaving aside the questions about appropriateness: It gets a bit more advanced if you'd like to allow interrupting motion mid-way through and still stay synced to the grid, but this should give you a good start. You could keep a boolean inMotion or, more succinctly, test if velocity is not Vector2.Zero (then you know you're in motion). If you don't, the character can freely travel "off" the tile grid by interrupting motion mid-progress. Make sure keypresses are disabled while in motion. There are many other methods for this too. To do this I suggest taking the center coordinates (coordinates + tile size / 2) of your character and converting them to tile coordinates and then convert that back to "real" coordinate floats. If you don't, the character will start ever so slowly getting "off" from the tile grid.
Snap to the nearest tile when you're done moving (the one you just travelled to). This will be a good start, but you will still need to do two more things: increment total distance indepedent of direction/axisĭistance += Math.Abs(velocity.X) + Math.Abs(velocity.Y) Increment it along with your Position change, and do the "travelled far enough" test right after: //assuming you're using GameTime gameTime for the timing values Keep a "distance" variable to represent how far the character has travelled so far in his motion, independent of direction/axis: There are many many ways to solve this, but I'll sketch out a simple one here.
#RPG MAKER XP PRODUCT KEY WERE TO FIND FULL#
Now that you've got basic velocity in, all you need now is to STOP after one full tile has been moved.
This is a completely separate issue from the "smooth" motion.
(If you're using fixed game time, you don't really need the elapsed time multiply, but I typically put it in there because I prefer to measure in units per second.) Tile Motion
#RPG MAKER XP PRODUCT KEY WERE TO FIND UPDATE#
Now, every frame, just update Position to make the velocity work actively on it: //assuming you're using GameTime gameTime for the timing values Modify velocity instead in your keypresses: if (aCurrentKeyboardState.IsKeyDown(Keys.Right) = true & mPreviousKeyboardState != aCurrentKeyboardState)Įlse if (aCurrentKeyboardState.IsKeyDown(Keys.Left) = true & mPreviousKeyboardState != aCurrentKeyboardState)Įlse if (aCurrentKeyboardState.IsKeyDown(Keys.Up) = true & mPreviousKeyboardState != aCurrentKeyboardState)Įlse if (aCurrentKeyboardState.IsKeyDown(Keys.Down) = true & mPreviousKeyboardState != aCurrentKeyboardState) Instead of directly setting Position, set up a velocity: Vector2 velocity = Vector2.Zero Īlso define some sort of movement speed: const float speed = 10.0f You need quite a bit more in place than you have now.įirst off, as you already realized, setting the position directly is instant.