ActionScript 3 - Reseting timer every frame (Quiz) - actionscript-3

I have been attempting to create a simple quiz in which you chose an answer, play right / wrong animation, then let you choose another question. Each question frame gives you 30 seconds to choose an answer and then forwards you to the "time is up" frame. Pretty simple. So far, everything has been working smooth except the timer. I can get it to work within a single frame, but when u click a button or for some other reason get re-directed to another frame, it crashes the whole program.
I theorized I can fix that by stoping the timer at "exit frame event", however that only prevented the timer from even starting (while the rest of the program continued to work smoothly);
Posting the code may and WILL not be enough so check the file out for yourself:
http://www.filedropper.com/semifinalsq
I need this for a school quiz and I haven't been able to find anything on the internet for days. I may possibly have multiple errors or errors in the way I've 'graphically' constructed and linked the whole thing, in which case I cannot fix it without somebody taking a look at the whole program.
If you can make anything from the code of only 1 frame:
import flash.events.TimerEvent;
import flash.utils.Timer;
var myTimer:Timer = new Timer(1000);
myTimer.addEventListener(TimerEvent.TIMER, countdownHandler);
function countdownHandler(event:TimerEvent):void{
countdown.text = 30-myTimer.currentCount+" s";
if(myTimer.currentCount==30){gotoAndStop(5)}
}
This is my timer code, where frame 5 is the "time is up" frame. This bit works perfectly, but how do I build upon it so it resets whenever a new frame with a timer is entered? (So, how do I stop it and restart it in certain frames?);
Help would be much appreciated!

Solution:
myTimer.stop();
myTimer.removeEventListener(TimerEvent.TIMER, countdownHandler);
Added within the function called by the buttons. Since it is a quiz, the answers are buttons and lead to another frame, while previously stopping the timer and removing it's listener (code above goes before 'goto' frame and stop ... );
Although this may not help in other situations, for a quiz of similar structure it is a pretty simple solution.
Special thanks to user LDMS

Related

Actionscript-3 looping issue--Adobe Animate CC

My employer decided they wanted me to start doing animation with Adobe's new "Animate CC" application. My issue is that I don't know how to loop my animation outside of the Adobe Animate environment. I am new to Adobe Animate CC and ActionScript, unfortunately, so I will probably need a relatively basic answer to understand why my solution isn't working. From what I can tell, my ActionScript code is being ignored by the IDE completely.
In the IDE and in the browser test command, the animation plays beyond frame 100, to the end, and then flashes a frame of white before repeating. I need it to loop without this white frame interrupting the screen, whether that be through a loop or some other means that I'm just not aware of.
For context: my project has about 100 layers of content and I'm unfamiliar with how this program works. I've thoroughly searched the web for tutorials on how to do what I need to do, but I've come up empty handed.
I have an actions layer among my motion tweens and other layers
https://gyazo.com/6e0b8502d98b6c9903bb96ac3a939bae
I've been trying to use gotoAndPlay(0) at frame 100 to start the animation over from the beginning.
https://gyazo.com/704ee7158bae6dfd149b6283cfa33451
Basically, how do I use Action-Script in Adobe Animate CC in order to infinitely loop my animation until closed?
Thanks everyone.
Your flicker may be a result of having an extra blank keyframe on one of your layers.
Assuming that you don't have any additional scripts to stop your animation (e.g. stop()), the Timeline should loop automatically whether your animation is inside a MovieClip or on the main Timeline. You shouldn't have to put any script on your timeline or in a separate AS file to make an animation loop. I would suggest this method.
Additionally, although you have the code specifying that you want it to go the first frame, it will ignore your call because the timeline is still playing and therefore the priority. One way you can combat this is by adding a stop(); function and a delay timer that contains your gotoAndPlay(0) function. This will take focus away from playing the Timeline and will allow you to execute your script. I wouldn't suggest this method because it seems a bit redundant.
However, if you're curious one way that you could approach this is shown below, simply add this script to the frame you want the animation to restart at.
//Stop the Timeline
stop();
//Create a delay timer for 5 miliseconds that is executed once
var timer:Timer = new Timer(5,1);
//Add an event listener that calls once the timer is complete
timer.addEventListener(TimerEvent.TIMER_COMPLETE, timerHandler);
//Start the timer
timer.start();
//Timer handler that is called once the delay timer is complete
function timerHandler(event:TimerEvent){
//Go to and play the first frame
gotoAndPlay(0);
}

AS3 timer not triggered correctly second time

I am trying to create a simple gallery. I have created a gallery movieclip that contains 4 frames with pictures. The above code works fine for one trigger. The gallery mc moves to frame 2 and the second image is shown. The next trigger though (after 2 more seconds) doesn't work properly. It gets stuck (eg other buttons of the stage aren't clickable any more). If I trace the NextPhoto function, it outputs two times and nothing more. What am I missing and the second trigger is not working?
import flash.utils.Timer;
stop();
gallery1.stop();
var myTimer:Timer = new Timer(2000);
myTimer.addEventListener(TimerEvent.TIMER, NextPhoto);
myTimer.start();
function NextPhoto(TimerEvent):void
{
if (gallery1.currentFrame < gallery1.totalFrames){
gallery1.gotoAndStop(gallery1.currentFrame+1);
}
else if (gallery1.currentFrame == gallery1.totalFrames){
gallery1.gotoAndStop(1);
}
}
I am not quite able to get your code as I'm a Flex kind of guy. But what I am able to guess from your code is that your Timer is not being restarted after it has executed for the first time. You should restart the timer for every frame, until the last frame to have the NextPhoto(e:TimerEvent) executed.
Thanks for the answes guys, eventually it was simply a memory issue.
When I Alt+Entered the project it didn't work. When I published at Projector, it worked.
Seems CS6 lately consumes too much memory, or I should upgrade my tower...

Fast rollOver on a button causes another label to rollOut (MovieClip button)

I'm new to AS3/Flash and stackoverflow and have tried to browse through different threads with this issue.
My issue is that when I rollover too quickly on one of my buttons, the button will rollover to the "Click" state. I have a tester that debugs the line "hit! " and whenever that glitch happens, the tester does not show the line "hit" so I know that it isn't actually registering a user-input click.
Interestingly enough, the issue also only happens when I move from the bottom or top of the button to the other side vertically. Faster FPS does seem to minimize the effect but it's still there. I have tried to get rid of my hit area layer, thinking that it was the culprit to the problem somehow but even then it did not do anything.
I'll post the .fla in case anybody can figure this out, would truly appreciate it as it's been driving me nuts.
https://dl.dropboxusercontent.com/u/18672917/Main_Btn_7halp6.fla
Here's the code I used in case someone wants to figure it out solely from possible coding errors. (Also, better_mc.Hit._visible = false; doesn't work it seems)
import flash.events.MouseEvent;
stop();
better_mc.addEventListener(MouseEvent.ROLL_OVER, betterOver);
better_mc.addEventListener(MouseEvent.ROLL_OUT, betterOut);
better_mc.addEventListener(MouseEvent.CLICK, betterClick);
function betterOver(evt:MouseEvent):void{
better_mc.gotoAndPlay("Over");
}
function betterOut(evt:MouseEvent):void{
better_mc.gotoAndPlay(27- (better_mc.currentFrame-10));
}
function betterClick(event:MouseEvent):void {
better_mc.gotoAndPlay("Click");
}
better_mc.hitArea = better_mc.Hit;
better_mc.addEventListener(MouseEvent.MOUSE_DOWN, Hitbox);
function Hitbox (event:MouseEvent){
trace("hit! "+this.name);
better_mc.Hit._visible = false;
};
Ok, got it. this is what is happening
Your calculation on rollout is creating a problem
function betterOut(evt:MouseEvent):void{
**better_mc.gotoAndPlay(27- (better_mc.currentFrame-10));**
}
This expression sometimes returns frame number 28 which is ahead of your 'stop()' which is at frame 27 and so it goes on playing the whole click animation.
27- (better_mc.currentFrame-10)
Try the simple solution of adding 'stop()' before your click animation starts i.e. frame 31 in this case.
See if this sorts your issue.
Can not open your fla as i have CS5 so not much help on that
Not sure why you need both click and mousedown events, code seems fine apart from the gotoAndPlay(labelname) parts since no idea how the animations are added here
Just for the last part of your query
(Also, better_mc.Hit._visible = false; doesn't work it seems)
For AS3, property 'visible' is used and not '_visible' so it will be,
better_mc.Hit.**visible** = false;

AS3 save previous frame while going in next to do something

Is posible something like this: in frame1 I have some user interaction (drag drop graphics etc), when user click some button I open frame2 or frame3 where are fields for taking some input from user, then when user finish, I back him to frame1 but frame is not "reset"? I tried what described here, and I can even send data from frame 2 to frame 1 using global vars, but frame 1 is "reset" all user actions are clean, so is there a way in this situation to keep frame1 in memory or something?
Previous or next go to a frame if the frame is being initialized is natural. There's no way to save it. In other words, move the frame is The following concepts, removeChild(previous-stage), addChild(next-stage)
In my experience, the script code to control the frame, increasing complexity are highly undesirable. Maintenance is difficult and in the future, the readability of the code is very poor. Recommend to handle than that in a single frame. Something like a single frame of the object location, size, status, and stored as a variable because it can restore it, it's more reasonable to think that.
If you are working with Flash & AS3 together you can put the code:
gotoAndStop(3); // 3 is the number of the frame
If you are using:
gotoAndPlay(3); // it will begin to play the frames from 3 till the timeline finds a stop mark.
So, you'll need to put a stop on the frame you want.
e.g. stop();
check out this tutorial: http://www.youtube.com/watch?v=9N9vE7wjoc4

Sound plays multiple times at once

I'm having trouble with sound in Flash. I may have went about coding the wrong way, because most of my codes are on frames.
So, I have these two variables
var outsideDay:Sound = new daysong();
var outsideNight:Sound = new nightsong();
And I want to play these songs on a specific frame. However, the sounds play sporadically, like 50 times at once. I think it's because I have other codes that link to the frames with a Enter_Frame function. How can I get the sounds to loop and not play multiple times at once?
Have you dropped the sound anywhere on the timeline in any frame? If so remove that frame.
Also, if you have your code declared on a keyframe that does not have a stop(); call on it, likely it is hitting that frame over and over again, when it "enters" it. Try adding stop(); either at the beginning of your code or at an ending key-frame, wherever it makes most sense for your project.
After trying those two things, another method I have learned to love that may come in handy is:
flash.media.SoundMixer.stopAll();
This will stop all sounds so that whatever sounds you start to play after making this call will not have other previously started sounds to contend with.
This sound tutorial may also be of use to you.
Let us know how it goes, or if any of this stuff helped.