ForumsProgramming ForumTile-based movement

4 4000
Katumi
offline
Katumi
9 posts
Nomad

Hello everyone

I'm making a rpg style game that i want to be tile based, meaning u will always move from one tile to another when u press one of the direction keys. I set up a coordinate system to make it easy to tell if you can move to the next tile or not by checking if that coordinate is walkable.

Now i am wondering if anyone know how to make the animation part? my tiles are 50x50 atm, for the testing period. the system is simple, if i press the right key, i move 50 pixles right and the x coordinate of the character increase by 1. but this means i will move very fast and very far if i'm holding down the key, i want an animation or something to make it go slowly. something like this written in code language:

character x position + 5 pixles for 50 pixles, then stop.

All help is very welcome, thanks in advance

  • 4 Replies
driejen
offline
driejen
486 posts
Nomad

You could try making a walking animation that itself moves 50 pixels without the actual registration point moving.

When you press an arrow key, the character starts the walking animation and is temporarily uncontrolabble.
At the end of the animation, the last frame could reanable character control, reset the animation to standing still and move the registration point of the character to the intended tile.

Programpro
offline
Programpro
562 posts
Nomad

What driejen said.

Another solution, which I used in a game of mine, is to have a boolean player state of "moving", true or false. He will have an X coord, a Y coord, variables called xpos and ypos, which reflect the "square" in the tile grid he is in, and xdest and ydest, which reflect the square he's moving to. If he's moving, then you just bring his x and y closer to the position of the xdest and ydest.

driejen's is probably the better way for what you're proposing. The advantage of my way is that you always have a position coordinate of the player on-screen, good for having the player collide with on-screen bullets, or for controlling the placement of aura's or weapons on the player (thought those would be best attached to the layer symbol himself).

Do whatever you think is best

Katumi
offline
Katumi
9 posts
Nomad

hey again and thanks for the replies, and sorry about my slow replay, i've been out of town for a few days. I tried following your advice, but ran into a few problems.

My character have two frames on it's timeline, one called 'idle' and one called 'walkRight'. Inside of walkRight is a 10 frames long animation that move the character 50 pixles to the right. idle is just a frame for standing still.

In the code i tell the character to play the walkRight animation when the right key is pressed, and this is how far i've gotten.

Now i want the character to return to the idle frame and add 50 pixles to it's x position when the walkRight animation reaches the end, but i'm not quite sure how to write the code properly.

Here is my attempt

character_mc.addEventListener(Event.ENTER_FRAME, characterDirectFrame);
function characterDirectFrame(event:Event):void
{
if (character_mc.walkRight.currentFrame == 10)
{
character_mc.x +=50;
character_mc.gotoAndStop("idle"
}
}


when i try to run it i get this error message:

TypeError: Error #1010: A term is undefined and has no properties.
at Untitled_fla::MainTimeline/characterDirectFrame()


I'm guessing i made a mistake trying to access the walkRight movieclip, as i'm not very good at coding yet.

Also, i made it work earlier by doing it a bit more simple, but that way i had to press the right key for every tile i wanted to move. I wanna be able to hold it and move right until i release it, and then stop at the current tile.

If anyone could help me sort this out i would be really thanksfull

Kusic
offline
Kusic
184 posts
Peasant

This command is good, i'm trying to make a truck driving game, like indestructo tank or something like that, so i need this command, thanks

Showing 1-4 of 4