import
flash.events.KeyboardEvent;
import
flash.display.DisplayObject;
import
flash.filters.DisplacementMapFilter;
const
MAX_BTN:
int
=
16
;
const
RIGHT:
int
=
0
;
const
LEFT:
int
=
1
;
const
UP:
int
=
2
;
const
DOWN:
int
=
3
;
var
pressedKey:
Array
=
new
Array
();
var
lastKey:
int
;
var
moveGraph:
Array
=
new
Array
(MAX_BTN);
var
focus:
int
=
0
;
var
time:
int
=
0
;
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDown_h);
stage.addEventListener(KeyboardEvent.KEY_UP, keyUp_h);
setFocus(
0
);
updateMoveGraph();
function
keyDown_h(evt:KeyboardEvent):
void
{
if
(getTimer()-time<
50
&& lastKey==evt.keyCode)
return
;
time = getTimer();
lastKey = evt.keyCode;
switch
(evt.keyCode) {
case
Keyboard.RIGHT:
case
Keyboard.LEFT:
case
Keyboard.UP:
case
Keyboard.DOWN:
moveFocus(evt.keyCode);
break
;
case
Keyboard.ENTER:
selectBtn();
}
pressedKey[evt.keyCode] =
true
;
}
function
keyUp_h(evt:KeyboardEvent):
void
{
pressedKey[evt.keyCode] =
false
;
if
(evt.keyCode == Keyboard.ENTER)
this
[
"btn"
+focus].gotoAndStop(
"over"
);
}
function
moveFocus(key:
uint
):
void
{
if
(key == Keyboard.RIGHT) {
setFocus(moveGraph[focus][RIGHT]);
}
else
if
(key == Keyboard.LEFT) {
setFocus(moveGraph[focus][LEFT]);
}
else
if
(key == Keyboard.UP) {
setFocus(moveGraph[focus][UP]);
}
else
if
(key == Keyboard.DOWN) {
setFocus(moveGraph[focus][DOWN]);
}
}
function
updateMoveGraph():
void
{
var
i:
int
, j:
int
;
var
dx:
Number
, dy:
Number
;
for
(i=
0
; i<MAX_BTN; i++) {
moveGraph[i] =
new
Array
(
4
);
moveGraph[i][RIGHT] = -
1
;
moveGraph[i][LEFT] = -
1
;
moveGraph[i][UP] = -
1
;
moveGraph[i][DOWN] = -
1
;
}
for
(i=
0
; i<MAX_BTN; i++) {
for
(j=
0
; j<MAX_BTN; j++) {
if
(i==j)
continue
;
dx =
this
[
"btn"
+i].x -
this
[
"btn"
+j].x
dy =
this
[
"btn"
+i].y -
this
[
"btn"
+j].y
if
(dx<
0
&& dx*dx>dy*dy) {
if
(moveGraph[i][RIGHT] == -
1
) moveGraph[i][RIGHT] = j;
else
if
(getNear(
this
[
"btn"
+i],
this
[
"btn"
+j],
this
[
"btn"
+moveGraph[i][RIGHT]]) ==
1
) {
moveGraph[i][RIGHT] = j;
}
}
else
if
(dx>
0
&& dx*dx>dy*dy) {
if
(moveGraph[i][LEFT] == -
1
) moveGraph[i][LEFT] = j;
else
if
(getNear(
this
[
"btn"
+i],
this
[
"btn"
+j],
this
[
"btn"
+moveGraph[i][LEFT]]) ==
1
) {
moveGraph[i][LEFT] = j;
}
}
else
if
(dy>
0
&& dx*dx<dy*dy) {
if
(moveGraph[i][UP] == -
1
) moveGraph[i][UP] = j;
else
if
(getNear(
this
[
"btn"
+i],
this
[
"btn"
+j],
this
[
"btn"
+moveGraph[i][UP]]) ==
1
) {
moveGraph[i][UP] = j;
}
}
else
if
(dy<
0
&& dx*dx<dy*dy) {
if
(moveGraph[i][DOWN] == -
1
) moveGraph[i][DOWN] = j;
else
if
(getNear(
this
[
"btn"
+i],
this
[
"btn"
+j],
this
[
"btn"
+moveGraph[i][DOWN]]) ==
1
) {
moveGraph[i][DOWN] = j;
}
}
}
}
}
function
getNear(src:DisplayObject, dest1:DisplayObject, dest2:DisplayObject):
int
{
var
distance1:
Number
;
var
distance2:
Number
;
distance1 = (src.x-dest1.x)*(src.x-dest1.x)+(src.y-dest1.y)*(src.y-dest1.y);
distance2 = (src.x-dest2.x)*(src.x-dest2.x)+(src.y-dest2.y)*(src.y-dest2.y);
if
(distance1<distance2)
return
1
;
else
if
(distance1>distance2)
return
-
1
;
return
0
;
}
function
setFocus(num:
int
):
void
{
if
(num==-
1
)
return
;
this
[
"btn"
+focus].gotoAndStop(
"up"
);
focus = num;
this
[
"btn"
+focus].gotoAndStop(
"over"
);
}
function
selectBtn():
void
{
if
(pressedKey[Keyboard.ENTER] !=
true
) {
this
[
"btn"
+focus].gotoAndStop(
"down"
);
}
if
(focus==
0
) {
}
if
(focus==
1
) {
}
if
(focus==
2
) {
}
if
(focus==
3
) {
}
}