Be a Supporter!

AS2 Interval problems

  • 337 Views
  • 4 Replies
New Topic Respond to this Topic
StaliN98
StaliN98
  • Member since: Jul. 27, 2007
  • Offline.
Forum Stats
Member
Level 10
Blank Slate
AS2 Interval problems 2009-03-25 16:10:05 Reply

I have a custom class, that has a function I want to execute every 1/48 seconds. But in the function, all previously defined variables are forgotten. Here is what my code basically is:

class WaterSplash {
	
	// Interval
	private var interval:Number;
	
	// How long it lasts
	public var life:Number;
	
	// Size
	private var radius:Number
	
	// Movie Clip for reference
	public var mc:MovieClip;
	
	function WaterSplash(instName:String, position:Point, speedMult:Number, depth:Number) {
		
		interval = setInterval(fadeWater, 1000/48);
		
		life = (Math.random()*50)+50;
		
		radius = (Math.random()*25)+25;
		
		// Usless stuff defining mc
		
		trace(mc + " " + radius + " " + life); // Traces the movie clip location, and two random numbers
		
	}
	
	private function fadeWater() {
		
		trace(mc + " " + radius + " " + life); // Traces undefined undefined undefined
	
	}
}

Is there something else I need to do when making an interval?

Denvish
Denvish
  • Member since: Apr. 25, 2003
  • Offline.
Forum Stats
Member
Level 46
Blank Slate
Response to AS2 Interval problems 2009-03-25 16:43:47 Reply

You need to send those parameters to the interval function when you set it up, ie:

function WaterSplash(instName:String, position:Point, speedMult:Number, depth:Number) {
	life = (Math.random()*50)+50;
	radius = (Math.random()*25)+25;
	interval = setInterval(fadeWater, 1000/48, instName, radius, life);
}
function fadeWater(mc,rad,lif) {
	trace(mc + " " + rad + " " + lif);
}

- - Flash - Music - Images - -

BBS Signature
StaliN98
StaliN98
  • Member since: Jul. 27, 2007
  • Offline.
Forum Stats
Member
Level 10
Blank Slate
Response to AS2 Interval problems 2009-03-25 17:04:56 Reply

Ah, thanks. Working now.

StaliN98
StaliN98
  • Member since: Jul. 27, 2007
  • Offline.
Forum Stats
Member
Level 10
Blank Slate
Response to AS2 Interval problems 2009-03-25 17:14:01 Reply

I have a new problem that follows from this - life needs to decrease by 1 every time the function is called, but this won't affect the original variable, the number traced doesn't decrease:

private function fadeWater(lif:Number) {

	lif -= 1;
		
	trace(lif);

}

How can I do this?

StaliN98
StaliN98
  • Member since: Jul. 27, 2007
  • Offline.
Forum Stats
Member
Level 10
Blank Slate
Response to AS2 Interval problems 2009-03-25 17:25:18 Reply

Forget that, I worked it out myself (I passed the "this" as the parameter for the function).