I'm trying to get a movieclip to start at a specific frame, play 3 times and then stop. My movieclip is in the keyframe. I've tried a couple of different approaches and cannot limit the loop - it just keeps going.
First, I tried embedding this AS within the movieclip (mc_enlightened_nut_glow) on frame 1:
for (var i:Number=1; i<=3;i++){
this.gotoAndPlay(1)
}
this.stop();
And then I tried removing that AS and putting this in the final frame of the entire movie (frame 132) where I had a stop();
for (var i:Number=1; i<=3;i++){
mc_enlightened_nut_glow.gotoAndPlay(1)
}
stop();
In both cases, the clip just keeps playing. What am I doing wrong?
var loopCounter:int = 0;
last frame
if(loopCounter < 3){
gotoandPlay(1);
loopCounter ++;
}
You'll need a variable defined prior to the 'loop' of your MovieClip animation (eg, on frame 1 - var counter:int = 0) that gets incremented at frame 132. At that point, check if the variable is being incremented to a value greater than or equal to 3 and stop, if so. Otherwise gotoAndPlay(2).
This is all assuming you don't want to put your code in an external .as file - which is a far more flexible way of working.
On first frame:
var currentLoops:uint = 0;
var maxLoops:uint = 10; // desired loops
On second frame:
currentLoops++;
if (currentLoops == maxLoops) {
stop();
}
On final frame:
gotoAndPlay(2); // the frame with the second code (increasing current loops)
Rough, but should work properly.
You don't need any iterations. You will need code in frist frame, and last one (frame 132), and a variable for storing current loop:
As for code in first frame:
if(this.loopCount == undefined){
//Initiate our stuff
//Only 3 loops...
this.loopCount = 3;
//Code will be invoked only once
//Deside here, about your starting frame
//Start first time from the third frame
gotoAndPlay(3);
}
Code for last frame:
//We have visited last frame
if(--this.loopCount <= 0){
stop();
//No more loops!
}
Related
I'm making a simple flash shooting gallery animation with about 5 targets on screen but I am useless at action script.
I have the main scene and 5 Target movie clips in an array. I would like to...
-> Start Animation -> load random clip -> play random clip till end-> generate new random clip -> Repeat with Delay Offset....
So far I have the following:
function getRandomLabel():String {
var labels:Array = new Array("Tar1", "Tar2", "Tar3", "Tar4", "Tar5");
var index:Number = Math.floor(Math.random() * labels.length);
return labels[index];
}
this.gotoAndStop(getRandomLabel());
}
This is working... but I would like to add a delay and no repeat to this...
Ok, lets do it.
// If you need to avoid playing the same movie two times.
var lastLabel:*;
// The list of labels.
var Labels:Array = ["Tar1", "Tar2", "Tar3", "Tar4", "Tar5"];
function playRandom():*
{
do
{
// Get a random index.
var anIndex:int = Math.random() * Labels.length;
}
while (Labels[anIndex] == currentLabel);
// Keep the current label in the variable.
currentLabel = Labels[anIndex];
gotoAndStop(currentlabel);
}
function playNext():void
{
// 1000 milliseconds = 1 second delay.
setInterval(playRandom, 1000);
}
Then. At the end of each of your movie clips you need to correctly call the playNext method. If these movies are in the same timeline, as the code above, just call playNext(); If they are separate MovieClip objects, it will probably be (parent as MovieClip).playNext(); I cannot really tell because I don't know the structure of your movie. You will probably need to read the following to understand: http://help.adobe.com/en_US/as3/dev/WS5b3ccc516d4fbf351e63e3d118a9b90204-7e3e.html
I have made a program in which small green rectangles (or Greeny here) are periodically generated at the bottom of the screen and inside the movie clip symbol, I have made a classic tween such that it moves to the top of the screen. I have also declared a variable (t) that increments itself periodically (I am running it at 24 fps). When the value variable, t reaches or exceeds 96, it moves to the next frame. The problem however is that even in the next frame, the generation of these small green rectangles do not stop. Please do excuse me if I have asked the question wrongly. By the way the code on frame 2 is just stop();. Here is the code for frame 1-
var c:int;
var t:int = 0;
var s:int = 8;
function eFrame(event:Event):void
{
t++;
if (t%s == 0)
{
var i:Greeny = new Greeny ;
i.x = Math.random() * 550;
i.y = 400;
stage.addChild(i);
}
if (t > 96) {
nextFrame();
}
}
this.addEventListener(Event.ENTER_FRAME, eFrame);
stop();
EDIT - Here is the link for the file - http://www.mediafire.com/download/crjh2fubcbnx3l5/Retro.fla
you have to remove your your event listener on frame 2. Try this (on frame2 or in frame 1 when your condition t>96 is reached):
this.removeEventListener(Event.ENTER_FRAME, eFrame);
If you want to stop the generation of green rectangles, you should remove the listener of Event.Enter_Frame, or do something to stop it in function eFrame.
If you want to make all rectangles not visible, you should set theirs "visible" attribute false or remove them from stage .
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.
I'm trying to make a reversed play module in Action Script 3. I have a video, 200 frames long that I imported to Flash as a movie clip. I name the movie clip and inserted some key frames to make the video stop at specific frames, making it a 3 stage animation.
Whenever I swipe/pan to the right (detecting a positive x offset) it gives the command play();, the movie clip will play til it finds a stop.
What I want to achieve is to play it backwards from the current frame til the previous stop when I swipe to the left (detecting a negative offset).
I sorted out the swipe/touch programming and what I'm missing is the backwards bit. I've managed to make it work, going backwards 1 single frame, not the whole bunch that exist prior to hit the previous stop frame. My code for the swipe and play forward is this, with the single prev frame included, which gives me just one frame back instead of the whole set before the previous stop.
Multitouch.inputMode = MultitouchInputMode.GESTURE;
mymovieclip.stop();
mymovieclip.addEventListener(TransformGestureEvent.GESTURE_SWIPE , onSwipe);
function onSwipe (e:TransformGestureEvent):void{
if (e.offsetX == 1) {
//User swiped right
mymovieclip.play();
}
if (e.offsetX == -1) {
//User swiped left
mymovieclip.prevFrame();
}
}
You could try this:
import flash.events.Event;
import flash.display.MovieClip;
//note that this is not hoisted, it must appear before the call
MovieClip.prototype.playBackward = function():void {
if(this.currentFrame > 1) {
this.prevFrame();
this.addEventListener(Event.ENTER_FRAME, playBackwardHandler);
}
}
function playBackwardHandler(e:Event):void {
var mc:MovieClip = e.currentTarget as MovieClip;
if(mc.currentFrame > 1 && (!mc.currentFrameLabel || mc.currentFrameLabel.indexOf("stopFrame") == -1)) { //check whether the clip reached its beginning or the playhead is at a frame with a label that contains the string 'stopFrame'
mc.prevFrame();
}
else {
mc.removeEventListener(Event.ENTER_FRAME, playBackwardHandler);
}
}
var clip:MovieClip = backMc; //some clip on the stage
clip.gotoAndStop(100); //send it to frame 100
clip.playBackward(); //play it backwards
Now you can put 'stopFrame' label to the clip's timeline (stopFrame1, stopFrame2... stopFrameWhatever) and the clip should stop there until playBackward is called again. Note that you should remove the enter frame event listener if the clip hasn't reached a stopFrame or its beginning and you want to call play/stop from the MovieClip API, otherwise it may cause problems.
I have an animation that I want to loop three times and then finish the final frames and then stop. I have tried this code to start with:
var stopNum = 0;
function looper(loopLimit) {
if (stopNum>=loopLimit) {
stop();
} else {
gotoAndPlay(2);
}
this.stopNum++;
}
And this code to stop:
if (!loopCount) {
var loopCount:Number = 0;
}
loopCount++;
if (loopCount >= 3) {
this.stop();
}
I have remaining frames to play from this point and then the entire animation stops. The problem is the frames loop three times but include all the frames including the closing frames.
Try using some events available from Flash 10 like so:
Event.FRAME_CONSTRUCTED
and
Event.EXIT_FRAME
Throwing some idea though, e.g. If your animation ends, then use Event.EXIT_FRAME and keep counter which will tell you that how many times animation is played.
Off the top of my head, I'd use an event listener to check for when the playhead advances for the moiveclip (as mentioned by Rajneesh). Within the event listener, I'd check for what frame it is at, and if it is at the end of my 'end loop frame' and then I'd check if I need to loop it. If so, then I increment a counter to keep track of how many times I've looped, and tell the movieclip to go to the start frame and play again.
Once it's looped enough times, I'd just let it run until the last frame then, on which I'd stop the animation.
I'll take a guess and assume your movieclip has 100 frames, and you only want frame 1 to 90 to loop 2 times, then have it play 1 more time, but from frame 1 to 100. Plays a total of three times from 1 to 90, then once 91 to 100, before stopping.
import flash.display.MovieClip;
import flash.events.Event;
var clip:MovieClip = this.aCircle;
var timesPlayed:int = 0;
var timesToLoop:int = 3;
var frameToStartLoop:int = 1;
var frameToStopLoop:int = 90;
function enterFrameListener(inputEvent:Event):void {
if(clip.currentFrame == frameToStopLoop){
timesPlayed++;
if(timesPlayed < timesToLoop){
clip.gotoAndPlay(frameToStartLoop);
}
// if the currentFrame made it past the above condition, then
// it means there is no more looping needed so just play till the end and stop.
} else if(clip.currentFrame == clip.totalFrames){
clip.stop();
// can remove this listener now, as it is no longer needed
clip.removeEventListener(Event.ENTER_FRAME, enterFrameListener, false);
}
}
// use an event listener to listen for the ENTER_FRAME event, which is triggered everytime the movieclip advances a frame
// (as a side note, you'll also find that this event is triggerd even if there is only 1 frame, or even if the playhead is not actually moving, check the doc for details)
clip.addEventListener(Event.ENTER_FRAME, enterFrameListener, false, 0, true);