T. Wolf
T. Wolf

Reputation: 83

(ActionScript 3.0) My function is not updating the global variable via Button Event

I am trying to figure out how to update my global variable. At the moment I am just shoving all my code at ActionScript 3 frame, instead of using external ActionScript file.

Nevertheless here is my code:

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;
}

I already did a trace, to make sure is working properly. The click event for the button is working fine. However my gloval variable for speed_multiplier is not being updated when I click on the button.

Upvotes: 0

Views: 71

Answers (1)

sfxworks
sfxworks

Reputation: 1091

multiplier_two.addEventListener(KeyboardEvent.CLICK, twoButton);

should be

multiplier_two.addEventListener(MouseEvent.CLICK, twoButton);

Don't know how the click event for your button is working fine but that's the only thing that caught my eye. Try that out.

Upvotes: 1

Related Questions