<DISCLAIMER START> The views expressed below are my own personal opinion. This is very likely not the best way to accomplish the desired result, and you may very well think that I am a brain-dead, low-life idiot for even suggesting that my code is an option. You are entitled to your opinion as well. Feel free to flame the comments and/or close this browser window if you're not smelling what I'm cooking. Also, all assets are products of my own effort. They aren't that great, so I don't know why you would want to steal them anyway. Keep an eye on my opengameart profile to see what I'm posting and the corresponding license(s) </DISCLAIMER STOP>
I've included the full tutorial zip below. I'll be first talking about the main chunk of the sprite control schemes in the main.lua file. As we go on, I'll be posting updated main.lua files, showing our progression as far as how I feel to best implement Sprite movement.
If you load up the main.lua, you'll find a very simple game screen: you've got your man in the middle, surrounded by trees and grass tiles. I'm not going to go into how I got to the PCG tiles right now, but I will in future installments.
Anyway, once loaded you'll see the sprite assets. Ground, character, directional arrows, you get the idea. Touch an arrow to make the player move in that direction. However, if we take a look at the code within main.lua, we'll see that the character itself isn't moving around the background, we're moving the background display group around the character.
This can be useful in some cases, and in RTS/TBS games, you might be able to get away with the below workflow (this is all taking place within a touch event):
- Identify that a movement is about to take place (began phase)
- Confirm direction of movement
- Test completed movement logic to confirm that movement can take place (no enemies/barriers/obstacles)
- Perform movement so that the player can see movement taking place
- Perform actions that completed movement may have initiated.
This option would work fine with the above, but it's not very sound practice when dealing with fluid movement. And since fluid movement is what we are all striving for, I didn't stop there. I'm going to point out that I struggled with this for quite some time before I found something that works for me. What I finished with isn't the end result, and it took me a lot longer to get to the finish line than I wanted, but I got there!
I fooled around with my code until I got to the main1.lua example, which incorporates some interesting logic into the movement phase. Instead of redrawing the map, character and all that, you are now moving the character, and the gameview moves within a pre-defined set of bounds. I also added in some bells and whistles, so now, the player can move freely, but will not be able to move through trees. Not bad for a super-novice! Check out the code and let me know what a moron I am in the comments!