Replay a timeline two times in as3 - actionscript-3

stop ()
Is there a function what will play a timeline two times and after it stops ?
from frame 1 to 15 I want it to play two times and stop after. I don't want to duplicate timeline.

No code needed:
make your animation a graphic
click on the graphic and under properties set
option: Loop
First: 2
and it will loop 2 times :-)

var i = 0; // put this on a blank first frame
// put this on the last frame
i++;
if(i==2){
stop();
}else{
gotoAndPlay(1); // that might be 0 not sure a bit rusty on flash
}
I'm not promising this will work because I haven't messed with flash in a few months but you can give it a try.

var loop:int = 2;
var counter:int = 0;
Put this to first frame and leave it empty, start animation from second frame.
if(counter == loop){
stop();
}
And copy this block to where you want animation to stop.
counter++;
gotoAndPlay(2);
Copy this to last frame

Put this code into the last frame. // we did not assign any value to the variable loopCount and use condition for assigning value in our variable loopCount. and second time first condition will not run and loopCount value will not change. then we are increasing loopCount value by 1 in every loop of animation. then just write condition to stop the movie by checking loopCount value.
//Put this code into the last frame or where you want to stop your movie after playing 2 times.
if(!loopCount){var loopCount:Number = 0;}
loopCount++;
if(loopCount >= 2){stop();}

Related

ActionScript 3 stage width

Im doing my first flash AS game, so need a little help.
I have only 1 thing on the stage, its ball (layer instance) which has anchor point in the middle. I'm trying to make this ball bounce off walls (i mean screen).
This instance name is called 'kugla1'
Heres my code (its second frame):
if(kugla1.x<=kugla1.width/2 || kugla1.x>=stage.stageWidth-kugla1.width/2)
speedX=-speedX;
if(kugla1.y<=kugla1.height/2 || kugla1.height>=stage.stageHeight-kugla1.height/2)
speedY=-speedY;
kugla1.x+=speedX;
kugla1.y+=speedY;
First frame is:
var speedX:int=5;
var speedY:int=5;
kugla1.x=100;
kugla1.y=100;
And third frame is only:
gotoAndPlay(2);
what am I doing wrong?
Thanks!
Your problem, is likely this line:
if(kugla1.y<=kugla1.height/2 || kugla1.height>=stage.stageHeight-kugla1.height/2)
In the second part (after the ||) you are comparing the height of kugla1 instead of the y position.
Another issue you could run into, is your ball could potentially meet the same condition for longer than one frame, so it would be best to separate your speed from the current direction of movement.
See code comments:
On your first frame, you'll need two additional variables:
var speedX:int=5;
var speedY:int=5;
var curSpeedX:Number = speedX;
var curSpeedY:Number = speedY;
on your second frame:
if(kugla1.x <= kugla1.width/2){
curSpeedX = speedX; //we need the positive value to make it go right
}
if(kugla1.x >= stage.stageWidth - kugla1.width/2){
curSpeedX = -speedX; //we need the negative value to make it go left
}
if(kugla1.y <= kugla1.height/2){
curSpeedY = speedY; //we need the positive value to make it go down
}
if(kugla1.y >= stage.stageHeight - kugla1.height/2){
curSpeedY = -speedY; //we need the negative value to make it go up
}
kugla1.x+= curSpeedX;
kugla1.y+= curSpeedY;

Flash Actionscript 3 Simple Counter

I am struggling to make a simple flash actionscript 3 counter in a text field, that starts at the beginning of my animation at 0, and increases with each frame.
This will count up until it reaches a particular frame on the timeline, where I would like the number to begin doubling instead of counting up.
I can only seem to find AS2 examples, and i'm not too sure how to get the double part working. Can anyone out there help me?
Thanks in advance!
I'm guessing you are using Flash CS and you are putting your code directly on the frames, so this snippet on the first frame should work.
Let's say you have a TextField on your stage with an instance name of counterTextField, and you want the count to start doubling up at frame 100.
import flash.events.Event;
var count:int = 0;
var limit:int = 100;
var increase:int = 1;
stage.addEventListener(Event.ENTER_FRAME, countFrames);
function countFrames(e:Event):void
{
if (count <= limit)
{
count += increase;
}
else
{
count *= 2;
}
counterTextField.text = String(count);
}

How can i make simply countdown in as3

I want to make simply countdown in actionscript 3. Please help me how can I make it simple.
Note Please show me it with example.
You can make countdown this this direction.
Firstly you must creating new textbox in actionscript its instance name must be "Timer"
And we are creating this actionscript codes
// Create the two variables.
var minute = 0;
var second = 0;
// Create the timer
// Checks the clock function every 1000 milisecond (1 second)
var timer:Timer = new Timer(1000);
timer.addEventListener(TimerEvent.TIMER, clock);
timer.start();
// Function that increments the timer
function clock(evt:TimerEvent):void {
// every time this function is checked increment second by one
second += 1;
// If the second is 59
if(second > 59){
// The minute will be plussed with 1
minute += 1;
//and the zero will be set to 00
second = 00;
}
// Displays the time in the textbox
Timer.text = String("Time is: ["+minute+":"+second+"]");
}
if you want to see this game example click here and see! The countdown usually using in game programing. you can see much more truck game example in this site. example you must park truck in 2 min.

move in move out tween animation for group of movieclips using as3

I have set of 6 movieclips as array_0 and another set of 6 movieclips as array_1. It is like two choices for different screens. Both these arrays are nested in another array as all_array. all 12 movieclips are positioned to a same x and y at initial load and that is outside the visible stage. I would like to use two different global variables for indexing. for example, cCat_Sel which ranges from 0-5 and another cScr_Sel ranges from 0-1. cCat_Sel will be changed on a click event of six buttons separate objects on stage (each button for each category).
so it will show the content for each category as per the value of cScr_Sel. if cScr_Sel is 0 then it will use all_array[0][cCat_Sel] to access the current target and similarly respective array for value of 1 as all_array[1][cCat_Sel]
I have done all the work including all tween animations to move current target and make it visible. But the tween does not bring the second set of mcs to visible area. I have two functions one for movein and one for move out by using tween animation for mc.x property. every relevant click event; I have to move current mc out and make alpha 0 and once that is finished, move in new current target and make alpha 1.
Somehow I have to combine these two tweens in one function. This is the part that I am stuck. or may be putting these mcs in two different arrays not a correct approach. I can easily achieve what I want on Enter Frame event of the root to check for cCat_Sel and cScr_Sel variables and do both animations one after the other but it seems like enter frame uses too much of cpu and makes it slower and probably not preferable.
willing to try anybody's suggestions or guidance. Thanks in advance.
I do not have any formal or informal programming education at all but I make things work by reading and trying out few things as per stackoverflow question and answers and sometime google. because most of my answers I have found from stack overflow.
Update:
function fnSlideInOut(cMc:Object, pMc:Object){
var HideX:Number =650;
var ShowX:Number = 0;
if(cMc != null){
if(cMc.x != ShowX){
//cMc.alpha = 1;
var SlideMcIn:Tween = new Tween(cMc, "x", Strong.easeOut, 650, ShowX, 0.5, true);
SlideMcIn.addEventListener(TweenEvent.MOTION_FINISH, fnSlideInFinish);
SlideMcIn.start();
}
}
if(pMc != null){
if(pMc.x != HideX){
//pMc.alpha = 1;
var SlideMcOut:Tween = new Tween(pMc, "x", Strong.easeOut, 0, HideX, 0.5, true);
SlideMcOut.addEventListener(TweenEvent.MOTION_FINISH, fnSlideOutFinish);
SlideMcOut.start();
}
}
function fnSlideOutFinish(e:TweenEvent){
//SlideMcOut.obj.alpha = 0;
SlideMcOut.removeEventListener(TweenEvent.MOTION_FINISH, fnSlideOutFinish);
}
function fnSlideInFinish(e:TweenEvent){
//SlideMcIn.obj.alpha = 1;
SlideMcIn.removeEventListener(TweenEvent.MOTION_FINISH, fnSlideInFinish);
}
}//End Function
fnSlideInOut(cScr_Sel, pScr_Sel);
I would like expert like you to comment on any kind of errors for the above code. It works 99 times but 1 time the movieclip either does not reach the destination or current and previous both targets showing and that too not where they are suppose to. This only happens when button click event happens in a quick succession. Thanks again
A option could be to use a third party library like TweenLite. It will then make it easy for you to run your second animation right after the first one is complete:
private function startAnimation():void
{
var mcToHide:MovieClip = all_array[cScr_Sel][cCat_Sel];
TweenLite.to(mcToHide, 1, {x: HIDDEN_X_POSITION, y:HIDDEN_Y_POSITION, alpha:0, onComplete:finishAnimation});
}
private function finishAnimation():void
{
var mcToShow:MovieClip = all_array[(cScr_Sel + 1) % 2][cCat_Sel];
TweenLite.to(mcToShow, 1, {x: VISIBLE_X_POSITION, y:VISIBLE_Y_POSITION, alpha:1});
}
You can then call startAnimation() on a relevant mouse click event and after having set cScr_Sel and cCat_Sel accordingly to your needs.

How can I specify ENTER_FRAME so that the object enters on every 4th frame?

So the ENTER_FRAME property will add an object to the stage on every frame the game runs. If the game is 24 fps, 24 objects created per second. How can I limit that so it will generate an object every 4 frames?
you can have a counter that increments every frame
var f:int = 0;
addEventListener(Event.ENTER_FRAME,onEnterFrame);
function onEnterFrame(e:Event):void{
if (f%4 == 0){
// do something
}
f++;
}
you can set f=0; inside the if statement if you like