Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

AS3 Keyboard script

Explorer ,
Jan 05, 2018 Jan 05, 2018

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.

711
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Explorer , Jan 05, 2018 Jan 05, 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.

Translate
Community Expert ,
Jan 05, 2018 Jan 05, 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);

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 05, 2018 Jan 05, 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()

}

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jan 05, 2018 Jan 05, 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?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jan 05, 2018 Jan 05, 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 05, 2018 Jan 05, 2018
LATEST

you're welcome.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines