Flash AS3 loop 3 times and stop on specific frame - actionscript-3

I've designed a simple web banner that I'd like to loop 3 times.
In my timeline, I've created a blank layer for my ActionScript. The first frame contains the following ActionScript:
var loops:int;
The final frame contains the following ActionScript:
loops = loops + 1;
if (loops == 3) {
stop();
}
This loops the animation 3 times. On the final loop, I'd like the animation to stop on a certain frame. This is because the animation usually fades out to loop again. Is this possible?

Insert the second code to the certain frame.

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: How to jump to another scene (or frame sequence) and return back to frame having jumped from

Right now I'm building a simulation of a sports match, played in 2 linear halves, divided in 2 separate scenes. Goals that have been scored in-match appear as a button on the stage as scoring occurs. The button works as a reference to replay the goal. Goals scored in let's say the first half (scene 1) should be accessible to replay whilst in the second half (scene 2).
I've created extra scenes per goal that serve as individual replays to refer to.
What I would like to have the user be able to, is to click that replay button at any chosen time, view the replay, and then return to the exact frame it originally jumped from, and play on from there when preferred.
The reason why I 'cannot' use movieClips and addChild methods, is that I want the final SWF to keep its transparent background, as I will embed it in html later on.
If (and how?) I use the addChild method, you see 2 partially transparent movieClips on top of each other, running at the same time.
If I'd find out how to replace the one with the other, this could be suited method.
Another would be using currentFrame and/or currentLabel methods I guess but I wouldn't know how to.
I have the project .fla file to give you a clearer insight on things.
Let's assume you have a replay button with the instance name of replayBtn. Here is one way to set this up:
On the first frame of your timeline, create the following vars and functions:
//define a variable to hold the last replay frame
var lastReplayFrame:int = 0;
//define a variable to hold the frame to go back to once a replay is done
var replayReturnFrame:int = 0;
//create a function to call after a replay is done
function replayReturn(){
//if we've set a frame to jump back to
if(replayReturnFrame > 0){
//go there and resume playing
this.gotoAndPlay(replayReturnFrame);
//reset the return frame so we don't go back there the next time if the button isn't hit
replayReturnFrame = 0;
}
}
//create the click handler for the replay button:
function replayClick(e:Event):void {
//check if there is a replay available
if(lastReplayFrame > 0){
//store the current frame before jumping back
replayReturnFrame = this.currentFrame;
gotoAndPlay(lastReplayFrame);
}
}
//add the click event listener to the replay button instance
replayBtn.addEventListener(MouseEvent.CLICK, replayClick);
Now, in your timeline, whenever a goal starts (eg the first frame of where a replay should start), put the following code on a keyframe:
lastReplayFrame = this.currentFrame;
Then, on the last frame of a replay, put this code on a keyframe:
replayReturn();

How to make a movieclip play on a specific frame in AS3?

I'm incredibly rusty at Flash having not touched it in probably 10 years and can't seem to figure this out, or find it online:
I have a MovieClip with two layers, each having a Shape Tween. Basically its a Door that opens and closes.
I dropped it onto the main timeline but now I need it to start and stop. This is where I'm now struggling since the last time I used Flash actions could go on specific keyframes.
I made a new layer called actions just to keep things organized and currently have:
barrier1.stop();
I just want something that lets me state a frame, say 57 to have barrier1 start playing on. Tried using play(); and Event.ENTER_FRAME with no luck. How would I set this up?
Well it is easy with the instance name of your movieClip
barrier1.stop(); // Stops the movieClip
barrier1.play(); // Resumes
barrier1.gotoAndStop(12) // Goes to 12nd frame and stop
barrier1.gotoAndPlay(12) // Goes to 12nd frame and play
barrier1.currentFrame // returns barrier currentframe
For capturing frame from scene level:
this.addEventListener(Event.ENTER_FRAME,onLoop);
function onLoop(event:Event){
if(barrier1.currentFrame == 57){
trace("BARRIER is in 57. frame");
}
}
Inside on the animation clip on the first frame
var root:MovieClip = this.parent as MovieClip
root.makeStartSceneAnimation()
**in timeline scene level [root]**
function makeStartSceneAnimation(){
/// barrier started to play
}
If you are using timeline, you can add Key frame on the desired frame, and then add stop(); as Action in the action layer. But bear in mind that if you do this in the main timeline - it will stop everything. If you want to stop that MovieClip, then you have to do this inside MoviceClip's timeline.

How to save data in previous frame in Flash

I have some movieclips called one, two, three, four, five that appear on stage with addChild. I also have 4 editable text fields on stage called theText1, theText2, theText3 and theText4 where users must write the numbers 4, 1, 5, 2 in each one. If they write something wrong they are sent in a next frame where they take some feedback and come back in current frame to correct their answer. The problem is that when they come back all are "reset". Movie clips added before and numbers written before must be on stage. How can i do that? (I need something simple because i am new to flash and as3).
Because MovieClips don't store frame states.
You should store values, and restore them, after returning to the frame.
//Add these 2 helper functions in your frame with Quiz UI
function restoreValues():void{
if(this.storedValues != null){
//Restore data
theText1.text = this.storedValues.txt1;
}
}
function storeValues():void{
//Save as much data as you want
this.storedValues = {
txt1 : theText1.text
};
}
When you go to the another frame, call storeValues right before you change frame and store all necessary data in simple object.
checkQuizButton.addEventListener(MouseEvent.CLICK, onCheck);
function onCheck(e: MouseEvent):void{
storeValues();
nextFrame();
}
In the frame with Quiz ui components, just after initialisation of all ui components, call restoreValues(); Don't forget to change functions for your needs.

Flash Actionscript 3.0 Loading screen

I'm trying to get a loading screen to work in Flash. This is how my project is set up:
All of the game occurs in "Layer 1," which is set up into many different scenes: "Level 0," "Level 1," etc. Its code is run in a ".as" file
I tried implementing a simple loading screen (with a progress bar) in a new layer, "Preloader." Its code is run in the layer's "Actions."
I realize that putting the Preloader's code in its "Actions" wasn't the best idea because I had Layer 1's ".as" file load Level 0 at first. So the "Preloader" and "Layer 1" layers tried to run at the same time. This caused problems.
Now I have tried putting the Preloader into a scene of its own. That is not working.
Here is the code I've tried using for the Preloader - "scene" version:
// This function loads the Preloader
public function loadPL(event:Event) {
// Load the Scene associated with the Preloader
this.gotoAndStop(1, "PL");
// Prevent the MovieClip (game) from playing right away
stop();
// Add an EventListener that calls the 'loading()' function
this.addEventListener(Event.ENTER_FRAME, loadingPL);
} // End of 'loadPL()' method
// 'loading()' function
// This function calculates how much of the game has been loaded vs. how much data
// the game contains. The loading progress bar is resized accordingly.
public function loadingPL(e:Event):void{
// How much data does the game have in all?
var totalData:Number = this.stage.loaderInfo.bytesTotal;
// How much data has been loaded so far?
var loadedData:Number = this.stage.loaderInfo.bytesLoaded;
// Scale the 'plBarIns' according to the loadedData:totalData ratio
plBarIns.scaleX = loadedData/totalData;
// If the 'loadedData' == 'totalData' (all of the game's data has been loaded), allow
// the game to play
if (loadedData == totalData) {
play();
// Remove the EventListener that calls the 'loading()' function. It's not needed now
this.removeEventListener(Event.ENTER_FRAME, loadingPL);
}
}
Could anyone help me?
Thanks,
Christian
You need to put your preloader in frame 1 and have the rest of your project start on frame 2. After that you need to setup your ActionScript settings so it knows to load all of your classes on frame 2 instead of frame 1.
Here's what settings you need to change:
File > ActionScript Settings...
Change "Export classes in frame:" to 2
Change "Default linkage:" to Merged into code
Your loaderinfo should now return the proper progress of the file loading instead of instantly jumping to completed.