我正在使用Adobe Flash CS6学习AS3,我正在编写一个教程,使用KeyboardEvent
创建一个带有动画的移动对象,它运行得很好但现在我想改变它所以我可以使用屏幕上的按钮(MouseEvent
s)移动角色,但我不确定如何去做。
var movingRight;
var movingLeft;
function onKeyPress(e:KeyboardEvent):void
{
if(e.keyCode == Keyboard.RIGHT){
movingRight=1;
}
else if(e.keyCode == Keyboard.LEFT){
movingLeft=1;
}
}
答案 0 :(得分:2)
要移动character
使用按钮,您必须使用两个MouseEvent
:按下按钮时发生MouseEvent.MOUSE_DOWN
事件,发布时MouseEvent.MOUSE_UP
发布LeftBTN.addEventListener(MouseEvent.MOUSE_DOWN, LeftBTN_onPress);
LeftBTN.addEventListener(MouseEvent.MOUSE_UP, LeftBTN_onRelease);
function LeftBTN_onPress(e:MouseEvent):void
{
movingLeft = 1;
}
function LeftBTN_onRelease(e:MouseEvent):void
{
character.gotoAndStop(1);
movingLeft = 0;
}
RightBTN.addEventListener(MouseEvent.MOUSE_DOWN, RightBTN_onPress);
RightBTN.addEventListener(MouseEvent.MOUSE_UP, RightBTN_onRelease);
function RightBTN_onPress(e:MouseEvent):void
{
movingRight = 1;
}
function RightBTN_onRelease(e:MouseEvent):void
{
character.gotoAndStop(4);
movingRight = 0;
}
,所以你可以这样做:
stage.addEventListener(KeyboardEvent.KEY_DOWN, move_character);
stage.addEventListener(KeyboardEvent.KEY_UP, move_character);
LeftBTN.addEventListener(MouseEvent.MOUSE_DOWN, move_character);
LeftBTN.addEventListener(MouseEvent.MOUSE_UP, move_character);
RightBTN.addEventListener(MouseEvent.MOUSE_DOWN, move_character);
RightBTN.addEventListener(MouseEvent.MOUSE_UP, move_character);
function move_character(e:*){
var event_type:String = e.type; // get event type : to identify which kind of event is fired
var current_target:String = e.currentTarget.name; // get target name : to identify the target of the fired event
switch (event_type){
case MouseEvent.MOUSE_DOWN :
if(current_target == 'LeftBTN') movingLeft = 1;
else movingRight = 1;
break;
case MouseEvent.MOUSE_UP:
if(current_target == 'LeftBTN'){
character.gotoAndStop(1);
movingLeft = 0;
} else {
character.gotoAndStop(4);
movingRight = 0;
}
break;
case KeyboardEvent.KEY_DOWN:
if (e.keyCode == Keyboard.RIGHT){
movingRight = 1;
} else if (e.keyCode == Keyboard.LEFT) {
movingLeft = 1;
}
break;
case KeyboardEvent.KEY_UP:
if (e.keyCode == Keyboard.LEFT) {
character.gotoAndStop(1);
movingLeft = 0;
} else if (e.keyCode == Keyboard.RIGHT) {
character.gotoAndStop(4);
movingRight = 0;
}
break;
}
}
您还可以为所有事件监听器使用一个函数,如下所示:
e
或者,使用Event
参数作为function move_character(e:Event){
var event_type:String = e.type; // get event type : to identify which kind of event is fired
var current_target:String = e.currentTarget.name; // get target name : to identify the target of the fired event
var key_code:Number;
switch (event_type){
case MouseEvent.MOUSE_DOWN :
if(current_target == 'LeftBTN') movingLeft = 1;
else movingRight = 1;
break;
case MouseEvent.MOUSE_UP:
if(current_target == 'LeftBTN'){
character.gotoAndStop(1);
movingLeft = 0;
} else {
character.gotoAndStop(4);
movingRight = 0;
}
break;
case KeyboardEvent.KEY_DOWN:
key_code = KeyboardEvent(e).keyCode;
if (key_code == Keyboard.RIGHT){
movingRight = 1;
} else if (key_code == Keyboard.LEFT) {
movingLeft = 1;
}
break;
case KeyboardEvent.KEY_UP:
key_code = KeyboardEvent(e).keyCode;
if (key_code == Keyboard.LEFT) {
character.gotoAndStop(1);
movingLeft = 0;
} else if (key_code == Keyboard.RIGHT) {
character.gotoAndStop(4);
movingRight = 0;
}
break;
}
}
:
{{1}}
希望可以提供帮助。
答案 1 :(得分:0)
在舞台上放两个按钮,将它们命名为btnRight和btnLeft,然后使用此代码
btnLeft.addEventListener(MouseEvent.CLICK, onButtonClick, false, 0, true);
btnRight.addEventListener(MouseEvent.CLICK, onButtonClick, false, 0, true);
function onButtonClick(evt:MouseEvent) {
if (evt.target == btnLeft) movingLeft = 1;
else movingRight = 1;
}
我还建议只使用一个名为movingDirection的var,它可以是1,0或-1:)