How to loop an animation or digital ad - actionscript-3

I am doing this digital ad and wanting to make it play more then 3 times, not sure on what code to write to make it go infinite time.
Right now the code is set up for playing 3 times then stopping.

Frame One:
if(!repeatCounter){
var repeatCounter:int=0;
}
if(repeatCounter<desiredAnimationRepetition){
play();
}else{
stop();
}
Final Frame:
repeatCounter++;
gotoAndPlay(1);
And that's all there is to it.

Related

How to play animation and then play reverse on hover, start playing again until end on hover out using in Adobe Animate (Flash)?

Sorry this is so specific but I have combed through so many pages and videos and tutorials and can't figure this out.
I have all of my animations within a MovieClip. In the movie clip is also a stage sized white square button with the instance name "btn". Back on the main stage I have a second layer called "actions" with the following code applied to the first (and only) frame. It's not working. At all. (HUGE) tia
stop(); // this will stop the movie from playing at the start
btn.addEventListener((MouseEvent.ROLL_OVER, playMovie);
btn.addEventListener((MouseEvent.ROLL_OUT, stopMovie);
function playMovie(evt:MouseEvent):void {
play();
}
function stopMovie(evt:MouseEvent):void {
stop();
}
The problem is when you say play(); or stop(); which object are you really commanding? Your playMovie function could be in theory used to control many MovieClips at once, in different ways, so be specific with your commands...
btn.play(); //start the playback of "btn" MC
btn.stop(); //stop the playback of "btn" MC
Also consider using MOUSE_OVER/OUT instead ROLL_OVER/OUT etc but whatever works for you.
For reversing you will use btn.prevFrame(); together with an ENTER_FRAME event function. This function reads your Document settings for the FPS. For example, if you set 30 frames-per-sec then whatever instructions you put inside the event function will be processed 30 times per second.
See this other Answer for advice about reversing the playback of a MovieClip.
#VC.One is correct in how you should implement a solve to your issue, however in response to your comment on their answer, I thought I would demonstrate how to implement this fully for you - incase they don't.
var removeUpdate = false;
btn.addEventListener(MouseEvent.MOUSE_OVER, playMovie);
btn.addEventListener(MouseEvent.MOUSE_OUT, stopMovie);
function playMovie(evt:MouseEvent):void {
// Stop rewinding the movie clip and play it
if(removeUpdate){
stage.removeEventListener(Event.ENTER_FRAME, update);
removeUpdate = false;
}
// play our button
btn.play();
}
function stopMovie(evt:MouseEvent):void {
// stop our button
btn.stop();
// ... and rewind it
stage.addEventListener(Event.ENTER_FRAME, update);
removeUpdate = true;
}
function update(evt: Event){
// moves the button movie clip backwards one frame.
btn.prevFrame();
// If we have finished rewinding the movie clip, then stop
if(btn.currentFrame == 1){
stage.removeEventListener(Event.ENTER_FRAME, update);
removeUpdate = false;
}
}
It is important that you remove the update event because if you don't, the movie will never play again, because it will go one frame forward and then back again every frame due to; btn.play(); btn.prevFrame();

Flash Action Script: how to have a common delay(ms) for a frame by frame animation in the timeline using AS3?

I am working with Action Script 3 for the first time. I am trying to animation few frames, i want to give a delay to each frame. I am currently using the below script on each frame for 20-frames in total.
stop();
setTimeout(function() { nextFrame(); }, 100);
here if i want to increase/decrease the delay i have to change the value in every single frame. I am pretty sure i am not doing the smart way. please help me out. Thanks in Advance Experts.
While the easiest solution would be to just adjust your frame rate to 10fps (the equivalent of 100ms between slides), there are reasons that may not be appropriate (animations inside the timeline etc).
Perhaps a Timer would be better.
Something like this:
import flash.utils.Timer;
import flash.events.TimerEvent;
//create a timer
var timer:Timer = new Timer(100); //fire every 100ms
//listen for it's tick, and run the timerTick function every interval
timer.addEventListener(TimerEvent.TIMER, timerTick);
//start the timer
timer.start();
function timerTick(e:Event):void {
//if the next frame is the last frame, stop the timer
if(this.currentFrame == this.totalFrames - 1){
timer.stop();
}
//go to the next frame
nextFrame();
}
On your individual frames, you can tweak the timer's delay (how long before ticking) or stop the timer all together. This can be helpful if you wanted to say pause for user interaction at some point, or make the delay longer/shorter for certain frames.
timer.delay = 200;
or
timer.stop();

AS3 - Make the screen flash for half a second

What I want to do is:
After colliding with an [object], I want the screen to flash for about half of a second. I have tried for loops and while loops but they seem to not work. I have no idea how I should program this.
I've been trying to figure out how to do this since I'v been making the game so it would be helpful if someone could help me.
Thank you for reading.
You need to use something that involves time. loops all run in a thread which doesn't pause for time - which is why they don't work.
Here is how you could do this with an AS3 Timer (let's say this code runs right after you've determined there's been a collision)
function flashScreen():void {
var timer:Timer = new Timer(50, 10); //run the timer every 50 milliseconds, 10 times (eg the whole timer will run for half a second giving you a tick 10 times)
var flash:Shape = new Shape(); //a white rectangle to cover the whole screen.
flash.graphics.beginFill(0xFFFFFF);
flash.graphics.drawRect(0,0,stage.stageWidth,stage.stageHeight);
flash.visible = false;
stage.addChild(flash);
timer.addEventListener(TimerEvent.TIMER, function(e:TimerEvent):void {
//we've told AS3 to run this every 50 milliseconds
flash.visible = !flash.visible; //toggle visibility
//if(Timer(e.currentTarget).currentCount % 2 == 0){ } //or you could use this as a fancy way to do something every other tick
});
timer.addEventListener(TimerEvent.TIMER_COMPLETE, function(e:TimerEvent):void {
//the timer has run 10 times, let's stop this flashing madness.
stage.removeChild(flash);
});
timer.start();
}
Other ways you can do this are with setInterval, setTimeout, a Tweening library,and an ENTER_FRAME event handler.

Play MovieClip frame by frame in addition after same function is executed several times

I have a MovieClip named fails_mc, it consists of 3 soccer balls and in my game every time my player fails I need to add an X mark over 1 ball, I have a hitTestObject detecting when the ball hits the goal area, but when the ball goes outside I need to play 1 frame inside the fails_mc and add the X mark to a ball ( I have 3 different png files inside fails_mc, 1 on each frame), this is the code I’m using but I don’t know how to play frame by frame the fails_mc (keep in mind that the same function will be used every time, that’s why each frame must be added to the last played frame:
if (ball.hitTestObject(goalie))
{
goal_mc.play();
net_mc.play();
}
else
{
fails_mc.play(+=1); // This is not working
trace("It’s a fail");
}
After 3 fails I must trigger another function that will finish the game but I will figure out how to do that later on.
I think all your looking for is the nextFrame() function of a movieClip.
you could also use gotoAndStop("x1") - where "x1" is the frame label (or number if not in quotes) you want the movieClip to goto. You could use a variable to track the current state.
var misses:int = 0;
if (ball.hitTestObject(goalie))
{
goal_mc.play();
net_mc.play();
}
else
{
misses++;
trace("It’s a fail");
if(misses > 3){
//do your game over stuff
}else{
fails_mc.gotoAndStop(misses)
}
}

Action Script 2 Suggestion

I am doing a project which is about interacting, mouse on screen, Hence, I want to do it by using Adobe Flash Pro and Action Script2.
here is the Task:
- a simple animations (for example: that full of spiders walking/jumping from up to down on the screen)
- once the mouse moves, the animation will Reverse (all spiders animation will reverse back and hide back to the top of screen from the place that the came).
- if the mouse doesn't move after 60 Seconds the animation will happen again till the mouse move again on the screen (Those spiders animation will happen again).
i have created a Animation Spider "Movie clip" (spider animation going down)
1- what script i should write to make animation reverse?
2- how can i tell Adobe my mouse is moved or it's not moving, so the spiders animation will happen or reverse?
by the way, I am a very beginner in action script 2.0
i appreciate any suggestion and help *is fine for me to do it in action script 3 too
Thanks.
Oh my. AS2 :)
OK first off I think it would be easier if you would create 2 different animations. One for the Spider to walk down. The other one to walk up. It is possible to reverse a MovieClip but I think if you`re a beginner, stick with the basics.
You need 3 thinks here.
1) Spider Clips. Going down and up.
2) An interval (timer in AS3)
var interval:Number = setInterval(spidersComeOut, 60 * 1000);
3) Mouse move listener
root.onMouseMove = function()
{
//swap your spider clips
//move the spider up again
//reset the interval with clearInterval(interval) and restart it again.
}
This is a very basic handler for mouse move.
Hope this will help you a little bit. This is not a complete solution. It will not work out of the box.
One thing at the end. If you are new to AS2 I would recomend to give as3 a shot. It is more difficult to start with, but there are more people out there willing to help with an AS3 problem then with as2.
Since you said you were ready to use AS3 here is solution.
I assume you have a separate movie clip containing your spiders animation which you place on the main timeline/stage.
1.place your MovieClip on the stage and give it the instance name of 'spiders'.
2.inside this MovieClip, on the first frame put this code (it will handle revesing the animation)
import flash.events.Event;
stop();
var _dir:int = 1;
addEventListener(Event.ENTER_FRAME, onEF);
function onEF(e:Event):void
{
getNextAnimationFrame();
}
function getNextAnimationFrame():void
{
var frameNum:int = currentFrame + _dir;
if (frameNum < 1 || frameNum > totalFrames)
{
removeEventListener(Event.ENTER_FRAME, onEF);
}
frameNum = Math.max(1, Math.min(totalFrames,frameNum));
gotoAndStop(frameNum);
}
function changeDirection($dir:int):void
{
_dir = $dir;
removeEventListener(Event.ENTER_FRAME, onEF);
addEventListener(Event.ENTER_FRAME, onEF);
}
3.on the main timeline (in the first frame) put this code:
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.events.MouseEvent;
var timer:Timer = new Timer(60 * 1000, 1);
timer.start();
timer.addEventListener(TimerEvent.TIMER_COMPLETE, onTime);
stage.addEventListener(MouseEvent.MOUSE_MOVE, onMove);
function onTime(e:TimerEvent):void
{
trace("it's time");
spiders.changeDirection(1);
}
function onMove(e:MouseEvent):void
{
timer.reset();
timer.start();
spiders.changeDirection(-1);
}
It would be possible to handle mouse, timer and animation in one piece of code, however the way it's build now is kind of OOP, if at some point you decided to build it properly it would be extremely easy to 'rewrite' this code as separate classes.