It simply attaches the character-movieclip in the correct position on top of the map. Test the movie and see if it works. Hopefully it does…

Move! It should move!

Yes, it should! So let´s make it walk.

Doubleclick the character-movieclip in the library and get inside it. Make two new layers, one for a script and one to hold our keyevent-movieclip. We start with the getting the keys(we´re going to use the arrowkeys to control).

Make an empty movieclip, ctrl-F8, drag it from the library into the character-movieclip and center it. Select it and bring up the actionscript-window, and attach the following code:

The if-statement just checks what key is pressed and sets the variable “dir” to that direction. Last we call a function that we´re about to make, and then we use updateAfterEvent to update the screen. This might be better of in a enterFrame-event later on if decide to do more complex stuff, but for now it works(one should really try to have as few enterFrame or frameloops as possible).

Next up is the walk-function, which we call from the keyevent-movieclip.

First we set a constant called speed, just how many pixels we should move each time.

In the function we first check to see if there is a direction(if a key is pressed), otherwise we just break.

Then by a simple equation like: xPostion / tileWidth.

We find out where we are in the array, let´s say our xPostion is 32 pixels from the left and our tileWidth is 12, 32/12 and we get something like 2.66666. If we then enclose this in a Math.floor we round this down to the closest integer, that is 2, so we should check on position 2 in the array.

So we simply find out where the edges of our character are.

Then we check what kind of tile that is in our map-array, like:

_parent.myMap[upY][centerX] != 1

Here we check what kind of tile is located in the top-center of the character. If it´s a 1-tile we don´t move, otherwise we do. You could of course do a little better checking, like if we move up you might want to check the leftX and rightX instead of the centerX. But you get the basic idea.

Here´s the example(Focus, then arrowkeys):

That´s it, now it should move, but not on the black tiles… You can download it here if you´re lazy, but I recommend that you try to do it your own way rather than trusting me.

Next up I´ll think we will look into scrolling, which can be bit tricky, especially in flash which is nearly to slow for that task. Until then, PLAY, add gravity, tiles with different properties and so on!