PDA

View Full Version : Keyboard assignment


markuskarlsson
11-26-2003, 11:18 AM
Hello.
Me and some friends here at school (hyper island) has got an assignment to make a flashgame.

Now we have some problems. We want to assign a button on the keyboard to a specific player. Something like this "Player 1 choose your button" and so on. We donīt know how to do this and would reeealy appreciate som help.

regards
markus karlsson
hyper island

prisma
11-26-2003, 12:26 PM
Each Key has an assigned Key Code. You can look them up in the Flash help: ActionScript Reference Guide -> Keyboard Keys and Key Code Values.

To call a function whenever a key is pressed you have to use a key Listener:

var keyListener:Object = new Object();
Key.addListener(keyListener);
keyListener.onKeyDown = function() {
trace("A key was pressed with the followig Key Code: "+Key.getCode());
}


You just have to save the Key Code of the key the user pressed in the beginning in a variable. Then whenever a key is pressed you compare the code of the pressed key (Key.getCode()) with the one stored in the variable.

Hope I could help you.