How can i make simply countdown in as3 - actionscript-3

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.

Related

How to Adjust Volume in an Audio loop?

How would someone change sound levels of a music playing in a loop? For example, I'm making a game and at a certain frame I want the music level (music.wav) to be decreased to half of its volume.
How could someone do this in AS3?
You are using the word "loop" in a confusing way. In programming, a loop usually refers to one of the "for" loops that looks like this:
for (var i:int = 0; i < 10; i++)
{
//do stuff 10 times
}
I surmise that this is not what you mean by loop, but rather that you would like a MovieClip or the main timeline to decrease the volume of a Sound object at the end of n frames. Or do you just mean the music itself is looping? Hopefully you see the value of asking well written questions. That being said..
Mind you, I haven't tried this, but according to my reference book (ActionScript 3.0 Cookbook by Lott, Schall & Peters) you need to use a SoundTransform object which specifies the volume at which you want the sound to be set. Try this:
var _sound:Sound = new Sound(music.wav); // creates a Sound object which has no internal volume control
var channel:SoundChannel = _sound.play(); // creates a SoundChannel which has a soundTransform property
var transform:SoundTransform = new SoundTransform(); // SoundTransform objects have a property called "volume". This is what you need to change volume.
Now in your loop (or on the frame event that you are using) do this:
transform.volume *= 0.9; // or whatever factor you want to have it decrease
//transform.volume /= 1.1; // or this if you prefer.
channel.soundTransform = transform; //
So anytime you want the volume to decrease by this incremental amount, run this bit of code. Of course, you need to make sure that any variables you set are accessible in the code that is referencing them. One way that comes to mind to do this is with a function.
private function soundDiminish(st:SoundTransform, c:SoundChannel, factor:Number = 0.9):void
{
st.volume *= factor;
c.soundTransform = st;
}
Now, whenever you want to diminish the volume just call the soundDiminish function.
Maybe your frame event looks like this:
function onLoadFrame(fe:Event):void
{
soundDiminish(transform, channel); // 3rd parameter optional
}
If you only want this function to be called every 20 frames then:
function onLoadFrame(fe:Event):void
{
// this is a counter that will count up each time this frame event happens
frameCount ++;
if (frameCount >= 20)
{
soundDiminish(transform, channel); // 3rd parameter optional
frameCount = 0; // reset the frame counter
}
}

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 to display the highest value from a random created list of numbers in AS3

I m building kind of a game in Flash (AS3) where sound input (microphone) trigger a equalizer (the micLevel.height controls the height of a mask (showing the equalizer). The activityLevel of the microphone gives me a number ( 0 - 100 ) wich is displayed in textfield prosent. The contestant shout in the microphone and try to reach 100 ( I use the mc.gain to make it hard to reach 100 ). So far so good! I m pretty new to AS3 so I feel kind of lost.
I need to display the highest number they manage to reach and I want to have a time limit on this. The highest sound level in lets say 5 seconds.
Here is the code so far:
var mic:Microphone = Microphone.getMicrophone();
Security.showSettings("privacy");
mic.setLoopBack(true);
if(mic != null)
{
mic.setUseEchoSuppression(true);
stage.addEventListener(Event.ENTER_FRAME, showLevel);
}
function showLevel(e:Event)
{
micLevel.height = mic.activityLevel * 6;
//mic.gain = 1;
//trace(mic.activityLevel);
prosent.text = "Activity: " + String(mic.activityLevel) + "%";
}
I just need some code that grab the highest number from the text field "prosent" (with a time limit) and display it in a new text field.
Im sorry if I m unclear, but if anyone could help me out I would be very happy!
Br Harald
Just create a variable that will be updated whenever the micLevel.height value is higher than it, e.g.
var highest:Number = 0;
function showLevel(e:Event):void
{
if(micLevel.height > highest)
{
// The mic level was higher than the previous highest level.
highest = micLevel.height;
// Change your other text field to show the value of 'highest'.
// ..
}
prosent.text = "Activity: " + String(mic.activityLevel) + "%";
}
Start a Timer to reset your highestLevel var. This will show you then the highest level every 5 seconds. You could add a timer start-stop button too if you wanted to keep one highest always showing.
Then in your showLevel function, use Math.max() to get the highest number (between current activity and recent highest)
var highestLevel:Number = 0;
var timer:Timer = new Timer(5000); // fires every 5 seconds
function initLevels(e:TimerEvent){
timer.stop(); // you could stop your timer here automatically and then use a button to start again
highestLevel = 0; // when timer fires restart your highestLevels var
}
function showLevel(e:Event) {
highestLevel = Math.max(mic.activityLevel * 6, highestLevel);
prosent.text = "Activity: " + String(highestLevel) + "%";
}
timer.start();
timer.addEventListener(TimerEvent.TIMER, initLevels);
addEventListener(Event.ENTER_FRAME, showLevel);

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

Replay a timeline two times in as3

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();}