-1

I am trying to us actionscript 3 to try and use a variable to play a separate animation but it doesn't work in flash cs6 and my school won't update it.

I have tried to use it in the context of a variable but it always spits out an error message:

var currentDirection = event.charCode;

Scene 1, Layer 'Sprite', Frame 1, Line 10  1120:Access of undefined property event
avariant
  • 2,234
  • 5
  • 25
  • 33
  • Not enough info given. Did you `import flash.events.*;` in your code? Why does this line exist... `var currentDirection = event.charCode;` _eg:_ what is trying to achieve? Does it exist inside a (keyboard) Event handling function? If yes, then does using **keyCode** help you? Try `var currentDirection : int = event.keyCode;` – VC.One Jun 07 '19 at 06:28
  • i didn't add import `flash.events.*;` i only added `flash.events.keyboard;` and `flash.events.mouse;` and `var currentDirection = event.charCode;` exist because i was trying to put that int a variable to refer to it easier later it was trying to log the last key which was pressed and add more onto it. also keycode would not help as i need something to retain the last key pressed so the code you have provided wouldn't have helped me much what i am really looking for is (keyPressed) but in cs6 – question mark Jun 07 '19 at 14:42
  • I think the possible key could be within the question VC. One asked, "is it inside an event handling function?" I can't see the rest of the code surrounding it, so I'm mostly guessing, but based on that error, it feels like the answer might be 'no'. If that is the case, what could be causing the error is an attempt to access it, either out of scope or before any key has actually been pressed (in both cases, the 'event' object would not exist and thus be undefined). – mitim Jun 09 '19 at 04:51

1 Answers1

1

Perhaps what would help is a full example showing all the code needed in AS3 to accomplish this:

import flash.events.KeyboardEvent;

//var to hold the last key pressed. 
var currentDirection:int = -1; 

//listen for the key down event, when it happens, call the onKeyDown function below
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);

//this function runs whenever a key goes down
function onKeyDown(event:KeyboardEvent) {
    currentDirection = event.charCode; //assign the var the current key press char code
    trace(event.charCode); //trace to the output panel to see if it's working
}
BadFeelingAboutThis
  • 14,445
  • 2
  • 33
  • 40