0
votes

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.

1
what is multiplier_two?suzuiyue
Slap a trace right under var speed_multiplier:int = 1; and should it trigger more than once per running the program, get the code off the timeline into a *.as file and forget about timeline coding entirely. (This looks like a code flow control problem, the code on a frame is triggered internally and you can't tell exactly when it will trigger)Vesper

1 Answers

1
votes
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.