Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

creating a bar that moves a specified rate in seconds

Status
Not open for further replies.

myatia

Programmer
Nov 21, 2002
232
Hi,

I'm trying to create a breathing pacer in Flash. When the user is supposed to inhale, the bar goes up; when the user is supposed to exhale, the bar goes down. Eventually, the user should be able to set the number of breaths per minute. For now, I just want to get a function to make the bar move at a specified pace (I'll do the input stuff later). I know how to do this conceptually, but I don't know ActionScript and Flash very well. If anyone has any advice or code samples, I'd really appreciate it.

Thanks,

Misty
 
well you could simply build it... like if your movie is 12fps and you want the breath to last 3 secs then you make the bar at its lowest at frame 0 and at its highest at frame 36... then I believe you can change the fps with as...
 
I think I want the movement to stay smooth, regardless of the breath rate, so I want to leave the frame rate alone. With that in mind, this is what I have so far. It doesn't work yet, but if anyone could tell me if it's on the right track (or if it's completely off track), I'd appreciate it.

Code:
// I have a rectangle set up as a movie clip.  
// This is the ActionScript attached to it
onClipEvent(enterFrame) {
	
  var MinimumHeight = 5;
  var MaximumHeight = 250;
  var heightIntervalSize = MaximumHeight - MinimumHeight;
  var height = MinimumHeight;
	
   // Increment values
   // Increment the height heightIncrement pixels...
   var heightIncrement;
   // every timeIncrement milliseconds.
   var timeInterval = 25;
	
	// Time variables
	var breathsPerMinute = 7;
	var secondsPerBreath = 60 / breathsPerMinute;
	
	// Number of milliseconds for one breath
	var breathTime = 1000 * secondsPerBreath;
	
	// Number of milliseconds to inhale or exhale
	var inhaleTime = 0.5 * breathTime;
	var exhaleTime = 0.5 * breathTime;
	
	// Number of times we increment the pacer.
	var numOfIncrements = inhaleTime / timeIncrement;
	if(inhaleTime % 1 != 0) {
	  // Make sure it's in integer
	  numOfIncrements = (inhaleTime % 1) + 1;
	}
	
	// Number of pixels we increment the bar by every
	// timeIncrement milliseconds.
	heightIncrement = heightIntervalSize /  
                          numOfIncrements;
	
	startTime = getTimer();
	
        // Upward inhale motion
	for(var i=0; 
           (getTimer() - startTime)<inhaleTime; 
            i++) {
		
           if(
            (getTimer() - startTime) < timeInterval + 5) &&
	    (getTimer() - startTime) > timeInterval - 5)
	   ) 

	   {
	      height = height + heightIncrement;
	      setProperty(this, _height, height);	
	   }
	}
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top