AS3 将键盘事件更改为鼠标事件
AS3 changing KeyboardEvents to MouseEvents
我正在使用 Adobe Flash CS6 学习 AS3,我正在编写一个使用 KeyboardEvent
s 创建带有动画的移动对象的教程,它运行良好,但现在我想更改它以便我可以使用屏幕上的按钮 (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;
}
}
在舞台上放置两个按钮,将它们命名为 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 的变量,它可以是 1、0 或 -1 :)
要使用按钮移动 character
,您必须使用两个 MouseEvent
s:按下按钮时的 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;
}
}
希望能帮到你。
我正在使用 Adobe Flash CS6 学习 AS3,我正在编写一个使用 KeyboardEvent
s 创建带有动画的移动对象的教程,它运行良好,但现在我想更改它以便我可以使用屏幕上的按钮 (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;
}
}
在舞台上放置两个按钮,将它们命名为 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 的变量,它可以是 1、0 或 -1 :)
要使用按钮移动 character
,您必须使用两个 MouseEvent
s:按下按钮时的 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;
}
}
希望能帮到你。