How to display the highest value from a random created list of numbers in AS3 - actionscript-3

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

Related

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

Changing the value of a Number Bug AS3

Hey everyone so I am having some trouble here. Been at it for an hour now and can't find a solution.
So I Have a movie clip named _Bunny added to the stage like so:
_Bunny = new mcBunny;
stage.addChild(_Bunny);
_Bunny.x = (stage.stageWidth / 2) - 225;
_Bunny.y = (stage.stageHeight / 2) - 330;
Now what this _Bunny does is move across the stage horizontally from right to left in a loop which i have set up like so in a Enter_Frame event listener:
private function bunnyView():void
{
_Bunny.x += nBunnySpeed;
if (_Bunny.x >=(stage.stageWidth / 2) + 215)
{
_Bunny.gotoAndStop("leftView");
nBunnySpeed--;
}
if (_Bunny.x <=(stage.stageWidth / 2) - 215)
{
_Bunny.gotoAndStop("rightView");
nBunnySpeed++;
}
}
It's speed is the nBunnySpeed which is equal to 5. Now I have another function that I am trying to change the value of the nBunnySpeed to say 20 whenever the nScore is equal to 1 like so:
private function updateDifficulty():void
{
if (nScore >= 1)
{
//Increase Speed
nBunnySpeed = 20;
}
but the bug that in which is produces is the Bunny shooting off to the right side of the screen which is the "+x" no matter what I do this always happens.
Can anyone see what I might be doing wrong? I don't understand why this is happening. Please help!
You need some kind of a flag that indicates that the difficulty has been already updated. Then, when you call updateDifficulty it first checks if there's a real need to update speed now, if there's none, it just returns. If yes, however, then you update your bunny's speed and set that flag so that the next time the function will not alter the bunny's speed.
var diffUpdated:Boolean=false;
private function updateDifficulty():void
{
if (diffUpdated) return; // here
if (nScore >= 1)
{
//Increase Speed
if (nBunnySpeed<0) nBunnySpeed=-20;
else nBunnySpeed = 20; // retain the direction of bunny's movement
}
diffUpdated=true;
}
Now, whenever you want your difficulty to be updated, you do diffUpdated=false; and voila, bunny's speed will be updated by this. For this, however, you will need more than two levels of speed, maybe one for 10 score, one for 50 and one for say 200.
What you do in this function
private function bunnyView():void
{
_Bunny.x += nBunnySpeed;
if (_Bunny.x >=(stage.stageWidth / 2) + 215)
{
_Bunny.gotoAndStop("leftView");
nBunnySpeed--;
}
if (_Bunny.x <=(stage.stageWidth / 2) - 215)
{
_Bunny.gotoAndStop("rightView");
nBunnySpeed++;
}
}
is check whether the _Bunny is offscreen or not. And if so, the nBunnySpeed will be nBunnySpeed - 1. But since BunnySpeed = 20, it will be 20 + 19 + 18 + 17, still going right. If you'd to turn it to BunnySpeed = -BunnySpeed, it will reverse immediately and go back.

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.

5 Minute Countdown With Buttons

I have to make a 5 minute countdown clock as an assignment. I was given the basis of a as3 and told to add 'Start' - 'Reset' - 'Stop' buttons. So far I have a respectable countdown going, but no way to control it. I'm new to as2 and flash so I hope this isn't one of those "If you know that, you should know your answer" situations. Anything i try to find on the web as far as tuts don't really help me so if anyone could just tell me if I'm on the right path or eve if it's possible to ass buttons to this code to do whats stated above. Thanks in adv :)
var secs = "0" + 0;
var mins = 5;
timerDisplay.text = mins + ":" + secs;
var timerInterval = setInterval(countDown,1000);
//DISPLAYS DYNAMIC TEXT
function countDown()
{
secs--;
if (secs < 0)
{
secs = 59;
mins--;
}
if (secs < 10)
{
var secs2 = "0" + secs;
}
else
{
var secs2 = secs;
}
if (mins == 0 && secs == 0)
{
clearInterval(timerInterval);//STOPS TIME # ZERO
}
timerDisplay.text = mins + ":" + secs2;
}
As this is an assignment, I won't give you any code, just some ideas to get you on your way to writing your own.
You may want to add two (possibly three) buttons to the stage. One button could be the reset button and another could be a button that trades off between being start and stop (depending on whether the timer is currently running).
The only code necessary for a reset button, is something that sets your min variable and your secs variable back to their original values (possibly plus 1 second because of your interval code). If you need help getting started with buttons clicks throwing functions check out the mc.onPress method.
The only code necessary for a stop button, is something that stops your interval counter from continuing to count. I believe you already have something doing that when "clear" your timer at zero.
The only code necessary for a start button, is something that restarts your interval counter. You do something of the sort when you first start your timerInterval.
This won't work if someone decides to click on the start button after you have finished the countdown or if someone decides to click the start button multiple times.
In the first case, the countdown will continue into negative numbers, so you may want to write an if statement that doesn't allow that to happen (inside of the start button function).
And in the second case, the countdown will get faster and faster each time the button is pressed. Creating a boolean that keeps track of whether the program is stopped could possibly help with that problem.
To clarify a statement I made above about the interval code: Your code will forever decrement every 1000 milliseconds. The text box is only ever being refreshed when you decrement, so if you try to reset with exactly 5 minutes and zero seconds, you will see the numbers jump to 4:59 and then keep decrementing. If you reset to 5 minutes and 1 second, the numbers will appear to jump to 5:00 and then decrement from there.
I hope this helps!
package
{
import flash.display.MovieClip;
import flash.events.Event;
import flash.utils.Timer;
import flash.events.TimerEvent;
public class Test extends MovieClip {
private var _timer:Timer = null;
private var _repeatCount:int = 0;
private var _totalMinutes:int = 5;
public function Test() : void {
addEventListener(Event.ADDED_TO_STAGE, _Init);
}
private function _Init(e:Event) : void {
_repeatCount = _totalMinutes * 60;
_timer = new Timer(1000, _repeatCount);
_timer.addEventListener(TimerEvent.TIMER, _OnTimerFired);
_timer.start();
}
private function _OnTimerFired(e:TimerEvent) : void {
var minRem:int = (_repeatCount - _timer.currentCount) / 60 ;
var secRem:int = (_repeatCount - _timer.currentCount) % 60;
trace(minRem + ":" + secRem);
}
}
}

NetStream.seek error Seeking videos

i try to do seek function in NetStream class using Action Script language , The seek is not work correctly . I read about KeyFrames in NetStream , what is the relationship between KeyFrames and seek ? is there other problem of using seek function by NetStream ?
onClick() Function to Seek;
private function onClick(event:MouseEvent):void
{
if (event.currentTarget is Group)
{
var myGroup:Group = event.currentTarget as Group;
if ( myGroup.mouseX >= 100)
{
mouseClickedXPos = myGroup.mouseX;
ns.inBufferSeek = true;
var seekTime:Number = (mouseClickedXPos-100) * (totalTime/(controlBarControls.width-100));
ns.seek(seekTime);
}
}
}
there is event for netStatus for net stream
ns.addEventListener(NetStatusEvent.NET_STATUS, onNetStatus);
private function onNetStatus(event:NetStatusEvent):void
{
if ( event.info == "NetStream.Play.StreamNotFound" )
legend.text = "Video file passed, not available!";
else if(event.info.code == "NetStream.Play.FileStructureInvalid")
legend.text = "Video file passed, FileStructureInvalid";
}
event.info.code is be NetStream.Seek.InvalidTime , the video will stop playing , sometime will seek for end of video , but i trace it , the ns.time() doesn't update to new value (seekTime)
I'm not entirely sure, but I think you got this line wrong:
var seekTime:Number = (mouseClickedXPos-100) * (totalTime/(controlBarControls.width-100));
Try this instead:
var seekTime:Number = ( ( mouseClickedXPos - 100 ) / ( controlBarControls.width - 100 ) ) * totalTime;
Basically, you were comparing two completely different quantities to form a ratio and multiply that by the mouse click position. Instead, divide that mouseX by the total width (giving the position of the click as a percentage of the total width) and multiply that by the time (giving you a percentage of the total time).
I'm not overly familiar with the way NetStream works (I've used it, just not extensively enough to know this off the top of my head), but if you pass a time greater than the totalTime, there has to be an error or it has to handle it by either not completing the command or by setting the value to 0 (which I personally think is what would happen. It's how I would do it, at any rate)