View Full Version : Key.code problem...
dirtyprincess
03-08-2008, 05:20 PM
Hello!
Hope somebody can help me...
I'm having bad times trying to make a simple game.
Key codes like UP, DOWN, LEFT, RIGHT they work all fine, but whenever I try to make SHIFT or TAB or ALT work for me, the movie seems to get no input.
:confused:Why?
var listKey:Object = new Object();
listKey.onKeyDown = function () {
k = Key.getAscii();
if(k == 42) {
_global.order = 0;
_root.gotoAndPlay(3);
}
if (Key.getCode() == Key.DOWN) {
_root.acc.gotoAndStop(4);
}
if (Key.getCode() == Key.UP) {
_root.acc.gotoAndStop(3);
}
if (Key.getCode() == Key.LEFT) {
_root.acc.gotoAndStop(5);
}
if (Key.getCode() == Key.RIGHT) {
_root.acc.gotoAndStop(6);
}
//////////////////THIS IS THE REBEL !!!
if (Key.getCode() == Key.SHIFT) {
_root.acc.gotoAndStop(1);
}
}
Key.addListener(listKey);
atomic
03-08-2008, 05:45 PM
Try...
if (Key.isDown(Key.SHIFT)) {
_root.acc.gotoAndStop(1);
}
dirtyprincess
03-08-2008, 06:02 PM
Thank you Atomic, but it doesn't work neither :eek:
I hate stumbling with silly things !!!
atomic
03-08-2008, 06:06 PM
Want to attach your .fla in a Flash 8 format?
dirtyprincess
03-08-2008, 06:18 PM
I've just realize that if I try it as an standalone app it works, it doesn't work when embeded in my document (even if the movie is focus) so it may have something to do with some javascript messing with the key event functions...
atomic
03-08-2008, 06:42 PM
You sure the movie & the keyboard have focus?
http://www.artifactinteractive.com.au/dev/focus.html
dirtyprincess
03-08-2008, 07:37 PM
That's a precious hint, I'm working on it, by the way I have another question: I can retrieve wheter the user is pressing, number 1, 2, 3, 4, 5, 6, 7, 8, 9 but what about number 10, 11, 12...
Is there a way to track this? I don't know if toggle options could be related to that...
:rolleyes:
dirtyprincess
03-09-2008, 10:17 AM
Till the moment I've been working in this code, which, of course, doesn't work, any help will be very much apreciated:
_global.knumbers = new Array();
_global.knumber = 0;
_global.asci = Array();
/////////To translate ascii values
_global.asci[48]=0;
_global.asci[49]=1;
_global.asci[50]=2;
_global.asci[51]=3;
_global.asci[52]=4;
_global.asci[53]=5;
_global.asci[54]=6;
_global.asci[55]=7;
_global.asci[56]=8;
_global.asci[57]=9;
/////////To keep track of the events happening during toggling
if (Key.isToggled(Key.DOWN)) {
_global.knumbers.push(Key.getAscii());///Adding values to the toggling array
index1 = _global.knumbers[0];
index2 = _global.knumbers[1];
thenum = _global.asci[index1]._global.asci[index2];///Putting all numbers together
_global.knumber = Number(thenum);///Converting them to numbers??
}
dirtyprincess
03-09-2008, 01:01 PM
By the moment it seems to work fine, but it is just an appearance...
If you try my code and you create a textfield on your stage called "writeit" you will see that if you press and hold the DOWN key and you inmediately enter two numbers it will write them down as a single one: 3 and 4 will be 34.
The problem is that the first listener doesn't seem to do much...
If you press and hold the DOWN key and you wait for a second without typing anything else, a zero value will appear in the text box...
Why does it happens?
_global.knumbers = new Array();
_global.knumber = 0;
_global.thenum = 0;
_global.thenum1 = 0;
_global.thenum2 = 0;
var listKey1:Object = new Object();
listKey1.onKeyDown = function () {
if (Key.isDown(Key.DOWN)) {
var listKey:Object = new Object();
listKey.onKeyDown = function () {
_global.knumbers.push(Key.getAscii());
if (_global.knumbers.length == 2){
if (_global.knumbers[0] == 48){
_global.thenum = 0;
}
if (_global.knumbers[0] == 49){
_global.thenum = 1;
}
if (_global.knumbers[0] == 50){
_global.thenum = 2;
}
if (_global.knumbers[0] == 51){
_global.thenum = 3;
}
if (_global.knumbers[0] == 52){
_global.thenum = 4;
}
if (_global.knumbers[0] == 53){
_global.thenum = 5;
}
if (_global.knumbers[0] == 54){
_global.thenum = 6;
}
if (_global.knumbers[0] == 55){
_global.thenum = 7;
}
if (_global.knumbers[0] == 56){
_global.thenum = 8;
}
if (_global.knumbers[0] == 57){
_global.thenum = 9;
}
if (_global.knumbers[1] == 48){
_global.thenum1 = 0;
}
if (_global.knumbers[1] == 49){
_global.thenum1 = 1;
}
if (_global.knumbers[1] == 50){
_global.thenum1 = 2;
}
if (_global.knumbers[1] == 51){
_global.thenum1 = 3;
}
if (_global.knumbers[1] == 52){
_global.thenum1 = 4;
}
if (_global.knumbers[1] == 53){
_global.thenum1 = 5;
}
if (_global.knumbers[1] == 54){
_global.thenum1 = 6;
}
if (_global.knumbers[1] == 55){
_global.thenum1 = 7;
}
if (_global.knumbers[1] == 56){
_global.thenum1 = 8;
}
if (_global.knumbers[1] == 57){
_global.thenum1 = 9;
}
_global.thenum2 = ((_global.thenum)*10)+_global.thenum1;
_root.writeit.text = thenum2;
}
}
Key.addListener(listKey);
}
}
Key.addListener(listKey1);
dirtyprincess
03-09-2008, 07:30 PM
Oh come'on I need a hand with this, I'm stuck :(
As I see, with my last code the Key.Down counts as a value so when you hold it is sending values till it fills the array...
I need the array to skip the values throwed by the holded Down arrow so it keeps waiting till good values fill it...
Durnus
03-11-2008, 09:04 PM
Well, I'm not entirely sure on what you're planning, but what I'm doing for a current game I'm working on was make an array to store which keys are pressed.
I made a simple array where array[0] is a boolean of whether or not up is pressed. Indexes 1, 2, and 3 are down, left, and right, respectively. Then on a KeyboardEvent.KEY_DOWN (I use AS3) event I check the key pressed and update that index in the array on its new status. Then I have another loop going every frame loop that uses that array to decide how the player moves. It may seem complicated at first look but once understood it's actually very, very useful. Not sure how to do it precisely in AS2 however...
Good luck though. Maybe you could tel us more on what you are planning to do? (I can't really read AS2 that well, doesn't look structured right to me XD)
|
vBulletin® v3.8.5, Copyright ©2000-2012, Jelsoft Enterprises Ltd.