我有一个带有动画片段的数组,我放在舞台上。我想使用键盘箭头键分别更改每个动画片段的alpha,就像你正在通过它们导航一样(我希望这样做)。 到目前为止,我只能使用向上/向下箭头一次突出显示它们。我的目标是使用alpha属性使用高光和筒灯循环它们。
这是我的代码:
import flash.events.KeyboardEvent; import flash.events.Event;
var num1:Number = 262; var aantal:Number = 8;
function Main() {
var BTN_arr: Array = new Array();
var houder: Number = 1;
var aantal2: uint = BTN_arr.length;
var nextBTN: uint;
var currentBTN: uint;
for (var i = 0; i < aantal; i++) {
var myBTN: BTNBg = new BTNBg();
myBTN.name = "btn" + i;
BTN_arr.push(myBTN);
addChild(myBTN);
myBTN.alpha = .45;
myBTN.x = 40;
myBTN.y = num1;
num1 += 90;
}
BTN_arr[0].alpha = 1;
stage.addEventListener(KeyboardEvent.KEY_DOWN, myKeyDown);
function myKeyDown(e: KeyboardEvent): void {
if (e.keyCode == Keyboard.DOWN) {
for (var i = 0; i < BTN_arr.length; i++) {
BTN_arr[i].alpha = 1;
}
}
trace("down");
if (e.keyCode == Keyboard.UP) {
for (var j = 0; j < BTN_arr.length; j++) {
BTN_arr[j].alpha = .45;
}
trace("up");
//MyBTN.alpha = 1;
}
}
}
Main();
答案 0 :(得分:0)
var position:int = 0;
function updateHighlight() : void{
BTN_arr[position].alpha = 1; //highlight new one
}
function myKeyDown(e: KeyboardEvent): void {
if (e.keyCode == Keyboard.DOWN) {
trace("down");
if(position > 0){
BTN_arr[position].alpha = .45; //unhighlight current
position--;
updateHighlight();
}
}
if (e.keyCode == Keyboard.UP) {
trace("up");
if(position < BTN_arr.length - 1){ //is not the last one
BTN_arr[position].alpha = .45; //unhighlight current
position++;
updateHighlight();
}
}
}
答案 1 :(得分:0)
要做你想做的事,你不需要每次都使用for
循环设置按钮alphas,这样你就可以使用var来设置当前按钮,你只需设置alpha就可以了,像这样:
var current_button:int = 0;
var sens:int = 0; // -1 : up, 1 : down
stage.addEventListener(Event.ENTER_FRAME, _onEnterFrame)
function _onEnterFrame(e:Event):void {
if(sens != 0){
BTN_arr[current_button].alpha = .45;
if(0 <= current_button + sens && current_button + sens < BTN_arr.length) current_button += sens;
// if you want to pass from the last button to the first one and vice versa, you can enable these 3 lines and disable the 1st if
//current_button += sens;
//if(current_button < 0) current_button = BTN_arr.length - 1;
//else if(current_button >= BTN_arr.length) current_button = 0;
BTN_arr[current_button].alpha = 1;
sens = 0;
}
}
stage.addEventListener(KeyboardEvent.KEY_DOWN, _onKeyDown);
function _onKeyDown(e: KeyboardEvent): void {
if (e.keyCode == Keyboard.DOWN) {
sens = 1;
} else if (e.keyCode == Keyboard.UP) {
sens = -1;
}
}
对于第二种情况(启用了3条注释行),这将为您提供this,类似this。
希望可以提供帮助。