Creating a control customizer (AS2)
- Guil07
-
Guil07
- Member since: Oct. 13, 2007
- Offline.
-
- Forum Stats
- Member
- Level 03
- Blank Slate
In a lot of flash games you have the ability to customize the in-game controls. Usually to do this you click on a text box, and hit whatever button it is you want to change the particular control.
I wasn't sure how people achieve this. I figured they must write a long function that will put a string in the text box (such as "Shift") for the corresponding keycode (Shift's key-code being 16).
So I wrote one up (feel free to copy):
function ShowKeyPressed(TextBoxName) {
if (Selection.getFocus() == "_level0."+TextBoxName) {
Selection.setSelection(0,_root[TextBoxNa me].length);
KeyPressed = Key.getCode();
if (KeyPressed == 8) {
_root[TextBoxName].text = "Backspace";
}
if (KeyPressed == 9) {
_root[TextBoxName].text = "Tab";
}
if (KeyPressed == 13) {
_root[TextBoxName].text = "Enter";
}
if (KeyPressed == 16) {
_root[TextBoxName].text = "Shift";
}
if (KeyPressed == 17) {
_root[TextBoxName].text = "ctrl";
}
if (KeyPressed == 20) {
_root[TextBoxName].text = "Caps Lock";
}
if (KeyPressed == 27) {
_root[TextBoxName].text = "Esc";
}
if (KeyPressed == 32) {
_root[TextBoxName].text = "Spacebar";
}
if (KeyPressed == 33) {
_root[TextBoxName].text = "Page Up";
}
if (KeyPressed == 34) {
_root[TextBoxName].text = "Page Down";
}
if (KeyPressed == 35) {
_root[TextBoxName].text = "End";
}
if (KeyPressed == 36) {
_root[TextBoxName].text = "Home";
}
if (KeyPressed == 37) {
_root[TextBoxName].text = "Left";
}
if (KeyPressed == 38) {
_root[TextBoxName].text = "Up";
}
if (KeyPressed == 39) {
_root[TextBoxName].text = "Right";
}
if (KeyPressed == 40) {
_root[TextBoxName].text = "Down";
}
if (KeyPressed == 45) {
_root[TextBoxName].text = "Insert";
}
if (KeyPressed == 46) {
_root[TextBoxName].text = "Delete";
}
if (KeyPressed == 144) {
_root[TextBoxName].text = "Num Lock";
}
if (KeyPressed == 145) {
_root[TextBoxName].text = "ScrLk";
}
if (KeyPressed == 19) {
_root[TextBoxName].text = "Pause/Break";
}
}
}
And then for the controls you could have something like
if (Key.isDown(_root.MyTextBox)) {}
Or something along those lines. However What if the user wants the left-click to be the control...? =O
This is my problem: Key.getcode(); does not notice mouse clicks - and therefore I couldn't add
//1 is keycode for left-click...
if (KeyPressed == 1) {
_root[TextBoxName].text = "Left-Click";
}
Any ideas around this? I don't know how I should approach this..
Thanks,
~Guil
- Guil07
-
Guil07
- Member since: Oct. 13, 2007
- Offline.
-
- Forum Stats
- Member
- Level 03
- Blank Slate
And while I'm at it, how do you put text in the AS format on NG BBS...?
oooh - [code]
I think I see now. Sorry/bump, then
~Guil
- Denvish
-
Denvish
- Member since: Apr. 25, 2003
- Offline.
-
- Send Private Message
- Browse All Posts (15,977)
- Block
-
- Forum Stats
- Member
- Level 46
- Blank Slate
Just as an experiment, parsed out the keycodes listed on this page into an array, just use chArr[Key.getCode()]; to show which key was pressed.
Seems to work OK
chArr=["","","","","","","","","Backspace","Tab","","","","Enter","","","Shift","Control","","Pause/Break","Caps Lock","","","","","","","Esc","","","","","Spacebar","Page Up","Page Down","End","Home","Left Arrow","Up Arrow","Right Arrow","Down Arrow","","","","","Insert","Delete","","0","1","2","3","4","5","6","7","8","9","","","","","","","","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","","","","","","Numpad 0","Numpad 1","Numpad 2","Numpad 3","Numpad 4","Numpad 5","Numpad 6","Numpad 7","Numpad 8","Numpad 9","Multiply","Add","","Subtract","Decimal","Divide","F1","F2","F3","F4","F5","F6","F7","F8","F9","","F11","F12","F13","F14","F15","","","","","","","","","","","","","","","","","","Num Lock","ScrLk","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","",";","=",",","-",".","/","`","","","","","","","","","","","","","","","","","","","","","","","","","","","[","\\","]","#","`"];
kl=new Object();
kl.onKeyDown=function(){
_root[TextBoxName].text=chArr[Key.getCode()];
}
Key.addListener(kl); - Denvish
-
Denvish
- Member since: Apr. 25, 2003
- Offline.
-
- Send Private Message
- Browse All Posts (15,977)
- Block
-
- Forum Stats
- Member
- Level 46
- Blank Slate
Oh, forgot to mention, you're better off handling mouseDowns separately with onMouseDown and/or a mouse Listener. The keycode 1 for mouse click won't work on MACs apparently, so it's not advised if your game is to be distributed.
- GodlyKira
-
GodlyKira
- Member since: Dec. 21, 2008
- Offline.
-
- Forum Stats
- Member
- Level 05
- Blank Slate
At 2/20/09 12:37 PM, Denvish wrote: Just as an experiment, parsed out the keycodes listed on this page into an array, just use chArr[Key.getCode()]; to show which key was pressed.
Seems to work OK
chArr=["","","","","","","","","Backspac e","Tab","","","","Enter","","","Shift",
"Control","","Pause/Break","Caps Lock","","","","","","","Esc","","","","
","Spacebar","Page Up","Page Down","End","Home","Left Arrow","Up Arrow","Right Arrow","Down Arrow","","","","","Insert","Delete","",
"0","1","2","3","4","5","6","7","8","9",
"","","","","","","","A","B","C","D","E"
,"F","G","H","I","J","K","L","M","N","O"
,"P","Q","R","S","T","U","V","W","X","Y"
,"Z","","","","","","Numpad 0","Numpad 1","Numpad 2","Numpad 3","Numpad 4","Numpad 5","Numpad 6","Numpad 7","Numpad 8","Numpad 9","Multiply","Add","","Subtract","Decim al","Divide","F1","F2","F3","F4","F5","F 6","F7","F8","F9","","F11","F12","F13","
F14","F15","","","","","","","","","",""
,"","","","","","","","Num Lock","ScrLk","","","","","","","","",""
,"","","","","","","","","","","","","",
"","","","","","","","","","","","","","
","","","","",";","=",",","-",".","/","`
","","","","","","","","","","","","",""
,"","","","","","","","","","","","","",
"[","\\","]","#","`"];
kl=new Object();
kl.onKeyDown=function(){
_root[TextBoxName].text=chArr[Key.getCod e()];
}
Key.addListener(kl);
or chr(Key.getCode()); its already built into flash, only thing you would need an array or an object for would be saving the keycode, to refer later in th control function
- Denvish
-
Denvish
- Member since: Apr. 25, 2003
- Offline.
-
- Send Private Message
- Browse All Posts (15,977)
- Block
-
- Forum Stats
- Member
- Level 46
- Blank Slate
At 2/20/09 12:42 PM, GodlyKira wrote: or chr(Key.getCode()); its already built into flash, only thing you would need an array or an object for would be saving the keycode, to refer later in th control function
Well I never knew that - thanks for the info. Although it doesn't seem to work for characters except numbers and letters, not picking up # or SHIFT etc



