سؤال

TLDR: I need code in action script 3 which will let users press keys like QWER to jump to a specific scene.

So what I'm doing is creating and interactive comic within flash, and to be blunt I didn't know any AS3 code before this and still pretty much know none.

So I'm going to need some help on this one and I think it is worth mentioning that I am using sound in this project.

what I need to know is how to use letter keys (eg. QWER) to act as shorts cuts to jump to specific scenes. What I have so far which is working is this and another version which uses a mouse click instead.

stop(); ( 1 ) 


stage.addEventListener(KeyboardEvent.KEY_DOWN, fl_KeyboardDownHandler);


function fl_KeyboardDownHandler(event:KeyboardEvent):void {



          gotoAndPlay(currentFrame+1);

}

and of course all this does is advance the frame, which I do need for some sections of dialogue but that's the basis I've been trying to get it to work.

I do know that q = 81, w = 87, e = 69 and r = 82.

Besides that I've got nothing and I need some help real bad.

هل كانت مفيدة؟

المحلول

You need to check which key has been pressed, you can do it like this:

function fl_KeyboardDownHandler(event:KeyboardEvent):void {
 if(event.keyCode == Keyboard.Q){
     //do what you need to do when Q was pressed
 }else if(event.keyCode == Keyboard.W){
     //same for W
 }

 ...etc
}

نصائح أخرى

The KeyboardEvent instance contains data about the event itself, part of it being keyCode, which gives the code of the pressed key. Using this property, you can detect which key the user pressed, and react accordingly.

As you understood, you can use gotoAndPlay() and gotoAndStop() to move around your animation.

It gives us the following code :

stop();
stage.addEventListener(KeyboardEvent.KEY_DOWN, fl_KeyboardDownHandler);

function fl_KeyboardDownHandler(event:KeyboardEvent):void {
  if(event.keyCode == Keyboard.Q) {
    gotoAndPlay(1); // Back to the start
  } else if (event.keyCode == Keyboard.W) {
    gotoAndPlay(currentFrame-1); // Back one frame
  } else if (event.keyCode == Keyboard.E) {
    gotoAndPlay(currentFrame-1); // Forward one frame
  } else if (event.keyCode == Keyboard.R) {
    gotoAndPlay(100); // Go to 100th frame
  }
}

Note that I am using the Keyboard class to get the keyCode associated to a specific key.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top