# Better way of making my character move? (RPG)

I’m working on a little RPG. I’m quite a noob, so I’m just moving the character by changing the x and y values. like, x = x + 1 type thing. I bet there is a better way of doing this. I also want the character to be able to collide with buildings and people as well. How could I go about doing this? I also don’t want anything too complicated, because I’m a noob. I’m guessing I’ll have to use tweens, but I’m not sure how to go about using them. If there are other methods please list them! Thanks.

@1029chris .something like this ?:

http://www.twolivesleft.com/Codea/Talk/discussion/3791/couple-of-useful-tweens#Item_1

btw, this project is pretty old, I think it was one of the first I made for tweens understand

If you want the code … Here : http://pastebin.com/UVuQdPkF

@1029chris - there is nothing wrong with x=x+1, but you may want to put the movement into its own function to avoid spaghetti code. In other words, you could keep all the code together that handles player movement.

Tweens are ok if you want to get a player from a to b over a period of t seconds, but you need to think about what happens if the user interferes and changes direction before the tween has finished - so it can get a little complicated.

Personally, I use the x=x+1 method most of the time, rather than tweens.

As far as collisions are concerned, you need to keep a table in memory that stores the positions of everything. Think of it as a grid map, ie you divide your map into squares. Then, if there is a building, and your code for buildings was “b”, then you would put “b” in all the squares in your table that contain the building. Then, before moving your character, you look up the new position on the map and see if anything is already there.

Of course, you need to decide how big to make the squares - which really is how far a player can move between collision checks. The smaller the squares, the more accurate the collision check and movement, but the more storage required.

The big advantage of a grid system is that you can simply move your player from one square to another. If instead you want your player to be able to walk freely anywhere (instead of from the centre of one square to the centre of another square), there is quite a lot more work.

So I would start with a grid system and see how you go.

@Ignatz How would I go about setting up a grid system?

@1029chris IMHO, you won’t want to use a grid system when you can use physic.bodies, as a grid system you will have to make everything from scratch.

if you are bouncing balls off each other, physics is best, yes. But if youre walking round a map with buildings, then a grid (also known as tile) based system should work best.

You divide your map into squares (eg 4x4 pixels or 8x8 pixels), and the player can move from square to square (you can easily use a tween to make that smooth). You have a 2D table in memory containing codes (eg b for building) that can tell you what is in each square. So for collisions, you simply look up the square the player is going to, and see what is in there.

PS If you dont know Lua tables, you will need to learn them. My ebook has stuff on them.

https://www.dropbox.com/s/qh1e7ft4rvhlpt2/Lua%20for%20beginners.pdf

Well you don’t have to do it all tile based, IMHO tile based maps are the best and easiest, tho you can always choose to move the character freely withour it being tile-based moves, the collision detecting however would take a little more thinking, but it’s certainly is doable

Agreed, but if I was a noob, I wouldn’t start with the harder option!

@Ignatz, true, but just giving some thoughts, since everyone’s here to learn, and we can give him some ideas for when he succeeds in doing the grid option, he then can still choose to do more advanced movement or not