PDA

View Full Version : on (keyPress "<Left>") not in a button


Erikina
05-16-2005, 09:15 PM
I really need to use on (keyPress "<Left>") in my action layer and not having it attached to a button. Is this possible? I'm pretty sure that I have seen it done somewhere, but I just can't remember where.

Also, would you happen to know how to make a variable global? And how to call it?

Thanks a lot guys.

zer0ice
05-16-2005, 09:20 PM
yes its possible...you would attach it to a movie clip instead. I like to have a blank mc off the stage that has my script to listen for certain keys to be pressed.

Erikina
05-16-2005, 09:23 PM
yes its possible...you would attach it to a movie clip instead. I like to have a blank mc off the stage that has my script to listen for certain keys to be pressed.

Thanks for that. I guess I will have to do that. I was hoping there was a way without using movieclips or buttons. Anyone know how to global variable? :D

zer0ice
05-16-2005, 09:26 PM
Thanks for that. I guess I will have to do that. I was hoping there was a way without using movieclips or buttons. Anyone know how to global variable? :D

you would do the following:

_global.TheNameofYourVariable = What you want the variable to be set to.


you can acess this variable anywhere on any timeline by typing _global.TheNameofYourVariable

acolyte
05-16-2005, 09:50 PM
Hello Erikina take a look at this it will solve your problem

http://www.actionscript.org/forums/showthread.php3?t=65253

enjoi Mat

zer0ice
05-16-2005, 09:58 PM
the link above reference...placing the code on a move clip too. But, they are using a key object on the movie clip...which I decided to leave out since the question was an easy one I figured Erikina was kinda new to Action Script.

so..if you really want to get away from the movie clip and button idea you can use the following:

var speed:Number = 3;

_root.onEnterFrame = function(){
if(Key.isDown(Key.RIGHT)){
balloon_mc._x += speed;
}
else if(Key.isDown(Key.LEFT)){
balloon_mc._x -= speed;
}
if(Key.isDown(Key.UP)){
balloon_mc._y -= speed;
}
else if(Key.isDown(Key.DOWN)){
balloon_mc._y += speed;
}
}




I used this to control a balloon on the screen using the keyboard.

acolyte
05-30-2005, 12:02 PM
the link above reference...placing the code on a move clip too. But, they are using a key object on the movie clip...which I decided to leave out ..............
I used this to control a balloon on the screen using the keyboard.

/ so why 'Mr. Zeroice' you are nagging on 'our Way' if you just post it in the same way ??/ :confused: ---

icio
05-30-2005, 01:21 PM
you could also use this method:
var myKeyHook:Object = new Object();
myKeyHook.onKeyDown = function() {
if (Key.getCode() == Key.LEFT) {
trace("you pressed the Left arrow key");
}
}
Key.addListener(myKeyHook);

hope this helps :)