AS3 timer not triggered correctly second time - actionscript-3

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...

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 On video end transfer to a new SWF?

I'm trying to make it so once my my video ends the player will be moved onto a new SWF.
Here's the code I created
import fl.video.VideoEvent;
stop();
BackgroundVid.addEventListener(Event.COMPLETE, Menu_Screen);
function Menu_Screen(event:MouseEvent):void
{
var Menu_Screen_Loader = new Loader();
Menu_Screen_Loader.load(new URLRequest("Menu_Screen.swf"));
addChild(Menu_Screen_Loader);
}
Can't figure out why it's not booting properly.
Ok, first of all fix the mistake in the event handler. You are saying that the Event expected is a MouseEvent, where as it will in fact be a plain old Event.
Then if it still doesn't work, you'll need to furnish a bit more info regarding what isn't happening. What do you mean by booting properly. Is your video playing?

ActionScript 3 - Reseting timer every frame (Quiz)

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

Timer Not Working AS3

I am using a countdown timer.
what the game should do:
my first scene is the countdown and once the countdown reaches 0, it goes to the second scene
the problem is that the countdown will not show up but it will go to the first scene after 15 seconds which is what the countdown is counting down to. However when i renswe only the first scene using ctrl+alt+enter, the countdown shows up. I think the problem is some other code which is interfering.
There is a lot of coding and possible sources of error thereofore I have uploaded my game to the internet. Please do not use! This is the work of Pranavan Kantharajah for a school assignment!
Download to my flash game (AS3):
https://www.dropbox.com/s/039lkek494k65m8/EndOfPart1%20copy.fla
just add and complete event to your timer and then try to reset your timer there just like this:
var TM:Timer = new Timer(1000,15);
TM.addEventListener(TimerEvent.TIMER, do_my_job);
function do_my_job(e:TimerEvent):void
{
//
}
TM.addEventListener(TimerEvent.TIMER_COMPLETE, do_this_at_the_end);
function do_this_at_the_end(e:TimerEvent):void
{
TM.reset();
}
TM.start();

As3 Preloader (Blank until 100%)

I have an FLA file with two frames. On the first frame I have nothing but a textfield and some code to do the preloading. After it is done preloading it does gotoAndStop(2)
On frame 1 I have:
stop();
stage.scaleMode = StageScaleMode.SHOW_ALL;
//Import the required assets
import flash.display.*;
//Stop the playhead while loading occurs
this.stop();
//Create a listener to call the loading function as the movie loads;
this.loaderInfo.addEventListener(ProgressEvent.PROGRESS, PL_LOADING);
this.loaderInfo.addEventListener(Event.COMPLETE, PL_FINISH);
function PL_LOADING(event:ProgressEvent):void
{
var pcent:Number = event.bytesLoaded / event.bytesTotal * 100;
//Display the % loaded in textfield
txt.text = int(pcent) + "%";
//If the movie is fully loaded, kick to the next frame on the main timeline
}
function PL_FINISH(event:Event):void
{
removeChild(txt);
gotoAndStop(2);
}
On frame 2 I have nothing except:
var game:Game = new Game();
addChild(game);
In my publisher settings I have export to frame 2.
My problem is that the preloader won't display until 100%. Does anyone know why?
P.S. For testing I put a large image file on the stage in frame 2 and the result is the same.
This normally happens if you haven't deselected "Export in frame 1" in each of the library symbols that you are exporting for ActionScript.
You'll need to make sure that you create reference to these objects (so that ActionScript can access them) by placing them onto the stage in frame 2 (out of sight).
What's happening is that all of the symbols are being loaded before the preloader itself has loaded.
If this isn't the issue, then please provide some code so I can better assess your issue.
Have you tried putting something static on frame one? Just because there is a preloader, that doesn't mean that your swf will be displaying at all...
I know that I had one swf once which simply took a minute to actually get the Flex preloader to even show up because of network latency. It could be that your swf isn't displaying until 90% of it has already loaded.
I´m dealing with similar problems, and there is something i found out: my antivirus contains a kind of "browser protection" feature, which sort of checks all files in advance before it allows the browser to actually display them. If this antivirus feature has not been installed on the system, the preloader works beautifully. I´ve checked some other web-sites with flash content, and they also behave "wrong" under these circumstances. Now, some people seem to have found a way to stop the antivirus from messing around, but i don´t know how they do it... not yet...