View Full Version : disable keyboard navigation
Caryn1980
08-20-2007, 03:12 PM
I have a flash presentation that uses the arrow keys and a drop down menu as navigation. Is it possible to disable the keyboard navigation ONLY AFTER the menu has been used?
Well, it would depend on how you were coding the keyPress event, but essentially you would just place a script to remove it in each button function of the menu.
Alternately you could declare a variable on a menu button press and evaluate that variable in the keyPress event. (If it finds the variable to be true, it deletes itself), but the variable solution would be a little harder on the processor as it would be evaluating on every press of the key, or enterFrame, depending on how you were listening for that event.
Caryn1980
08-20-2007, 04:08 PM
ok I sort of understand what you are saying. I'm new to AS.
But basically I arranged the presentation by dividing it into sections. Each section is it's own movie clip. In the movie clip, at each frame where I want the keys to work, I have script like this:
stop();
this.onKeyDown = function() {
if (Key.getCode() == (Key.RIGHT)) {
this.gotoAndPlay("page1");
Key.removeListener(this);
}
if (Key.getCode() == (Key.LEFT)) {
this.gotoAndPlay(1);
Key.removeListener(this);
}
};
Key.addListener(this);
and on the last frame of each movie clip I have this:
stop();
this.onKeyDown = function() {
if (Key.getCode() == (Key.LEFT)) {
this.gotoAndPlay(127);
Key.removeListener(this);
}
if (Key.getCode() == (Key.RIGHT)) {
_root.gotoAndPlay("people_mc");
Key.removeListener(this);
}
};
Key.addListener(this);
so that it plays the next movie clip. In the main timeline, I have frame labels for each movie clip section and at the end of each one I have a stop.
If I'm understanding you correctly, I have to put a remove keyPress event in the script for the buttons of my menu? Would you be able to provide me with more detailed information on this?
Well, I thought you meant you were declaring once listener at the base of your file, I didn't realize you were creating them all over the place...
Try this, it isn't the cleanest method, but it should work without having to change too much of your file.
In each onRelease function of your menu's buttons, add a variable declaration like this:
this.myMenu.button1.onRelease = function() {
_global.menuPressed = true;
};
this.myMenu.button2.onRelease = function() {
_global.menuPressed = true;
};
//etc...
Then in all your listeners, evaluate it.
It basically says, if the variable has been declared (as true), remove the lisener, if not, run the script you already had.
this.onKeyDown = function() {
if (_global.menuPressed) {
Key.removeListener(this);
} else {
if (Key.getCode() == (Key.RIGHT)) {
this.gotoAndPlay("page1");
Key.removeListener(this);
}
if (Key.getCode() == (Key.LEFT)) {
this.gotoAndPlay(1);
Key.removeListener(this);
}
}
};
Key.addListener(this);
Using _global in this fashion is not the greatest practice, but without knowing your paths/project it is the easiest solution.
Caryn1980
08-20-2007, 08:24 PM
Thanks for the assistance. Unfortunately, it didn't work. I have a feeling it has to do with my menu button structure. It looks like this with the section you suggested using:
stop();
button1.onPress = function(){
gotoAndStop(1)
_parent.gotoAndPlay("intro");
}
this.menu_mc.button1.onRelease = function() {
_global.menuPressed = true;
};
button2.onPress = function(){
gotoAndStop(1)
_parent.gotoAndStop("approach_mc");
}
etc...
All my menu buttons are in a movie clip called menu_mc. The first frame is empty and the second one starts a tween for them to move downward. I don't think that part should make a difference though.
As I said, I'm new to AS. So if I've created the listeners in a more difficult way (which it seems I did based on your comment about declaring a listener in the base of my file) then any assistance would be appreciated.
Well, from a best practices standpoint, we usually try to keep all code in one place. You will absolutely have to divert from that at times, but generally only for timeline specific actions that would be more processor intensive to evaluate from the main timeline. Like, say for instance I wanted to stop a clip on frame 50, I could stick a function on my main timeline that evaluated said clip on every frame and stopped it when it got to 50, but it would be much smarter to just place that stop command on frame 50 of the clip itself...
In your case I would probably declare the keyListener at the base of the file and then evaluate what clip is currently running right from within the listener.
Then you would only need one listener, and it would be much easier to control turning it on and off...
Apart from that, the code you're showing confuses me...
Are the buttons with the onPress functions you are showing the same menu buttons we are talking about?
If so, they shouldn't have a different path in the variable declaration then they do in the onPress events...
For that matter, they shouldn't be two seperate events; just place the variable declaration I gave you into the functions you already have.
(BTW, it is usually better to use onRelease then onPress for navigation/buttons)
button1.onPress = function(){
gotoAndStop(1)
_parent.gotoAndPlay("intro");
_global.menuPressed = true;
}
If you could post your file I could help a lot more...
Caryn1980
08-20-2007, 09:13 PM
Ohhh, ok. I understand. Yes, I was writing the variable wrong. I changed it to how you said and it worked! I also changed it to onRelease
If you wouldn't mind continuing to assist me, I am trying to understand the best practices point you made.
In your case I would probably declare the keyListener at the base of the file and then evaluate what clip is currently running right from within the listener.
Then you would only need one listener, and it would be much easier to control turning it on and off...
How would I do this? I have the listeners in the middle of the clip as well as at the end of each one. I am trying to attach my .fla but am having some trouble because it's so big, even if I zip it.
Strip out anything the file doesn't need first.
(For safety sake, make a copy of the fla and close the original.)
Go into the library and then either using the drop down menu in the upper right hand corner, or by right clicking somewhere in the blank space, choose "Select Unused Items". Delete the items selected, then from the main File menu, select "Save and Compact".
Those two methods should signifigantly shrink down the size of your fla. If after zipping that result up it is still too large, try creating a small example file that uses your code but without all the file heavy objects such as images or audio.
Caryn1980
08-21-2007, 03:21 PM
Ok got it now. I've stripped out the intro animation and many of the movie clips. I left 2 there so you can see how I wrote the script and everything. This piece is for my boss. He wants to be able to use it as a template for later presentations, so if I have to re-organize it to make that easier, then I'll take any suggestions offerred.
Again, thank you so much for your help!
Ok, take a look at the file I'm attaching, it has all the code on the first frame. You actually don't even need to kill the listener as it is, I have it checking what clip is on the stage anytime a key is pressed and performing different actions dependant on that.
It might seem a little overwhelming at first, but if you don't understand anything, just ask.
this.mainButton.onPress = function() {
this._parent.menu_mc.gotoAndPlay(2);
};
//Declare var to hold menuPressed status
var menuPressed:Boolean = false;
//Key Listener ----------------------------------------------------------//
//When the key is pressed we will check the timeline,
//Depending on what frame it is, and what clips are available,
//we will perform different actions
//This variable will be used below:
var movieJump:Boolean = false;
//Declare keyFunction: (pass it a clip to look for)
function KeyFunc(mC:MovieClip) {
//If menuPressed is true, kill all key Listening:
if (menuPressed) {
Key.removeListener(this);
//Else run the key functions
} else {
//If the clip passed to the function is not undefined (if it is on the stage)
if (mC != undefined) {
//Check current frame of movieClip in question
switch (mC._currentframe) {
//If it is on frame 91
case (91) :
//Use these values
var frameLabel:String = "page1";
var frameNum:Number = 1;
break;
//If it on Frame 127,
case (127) :
//Determine which clip this is:
switch (mC._name) {
case ("approach_mc") :
//Use these values
var frameLabel:String = "page2";
var frameNum:Number = 91;
break;
case ("people_mc") :
//Use these values
//Im temporarily showing the menu here just to see it in action
//You could uncomment the lines below this to get the navigation you had back
//var frameLabel:String = "technology_mc";
//var frameNum:Number = 91;
this.gotoAndStop("menu");
this.menu_mc.gotoAndPlay(2);
break;
}
break;
//If it on Frame 147,
case (147) :
//Determine which clip this is:
switch (mC._name) {
case ("approach_mc") :
//Use these values
var frameLabel:String = "page2";
var frameNum:Number = 127;
//Determine which clip this is:
switch (mC._name) {
case ("approach_mc") :
//Use these values
//Do not pass a label here
var frameLabel:String = "";
var frameNum:Number = 127;
//toggel movieJump to true
movieJump = true;
//Pass a label to send to the main timeline
var movieLabel:String = "people_mc";
break;
case ("people_mc") :
//Use these values
break;
}
break;
case ("people_mc") :
//Use these values
break;
}
}
//Declare key Press events
if (Key.getCode() == (Key.RIGHT)) {
//If not a movieJump spot, pass the label to move to
if (movieJump) {
_root.gotoAndPlay(movieLabel);
movieJump = false;
} else {
mC.gotoAndPlay(frameLabel);
}
}
if (Key.getCode() == (Key.LEFT)) {
mC.gotoAndPlay(frameNum);
}
}
}
}
//Declare actual keyPress event
this.onKeyDown = function() {
//When a key is pressed, run the function once for each clip
KeyFunc(this.approach_mc);
KeyFunc(this.people_mc);
};
Key.addListener(this);
//Nav Buttons-------------------------------------------------------------//
//Create re-usable function so we don't have to type it all out each time
//When you call this funciton you pass it the path and the frameName
function menuFunc(path:MovieClip, frameName:String) {
path.gotoAndStop(1);
if (frameName == "intro") {
path._parent.gotoAndPlay(frameName);
} else {
path._parent.gotoAndStop(frameName);
}
menuPressed = true;
}
this.menu_mc.button1.onRelease = function() {
//Path is this, frameName = "intro"
menuFunc(this, "intro");
};
this.menu_mc.button2.onRelease = function() {
menuFunc(this, "approach_mc");
};
this.menu_mc.button3.onRelease = function() {
menuFunc(this, "people_mc");
};
this.menu_mc.button4.onRelease = function() {
menuFunc(this, "technology_mc");
};
this.menu_mc.button5.onRelease = function() {
menuFunc(this, "range_mc");
};
this.menu_mc.button6.onRelease = function() {
menuFunc(this, "contact_mc");
};
Caryn1980
08-26-2007, 05:05 PM
Thanks for your help, Noct. I'm going to go through your test file and make sure I understand it. I appreciate your assistance!
|
vBulletin® v3.8.4, Copyright ©2000-2009, Jelsoft Enterprises Ltd.