Be a Supporter!

Timer Not Updating

  • 283 Views
  • 6 Replies
New Topic Respond to this Topic
Noamyoungerm
Noamyoungerm
  • Member since: Dec. 6, 2009
  • Offline.
Forum Stats
Member
Level 11
Programmer
Timer Not Updating Apr. 21st, 2012 @ 11:54 AM Reply

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
ProfessorFlash
ProfessorFlash
  • Member since: Oct. 6, 2007
  • Offline.
Forum Stats
Member
Level 32
Programmer
Response to Timer Not Updating Apr. 21st, 2012 @ 12:57 PM Reply

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.

Diki
Diki
  • Member since: Jan. 31, 2004
  • Offline.
Forum Stats
Moderator
Level 13
Programmer
Response to Timer Not Updating Apr. 21st, 2012 @ 01:07 PM Reply

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

Jin
Jin
  • Member since: Sep. 9, 2006
  • Offline.
Forum Stats
Supporter
Level 49
Blank Slate
Response to Timer Not Updating Apr. 21st, 2012 @ 01:14 PM Reply

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

BBS Signature
Noamyoungerm
Noamyoungerm
  • Member since: Dec. 6, 2009
  • Offline.
Forum Stats
Member
Level 11
Programmer
Response to Timer Not Updating Apr. 21st, 2012 @ 02:08 PM Reply

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
Noamyoungerm
Noamyoungerm
  • Member since: Dec. 6, 2009
  • Offline.
Forum Stats
Member
Level 11
Programmer
Response to Timer Not Updating Apr. 21st, 2012 @ 02:10 PM Reply

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
Diki
Diki
  • Member since: Jan. 31, 2004
  • Offline.
Forum Stats
Moderator
Level 13
Programmer
Response to Timer Not Updating Apr. 21st, 2012 @ 06:49 PM Reply

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.