(ActionScript 3.0)我的函数不是通过Button Event更新全局变量

时间:2016-05-25 07:22:54

标签: actionscript-3 flash events actionscript

我试图弄清楚如何更新我的全局变量。目前我只是在ActionScript 3框架上推送所有代码,而不是使用外部ActionScript文件。

然而,这是我的代码:

import flash.ui.Keyboard;
import flash.events.MouseEvent;
import flash.events.KeyboardEvent;

var SPEED:int = 10;
var speed_multiplier:int = 1;

stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDown);
multiplier_two.addEventListener(KeyboardEvent.CLICK, twoButton);

function keyDown(e:KeyboardEvent):void {
   switch(e.keyCode) {
      case(Keyboard.LEFT):
         character.x -= SPEED*speed_multiplier;
         collisionDetection(3);
         break;

      case(Keyboard.RIGHT):
         character.x += SPEED*speed_multiplier;
         collisionDetection(4);
         break;

      case(Keyboard.UP):
         character.y -= SPEED*speed_multiplier;
         collisionDetection(1);
         break;

      case(Keyboard.DOWN):
         character.y += SPEED*speed_multiplier;
         collisionDetection(2);
         break;
   }
}

function twoButton(e:MouseEvent):void {
    speed_multiplier = 2;
}

我已经做了一些追踪,以确保工作正常。按钮的单击事件工作正常。但是,当我点击按钮时,我的speed_multiplier的gloval变量没有更新。

1 个答案:

答案 0 :(得分:1)

multiplier_two.addEventListener(KeyboardEvent.CLICK, twoButton);

应该是

multiplier_two.addEventListener(MouseEvent.CLICK, twoButton);

不知道按钮的点击事件是如何正常工作的,但这是引起我注意的唯一因素。试试吧。