AS3: currentFrame & stop (); - actionscript-3

So, my next problem. Sorry for asking so much, but my teacher doens't respond. So, this doesn't work. The stop command doesn't seem too respond, but i don't get an error either. I know i miss a bracket, but my function isn't finished. Due to copy and paste the place of the brackets have shifted a bit.
function Knipperen (event:TimerEvent):void
{
if (event.currentTarget.currentCount == 3 && geknipperd < 3)
{
geknipperd ++;
timer.reset();
timer.start();
trace (geknipperd);
gotoAndPlay(1);
if (currentFrame == 13)
{
stop();
}
}

Here you go to frame 1.
gotoAndPlay(1);
And here you check if the frame is the 13th.
if (currentFrame == 13)
{
stop();
}
The condition failing is very much expected. I see you are using the trace command. Using it more liberally or adding breakpoints will help you with these trivial problems.

Related

Play only a certain range of frames

I have a combobox on the stage and changing its value I want to play only a certain range of frames:
stop();
combo01.addEventListener(Event.CHANGE, change);
function change(event:Event):void{
if (combo01.selectedItem.label == "BAL"){
gotoAndPlay(50);
if (currentFrame == 99) {stop();}
}
}
The game is not stopped but returned to frame 1.
You want your current frame check to happen when frame 99 is reached, not when change() is called. One way you can do this is to add a listener to check each frame as it is entered by the timeline until it reaches your desired frame:
addEventListener(Event.ENTER_FRAME, checkFrame);
function checkFrame(e:Event):void {
if(currentFrame == 99){
stop();
removeEventListener(Event.ENTER_FRAME, checkFrame);
}
}
Another way is to use the undocumented (but long supported) addFrameScript(): actionscript3 whats the point of addFrameScript
You could also just put a conditional stop() on frame 99, such as if (stopFrame == 99) stop(), then simply set stopFrame in your change handler.
You get the idea. You need your check to happen at frame 99.

AS3 Key down listener not working after returning to frame

I have two frames, one labeled "mainFrame" and another labeled "secondFrame". When the program starts, the user clicks a button called start and that sends the user to "secondFrame". Once the user is on second frame, the KeyCode of whatever key they have pressed is traced and if the key pressed has the KeyCode of 68, the program returns to "mainFrame". This works fine, the problem is then when the user clicks the button again and returns to the "secondFrame" at which point the KeyCode does not trace.
Here is the code on the mainFrame:
stop();
start.addEventListener(MouseEvent.CLICK, startGame);
function startGame(e:MouseEvent):void
{
gotoAndStop("secondFrame");
}
And the code on the secondFrame:
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownCheck);
function keyDownCheck(event:KeyboardEvent):void
{
trace(event.keyCode);
if(event.keyCode == 68)
{
gotoAndStop("mainFrame");
stage.removeEventListener(KeyboardEvent.KEY_DOWN, keyDownCheck);
}
}
I'm sure I'm doing something stupidly obvious that is preventing this from working, but I just can't figure out what.
Try to add this.setFocus() to the second frame's code.
I've found the answer! Well, Zavr and Vesper actually said it xD The focus was being set out of the application, so stage.focus() = this worked. I don't know what I did that caused the errors when I tried it the first time but it works now xD Thanks all!
Change second frame code to:
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownCheck);
function keyDownCheck(event:KeyboardEvent):void
{
trace(event.keyCode);
if(event.keyCode == 68)
{
stage.removeEventListener(KeyboardEvent.KEY_DOWN, keyDownCheck);
gotoAndStop("mainFrame");
}
}

Using QWER keys to jump to a specific scene in AS3

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.

Actionscript 3: Pausing a game

Hey so I'm working on pausing my game. I have it almost working, but there's a couple malfunctions that I can't seem to understand.
Basically I have some code (below) that pauses the frame rate and then resumes the frame rate. Both snippets of code work correctly individually, but when put together, if I press "p" it instantly cycles through both snippets of code, effectively only executing the second one (I put traces in and it shows that both codes are being executed when I press "p" once.)
So my first and primary question is how do I get this to work? I just want to execute one snippet of the code when "p" is pressed to pause the game, and then after that occurs, be able to press "p" again and execute the other snippet.
My second question is...why when I trace the frame rate does it say it is 0.01 instead of 0? Found this sort of interesting....Anyways, here's the code. Tell me if you need more context, but I don't think you will.
if (stage.frameRate == 30)
{
if (keyboardEvent.keyCode == Keyboard.P)
{
dispatchEvent(new NavigationEvent(NavigationEvent.PAUSEGAME));
stage.frameRate = 0;
checkIfPaused = true;
trace("pause game");
trace(stage.frameRate);
}
}
if (stage.frameRate == 0.01)
{
if (keyboardEvent.keyCode == Keyboard.P)
{
stage.frameRate = 30;
dispatchEvent(new NavigationEvent(NavigationEvent.RESUMEGAME));
checkIfPaused = false;
trace("resume game");
}
}
Nevermind, I figured it out. I just added else if instead of if for the second snippet and now it works =), but if anyone can answer my question about the frame rate being 0.01 in a nice fashion I'll give the answer to you

As3 Flash trying to hide a clip / frame

I have the following code:
stop();
stage.addEventListener(MouseEvent.RIGHT_CLICK, function(e:Event){});
login_btn.buttonMode = true; // show hand cursor
//adding event listener
login_btn.addEventListener(MouseEvent.CLICK, loginUser);
function loginUser(e:MouseEvent):void{
//if username & password are correct allow entry
if(IDTxt.text == "wixlab"){
gotoAndStop(2);
}
if(IDTxt.text == "hello"){
gotoAndStop(2);
}
if(IDTxt.text == "hello"){
gotoAndStop(2);
}else {
//otherwise show an error message
var alert:TextFormat = new TextFormat();
alert.color = 0xff0000; //red font
messageTxt.text = "Incorrect ID, Please try again.";
messageTxt.setTextFormat(alert);
}
}
and in the frame 2, i have stop();
Please i am trying to make it ones they login, the whole clip just go away so they can click on what is behind it. i am using Wix to add this SWF to hide a video. but ones they are logined you won't be able to click on the video because the swf is above it.
please help
If you move all this code to a layer just for scripting, at frame 1 (which you may already have) you could write a simple conditional that says something like if (this.currentFrame == 2){ getRidOfMyObjects(); } I'm not positive that syntax is correct, but you could put this right under your current conditional statement. And getRidOfMyObjects could be simple visibility commands or removeChild or whatever is best for your scene...Does this help?