I've created a list menu by placing Button symbols in a vertical row, for the user to be able to go through them with the up/down keys (each keypress highlighting the next button). However, sometimes they are highlighted in the wrong order, ie if I'm on menu item no 3 and press down, I might go to item no 5. When going upwards the "skips" are different, but in both directions always consistent.
The buttons are all of the same size, but obviously have different amounts of text on them. I've tried tracking them as both Menu Items and Buttons, but it doesn't seem to make any difference.
normally developers do not rely upon flash's tab order to control focus.
Instead developers tend to create an off screen button to trap events and then change the appearance of on screen elements (not buttons but movie clips or graphics) to show focus.
Thanks for your answer, sounds reasonable. However, since I'm just starting out and am stuck with a very limited deadline for this application, I'll just go with the easier (?) focus tabbing.
They're ordered in a vertical column. I've tried spacing them out a little further (they're pretty close to each other), but it didn't seem to help.
Currently I'm not using any movie clips at all, the buttons are all directly on "scene 1". Is this the wrong way to do it?
I'm using Flash Prof CS3 and Lite 2.0.
Thanks for taking the time to help me in this! I'm just starting out, so I'm having trouble with a few things as you can tell.
EDIT: Another wierd observation: If I push to the right a couple of times at some place in the menu (sometimes shifts the focus up or down a slot or two, depending on where focus is), the menu seems to scroll correctly after that. Strange...
Last edited by squallsquall; 03-17-2008 at 04:55 AM.
I've tried a different approach now, more like the one you suggested. There, I have one keyframe for each button (although now they're only images) focus, with a corresponding ActionsScript looking like this:
var myListener:Object = new Object();
myListener.onKeyDown = function() {
if (Key.getCode() == Key.LEFT) {
gotoAndStop(2);
} else if (Key.getCode() == Key.RIGHT) {
gotoAndStop(4);
} else if (Key.getCode() == Key.UP) {
gotoAndStop(9);
} else if (Key.getCode() == Key.DOWN) {
gotoAndStop(6);
}
};
Key.addListener(myListener);
However, it behaves strange as well, with seemingly random movements even though I have double checked the numbers. Also, sometimes it becomes sluggish after a while and even freezes Flash. Is it a problem that listeners on different frames are added with the same namne, or that when I return to a fram, the same listener is added again?
you only need to declare the key listener once, probably best in frame 1.
by the way you may find a switch/case statement more friendly for this sort of thing.
switch(Key.getCode()){
case key.LEFT: gotoAndStop(2); break;
case key.RIGHT: gotoAndStop(4); break;
case key.UP: gotoAndStop(9); break;
case key.DOWN: gotoAndStop(6); break;
}
Yeah, figured all the declarations were the cause of the error. Current design is like this.
Frame1:
var myListener:Object = new Object();
Key.addListener(myListener);
gotoAndStop(6);
Frames 2-10 each similar to this (but with "their own" numbers):
myListener.onKeyDown = function() {
if (Key.getCode() == Key.LEFT) {
gotoAndStop(4);
} else if (Key.getCode() == Key.RIGHT) {
gotoAndStop(6);
} else if (Key.getCode() == Key.UP) {
gotoAndStop(2);
} else if (Key.getCode() == Key.DOWN) {
gotoAndStop(8);
}
};
Is that the correct way to first declare and then have a unique function for each state?
The funny this now is that navigation works like a charm as long as the gotoAndStop() directs to frames 2-5. If it directs to 6 it still goes to frame 5 but then works, and if it directs to 6-10 the application does not respond to key presses at all (although Flash itself does not crash). I guess it's kind of hard for you to find the cause through this forum, though... the thing is I've checked each frame and their actionscript is built the exact same way (except for the numbers).
EDIT: Will try out the Switch/Case, although I suspect you meant it more as an easier way of coding than actual bug-fix.
you only need to set up the code for all key events once. You do not need one in each frame. The switch/case is not a bug fix but an easier code to use.
I thought of that as well, my problem is that the buttons' action depend on which frame the application is currently on (for example, if on frame 5 "UP" might mean gotoAndStop(2), but if on frame 9 the same "UP" keypress should trigger gotoAndStop(5)). When declaring the function for the keys only once, I guess it needs to be more dynamic. Is there a way to check which frame the application is currently on (I searched for such a feature, but didn't find any), or do you recommend another way of solving this?
Eventhough it requires more typing and longer code, it may be easier to manage the logic for this if you put all the code into functions and events in the first frame.
You can get the current frame with the _currentframe property.
I would probably create an event handler function for each type of button event.
Code:
// function to process "up" key presses depending upon current frame
function onUpKeyPress(){
switch(_currentframe){
case 2: gotoAndStop(?); break; // move to appropriate frame
case x: gotoAndStop(?); break; // move to appropriate frame
}
}
// listener code to trap key down events and assign them to functions
myListener.onKeyDown = function() {
switch(Key.getCode()){
case Key.UP: onUpKeyPress(); break;
case Key.DOWN: onDownKeyPress(); break;
}
}