The McLeodGaming forums were permanently closed on April 30th, 2020. You are currently viewing a read-only archive.
View unanswered posts | View active topics It is currently Fri May 15, 2020 5:38 am

Forum rules


IMPORTANT

This Forum is for Flash related questions ONLY! Do not ask questions about spriting, Game Maker, Photoshop, or anything else not related to Flash.



 [ 3 posts ] 
[Tutorial] Key Assignment 
Author Message
User avatar

Joined: Mon Aug 11, 2008 1:09 pm
Posts: 166
Location: England lol
Gender: Male
Currently Playing: Minecraft
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:

Code:
//Set default keys and variables
player_left = 37;
player_up = 38;
player_right = 39;
player_down = 40;



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:

Code:
onClipEvent (enterFrame) {
   if (Key.isDown(_root.player_left)) {
      _x -= 10;
   }
   if (Key.isDown(_root.player_right)) {
      _x += 10;
   }
   if (Key.isDown(_root.player_up)) {
      _y -= 10;
   }
   if (Key.isDown(_root.player_down)) {
      _y += 10;
   }
}



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.

Code:
onClipEvent (keyDown) {
   trace(Key.getCode());
}


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:

Code:
function changeKey(action, newkey) {
   _root["player_" + action] = newkey
}


Then to call the function you will need to add a keydown event to something, can be an invisible movie clip if you want:

Code:
onClipEvent (keyDown) {
   _root.changeKey("up" ,Key.getCode());
   //code to go back to previous menu
}



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.


Thu Jun 18, 2009 10:13 am
WWW
User avatar

Joined: Fri Mar 27, 2009 8:55 pm
Posts: 282
Location: Why do you want to know you perv!
Gender: Male
Skype: lomeli1241
Currently Playing: Kingdom Hearts Re:Coded (JP)
x2i wrote:
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.

I'm struggling a bit.

_________________
Image
Image
Give cred 2 Doromac
Mes Amis: show
BICURIOUS GEORGE
daniel
King Master X12
Neo_Fire_Sonic
Racerx52
Sora
Tycron
Tyli
User Avatar
yoh
Envoyez-moi un message pour ĂȘtre mon ami! Ha! That's French 4 Pm to be Friends


Fri Jan 08, 2010 2:03 am
WWW

Joined: Wed Sep 03, 2008 9:27 am
Posts: 676
Location: NY
Gender: Anime Girl
Thanks, this makes a little more sense to me, and be notes "little"

_________________
Image
Image


Sun Jan 10, 2010 11:24 am
WWW
Display posts from previous:  Sort by  
 [ 3 posts ] 

Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

cron
Powered by phpBB® Forum Software © phpBB Group
Designed by ST Software for PTF.