Recently a request for a key assignment code was made, therefore I thought I'd post one up for anyone who wishes to add key customisation to their games.
This tutorial assumes you are familiar with creating movie clips, controlling movie clips, basic actionscript knowledge and possibly knowledge of how to create basic platforming games - although not necessary if your not intending to make a that type of game. However the concepts can be used for any purpose. Click here for a list of my other tutorials which pretty much covers the above list of prerequisite.First of all I am going to explain something, for our imaginary game we will have 4 controls, move left (left arrow key), move right (right arrow key), move down (down arrow key) and move up (up arrow key). Because key customisation is optional it is important that we set default controls so that if a user doesn't wish to change the keys then they don't have to. Each key on the keyboard has it's own ASCII Key Code (a list of which can be found
here). Type this into the first frame of your timeline:
So far we have the directional arrow keys to control movement and the spacebar to attack. But until we add this next code to our character movie clip, which for this demo you will simply place on the stage, he won't do anything. So create a movie clip of anything to act as a character. Assign this actionscript to the movie clip:
You will now see that the character moves left, right, up and down - nothing special. However now comes the part where we can change the keys. If you referred to the Ascii Key Codes then you will know that each key has a number assigned to it, to change what key is assigned to each action, all we need to do is change the numbers we used in the first block of variables at the beginning of this tutorial - simple. For example if we wanted to change the Right Arrow key to the letter S key, we just change the number 39 to 83 and voila - the S key now moves the character right instead.
Now for most games that allow you to change the key mapping, they wouldn't expect you to input each of the numbers manually when you were playing nor would you be expected to know every key code if you were programming the game, therefore we have a handy little function we can use to allow the user to press a key and be told the key code assigned to the key they pressed.
If you attach that code to your movie clip you will see that every key you press will cause a number to appear in the output window, the number will be the representation of the key you pressed according to the Ascii Table given previously.
Now allowing the user to change the controls varies from game to game. Most people let the user press the key they wish to assign the new key code value for - you can use the Key.getCode() for this. Some people might have 2 or 3 predetermined control sets for users to select - you will need to program these in manually and allow the user to change it from an options menu - I am not going to explain this one as it should be easy enough.
As the last part to this tutorial I will now show you how to allow the user to press the key they wish to assign as the new key code value. In an options menu type area, you will need to set up a method of doing this on your own as there are countless ways of doing it - most people however have all of the controls in a list with a button next to it. Like So:
Up (38) [CHANGE]
Down (40) [CHANGE]
Left (37) [CHANGE]
Right (39) [CHANGE]
Then when change is clicked they will be taken to a new place that will say 'Please press the key you wish to assign to this action'. Then when they press it they will be taken back to the previous menu and the new value will be there. The way they change the variables given at the beginning would be a function like this:
Then to call the function you will need to add a keydown event to something, can be an invisible movie clip if you want:
There are lots of ways of making a menu like I mentioned previously so I am not going into too much detail about this but given what I have told you in this and providing you have a sound knowledge of flash - you should be able to pull this off on your own... however IF anyone is really struggling on this then let me know and I will post a tutorial flash for you to have a look at and utilise.