Hi all,
please see the attached file for reference. A couple of things :
I have a selection bar moving down or up 25 pixels on press, but it seems to be adding 2 pixels on each successive move, how do I get it to respect the 25 pixel movement restriction?
While the selection bar is moving, you can press the button again, breaking the code and sending the bar off the screen. Ive tried to solve this with a boolean (var moving) to tell whether the bar is moving or not, and thus prevent the movement code from running. However, once the bar has stopped, I cannot get it out of the moving = true state, to move the bar again.
edit: “I’d like to add a movement tween to the bar…” There is a thread above about this, plus millions of examples around the web.
Thanks for the help! File and code below.
-haus
var barMoveDistance = 25 //travel distance of selectionBar
var barSelectionOptions = 5; //number menu options available
var barCurrentSelection = 1 //current menu position of the selectionBar
var oldY = selectionBar._y //tracks previous/starting position of selectionBar
var moving:Boolean = false;
btnDown.onPress = function() {
trace(moving);
if (moving == false) { //ok to initiate selectionBar move
if (barCurrentSelection >= barSelectionOptions) {
/**do nothing, bar is at bottom of menu**/
} else {
oldY = selectionBar._y;
barDown = setInterval(moveBarDown, 50);
}
}else if (moving == true) {
/**do nothing on press**/
}
}
function moveBarDown() {
if (selectionBar._y >= oldY + barMoveDistance) { //stop the bar from moving down
++barCurrentSelection
moving = false;
clearInterval(barDown);
trace(barCurrentSelection);
trace("stopping");
}
selectionBar._y = selectionBar._y + 2 //movement speed
//moving = true;
}
btnUp.onPress = function() {
if (moving == false) { //ok to initiate selectionBar move
if (barCurrentSelection == 1) {
/**do nothing, bar is at top of menu**/
} else {
oldY = selectionBar._y;
barUp = setInterval(moveBarUp, 50);
}
} else if (moving == true) {
/**do nothing on press**/
}
}
function moveBarUp() {
if (selectionBar._y <= oldY - barMoveDistance) { //stop the bar from moving down
--barCurrentSelection;
moving = false;
trace(barCurrentSelection);
clearInterval(barUp);
trace("stopping");
}
selectionBar._y = selectionBar._y - 2 //movement speed
//moving = true;
}