Copy link to clipboard
Copied
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.
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.
Copy link to clipboard
Copied
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);
Copy link to clipboard
Copied
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()
}
}
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
you're welcome.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now