Skip to main content
hartz
Inspiring
January 5, 2018
Answered

AS3 Keyboard script

  • January 5, 2018
  • 2 replies
  • 804 views

I have 101 frames that I want to advance or return by using the left and right keyboard arrows.  One Caveat, when I am on frame 1;  I want the Right arrow to go to frame 101.  I used this code:

stage.addEventListener(KeyboardEvent.KEY_DOWN, myKeyDown);

function myKeyDown(event:KeyboardEvent):void

{

    if (event.keyCode == Keyboard.RIGHT)

    {

gotoAndStop(101);

    }

}

Works fine.  But when I am on frame 101, I want the playhead to go to frame 2 when pressing the right arrow, and to frame 1 when pressing the left arrow.  Then when I am on frame 2,  I want the player to go to 3 when pressing the right arrow and back to 101 when pressing the left arrow.  From frame 3 - frame 100 simply next frame or previous frame.  From frame 100, the right arrow should bring it to 1.

In a nutshell, I have a number on each frame that corresponds to the frame number.  Since Frame one is set up as a landing page, I am using 101 as my frame 1.  I could put the above code on each frame, but that seems like too much work.  I'm an AS2 guy and can no longer access my CS6, so I'm forced to try to replicate super easy code with AS3.  Can anyone help me?  Can this be written with 'if' statements?  I tried but Animate just kept crashing.

This topic has been closed for replies.
Correct answer hartz

Okay Kglad, I figured it out:

stage.addEventListener(KeyboardEvent.KEY_DOWN, myKeyDown);

function myKeyDown(event:KeyboardEvent):void{

    if (event.keyCode == Keyboard.RIGHT){

if(currentFrame==100){

gotoAndStop(1)

} else if(currentFrame==101){

gotoAndStop(2);

}else if(currentFrame==1) {

gotoAndStop(101);

} else{

nextFrame()

}

    }

  if (event.keyCode == Keyboard.LEFT){

if(currentFrame==101){

gotoAndStop(1);

} else if(currentFrame==2){

gotoAndStop(101);

} else {

prevFrame()

}

}

}

THANK YOU FOR GETTING ME THERE.

2 replies

kglad
Community Expert
Community Expert
January 5, 2018

hartz

stage.addEventListener(KeyboardEvent.KEY_DOWN, myKeyDown);

function myKeyDown(event:KeyboardEvent):void{

    if (event.keyCode == Keyboard.RIGHT){

if(currentFrame==100){

gotoAndStop(1)

} else if(currentFrame==101){

gotoAndStop(2);

} else{

nextFrame()

}

    }

  if (event.keyCode == Keyboard.LEFT){

if(currentFrame==101){

gotoAndStop(1);

} else if(currentFrame==2){

gotoAndStop(101);

} else {

prevFrame()

}

}

hartz
hartzAuthor
Inspiring
January 5, 2018

Kglad, thank you very much.  It's way further than I had even hoped.  It just has one thing missing...the right arrow from frame one to frame 101.   I tried to insert this:

}else if(currentFrame==1)

gotoAndStop(101)

in the first part and it gave me a 1087 syntax error.

Currently it goes to frame 2 from frame one.  An easy fix?

hartz
hartzAuthorCorrect answer
Inspiring
January 5, 2018

Okay Kglad, I figured it out:

stage.addEventListener(KeyboardEvent.KEY_DOWN, myKeyDown);

function myKeyDown(event:KeyboardEvent):void{

    if (event.keyCode == Keyboard.RIGHT){

if(currentFrame==100){

gotoAndStop(1)

} else if(currentFrame==101){

gotoAndStop(2);

}else if(currentFrame==1) {

gotoAndStop(101);

} else{

nextFrame()

}

    }

  if (event.keyCode == Keyboard.LEFT){

if(currentFrame==101){

gotoAndStop(1);

} else if(currentFrame==2){

gotoAndStop(101);

} else {

prevFrame()

}

}

}

THANK YOU FOR GETTING ME THERE.

JoãoCésar17023019
Community Expert
Community Expert
January 5, 2018

So basically do you want a loop?

If so, try this:

import flash.events.KeyboardEvent;

import flash.ui.Keyboard;

function myKeyDown(event:KeyboardEvent):void

{

    if (event.keyCode == Keyboard.LEFT)

    {

        if (this.currentFrame > 1)

            prevFrame();

        else

            gotoAndStop(this.totalFrames);

    }  

    else if (event.keyCode == Keyboard.RIGHT)

    {

        if (this.currentFrame < this.totalFrames)

            nextFrame();

        else

            gotoAndStop(1);

    }

}

stop();

if (!stage.hasEventListener(KeyboardEvent.KEY_DOWN))

    stage.addEventListener(KeyboardEvent.KEY_DOWN, myKeyDown);