Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

key.isDown with an if statement? 2

Status
Not open for further replies.

CarddassX

Programmer
Apr 2, 2002
9
US
I want to make a toggle switch with the key.isDown function. I want the user to be able to press the key and toggle the visibility on and off for a movie clip.
So, if user clicks 'h' (keycode 72) or 'H' (keycode 104) they will turn on visibility for the movie clip "helpmenu".
If the user presses it again it'll turn off for 'helpmenu'. The Key.isDown function must be used in this case as Keypress only supports one key command. And Key.isDown only works in a if statement, as far as I know. So I'm royally confused.
The coding below doesn't work but I'm trying to fudge with it so it does.

=============================
onClipEvent (enterFrame) {
if (Key.isDown (72, 104)) {
if (_root.menu_up == false) {
setProperty (helpmenu, _visible, true);
_root.menu_up = true;
}
} else if (_root.menu_up == true) {
setProperty (helpmenu, _visible, false);
_root.menu_up = false;
}
}
=============================

I asked a similar question in regard to keyPress last week sometime, and oldnewbie provided the answer which I incorporated above. Unfortunately I can't translate it to Key.isDown terms. If anyone can help me out, thanks in advance!

-Cardy
 
hows this :

onClipEvent (load) {
_root.menu_up = false;
}

onClipEvent (enterFrame) {
if (Key.isDown (72, 104)) {
if (_root.menu_up != true) {
setProperty (helpmenu, _visible, true);
_root.menu_up = true;
} else {
setProperty (helpmenu, _visible, false);
_root.menu_up = false;
}
}
} Regards

Big Bad Dave

logo.gif


davidbyng@hotmail.com
 
This is a better way of doing it :

onClipEvent (load) {
var menu_up = false;
_visible = false;
}
onClipEvent (enterFrame) {
if (Key.isDown(72, 104)) {
if (menu_up != true) {
_visible = true;
menu_up = true;
} else {
_visible = false;
menu_up = false;
}
}
}

stick this on the actions of the help menu movieclip Regards

Big Bad Dave

logo.gif


davidbyng@hotmail.com
 
The script I suggested works fine!

onClipEvent (load) {
_root.menu_up = false;
}
onClipEvent (enterFrame) {
if (Key.isDown(72, 104)) {
if (_root.menu_up == false) {
_root.helpmenu._visible = true;
_root.menu_up = true;
} else {
_root.helpmenu._visible = false;
_root.menu_up = false;
}
}
}

Two possible problems with this though. Depending of the sensitivity and/or the keyboard's repeat rate, this toggle will be touchy, because you can sort of set the helpmenu to visible and invisible on the same press, if you keep your finger on the key too long or somewhat double click it.
Second, depending on how your movie clip is set up (is it invisible to start with?), this might not work at all, especially if used it in a one frame movie!

Regards,
new.gif
 
BigBadDave, Oldnewbie, you guys rock!
Both of what you guys gave me worked perfectly.
And as you mentioned Oldnewbie, the key repeat rate makes it a little trickier to use. And the movie starts off invisible, and it appears when I use the code you and BigBadDave posted. I'll probably just use the key.isDown when I use key commands with ctrl in it.

I have this project due tomorrow and I decided to take on the challenge of doing a mock web browser in flash. I wished I had learned a little more flash before going it to it though. I'm stripping my mock web broswer of all it's buttons and doohickies and just make it a keyboard controlled program. I still have an affinity for the DOS era where it was all about typing the commands. hehe

Once again, thanks for the help!

-Cardy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top