• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 218
  • Last Modified:

How can I play a sound in a specified intervals?

I am working on a metronome in as3.  I would like to be able to have the user specify what interval they would like to hear the metronome tick.     So My idea is to start a counter when the swf loads and play the tick sound when ever the specified time elapsed on the counter.  So if the interval time was 2 seconds I would like to play the metronome tick every 2 seconds.  Here Is the code I have so far.

The condition in the if statement is what I need help with .  "x amount of seconds has progressed"  What can I use to check to see if a certain amount of time has passed to play the sound?  
var startTime:Number= 0;
stage.addEventListener(MouseEvent.CLICK, reset);
function reset(myevent:MouseEvent):void {
	startTime = getTimer();
}


stage.addEventListener(Event.ENTER_FRAME, showTime);
function showTime(myevent:Event):void {
	var currentTime:Number = getTimer();
	var elapsedTime:Number = (currentTime - startTime) / 1000;
	myDisplay_txt.text = elapsedTime.toString();
	if ( x amount of seconds has progressed){
		play sound();
	}
	
	
	
}

Open in new window

0
ForLoop5
Asked:
ForLoop5
  • 2
1 Solution
 
moagriusCommented:
change startTime to:

var startTime:Number=getTimer();

then elapsedTime will be the number of seconds that have elapsed.

so you could say

if(elapsedTime > 2)

means more than two seconds have elapsed since the app was loaded.

that said, it'd be a lot easier to just use a timer
function showTime(myevent:Event):void {
  play sound();
}
var timer:Timer = new Timer(2000); // set a timer to "tick" every 2 seconds
timer.addEventListener("timer", showTime); // add the handler
timer.start();

Open in new window

0
 
ForLoop5Author Commented:
Nice.  How can I set the "2000" to be a variable from an input text field?

var timer:Timer = new Timer(inputText);



0
 
ForLoop5Author Commented:
Thank you.  I figured out the second part.  Heres how I did it

var myNumber:Number = Number(beat.text);
var myTimer:Timer=new Timer(myNumber);
myTimer.start();
function movecar(myevent:TimerEvent):void {
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now