00:00
00:00
Newgrounds Background Image Theme

Kinoki just joined the crew!

We need you on the team, too.

Support Newgrounds and get tons of perks for just $2.99!

Create a Free Account and then..

Become a Supporter!

Timer Not Updating

561 Views | 6 Replies
New Topic Respond to this Topic

Timer Not Updating 2012-04-21 11:54:08


So I have the following Timer code

var platformTimer:Timer = new Timer(5000 / speed,0);
	platformTimer.addEventListener(TimerEvent.TIMER, addPlatform);
	function addPlatform(Event:TimerEvent):void {
		generatePlatform();
		trace("Platform Generated " + platformTimer.delay + " - " + platformTimer.repeatCount)
	}
	platformTimer.start();

The speed has a starting value of 3, but the value is constantly rising as time goes on. Therefore,

var platformTimer:Timer = new Timer(5000 / speed,0);

should start out as 5 / 3 and advance to 5 / 5 or 5 / 10
However, this doesn't happen. The trace

trace("Platform Generated " + platformTimer.delay + " - " + platformTimer.repeatCount)

always returns Platform Generated 1666.6666666666667 - 0
The speed is updated according to this bit of code

stage.addEventListener(Event.ENTER_FRAME, scoreCalc, false, 0, false);
	function scoreCalc(event:Event) {
		if (scrolling) {
			score++;
		}
		speed = 3 + (score/1000);
		Score.text = String(score);
		return speed;
	}

Any help?


BBS Signature

Response to Timer Not Updating 2012-04-21 12:57:27


The timer interval isn't counted as 5000 / speed, so increasing speed doesn't make any difference to the timer interval. You need to start a new timer in order to get it ticking according to the new speed.


You can solve pretty much any problem you may have with AS3 by consulting the AS3 Language reference.

Response to Timer Not Updating 2012-04-21 13:07:40


This sounds a lot like an XY Problem.
What precisely are you attempting to do with the timer?

Response to Timer Not Updating 2012-04-21 13:14:23


If you only knew how functions worked you wouldn't have a problem.

When you create the a new instance of an object and pass in arguments, you're invoking the constructor of the class. The constructor in the Timer class receives the "delay" value and sets it once. To make a DisplayObject move faster, you can alter the amount of pixels moved per timer tick, or if you really want to, set the "delay" value on each tick.

function onTick(e:TimerEvent):void
{
	platformTimer.delay = 5000 / speed;
}

Check out the Flash RPG I made in 2024. It takes about 25 minutes to complete.

BBS Signature

Response to Timer Not Updating 2012-04-21 14:08:53


At 4/21/12 01:14 PM, Jin wrote: If you only knew how functions worked you wouldn't have a problem.

When you create the a new instance of an object and pass in arguments, you're invoking the constructor of the class. The constructor in the Timer class receives the "delay" value and sets it once. To make a DisplayObject move faster, you can alter the amount of pixels moved per timer tick, or if you really want to, set the "delay" value on each tick.

function onTick(e:TimerEvent):void
{
platformTimer.delay = 5000 / speed;
}

Ok. That's really all I needed. I had identified the problem and was just trying to figure out how to solve it.


BBS Signature

Response to Timer Not Updating 2012-04-21 14:10:08


At 4/21/12 01:07 PM, Diki wrote: This sounds a lot like an XY Problem.
What precisely are you attempting to do with the timer?

The timer sets the pace of the platform's scroll. In effect, as the platforms scroll faster, the rate at which they are produced has to be reduced to keep the distance between them stable. As suck, I am dividing the Timer's delay by the speed.


BBS Signature

Response to Timer Not Updating 2012-04-21 18:49:29


At 4/21/12 02:10 PM, Noamyoungerm wrote: The timer sets the pace of the platform's scroll. In effect, as the platforms scroll faster, the rate at which they are produced has to be reduced to keep the distance between them stable. As suck, I am dividing the Timer's delay by the speed.

Personally if I were to do this, I would make some generic platform class that all platforms would extend, allowing me to reuse platforms that are no longer necessary.
Then using a delta time of some sort determine the rate at which they should be generated.

Something kind of like this:

class MyPlatformManager
{
	private var __mRequiredTime:uint = 100;
	private var __mTimeSinceLastMake:uint = 0;
	
	public function update():void
	{
		var diff:uint = (getTimer() - __mTimeSinceLastMake);
		
		if (diff >= __mRequiredTime)
		{
			var myPlatform:PlatformObject = makeNewPlatform(x, y, w, h);
			__mTimeSinceLastMake = getTimer();
		}
	}
}

And have the update() function be called by some main update loop.

I'd do it this way only because I find having a Timer that frequently changes its delay can be cumbersome. Albeit your solution is a viable one.