stage force render / redraw - actionscript-3

Normally I would listen to the ENTER_FRAME event and execute code each time a frame gets rendered.. But for my application this is too slow. The time between 2 ENTER_FRAME events is 40 milliseconds. Doesn't matter if I change my framerate.
So is it possible to force flash to redraw/render the frame without listening to events? I tried the stage.invalidate() method and changing the framerate but it doesn't improve anything (stage.invalidate() doesn't do anything actually)
I did a small test to see the average time between the ENTER_FRAME and the EXIT_FRAME event:
private var beginTime:Number;
private var endTime:Number;
public function init():void {
addEventListener(Event.ENTER_FRAME, enterFrame);
addEventListener(Event.EXIT_FRAME, exitFrame);
}
private function enterFrame(e:Event):void {
beginTime = new Date().getTime();
if(endTime) {
trace(beginTime - endTime);
}
}
private function exitFrame(e:Event):void {
endTime = new Date().getTime();
}
If I am right the ENTER_FRAME event is fired before the rendering of that frame and the EXIT_FRAME event is fired after the rendering. So after the EXIT_FRAME event the rendering is complete and the app will dispatch an ENTER_FRAME event.
The average time between those two events is 52 milliseconds. And I want to shorten that time
Bytheway I have set my framerate to 180 for this test

stage.Invalidate will force the stage to redraw. Its not suggested you should fix your time problem first.

Related

ActionScript 3 - Timer/Countdown

I'm trying to make timer which goes up 1 every second and this is displayed in a dynamic text box called timer_txt.
When the timer reaches 60 secounds I want to display a screen, to do this I will use gameOver.visible = true;. But how would I get the timer to completely stop?
Also I want the car object to stop the timer completely when it hits the finish movie clip and to display the finshed time in a dynamic text box called finishTime. This is all on the same frame.
Would anyone please help me ?
This is the code I currently have to calculated the time but I'm currently having no luck.
var currentTime:int = getTimer();
//Setting the timer to 0.
var Secounds = 0;
function timer():void{
//Adding 1 to secounds var.
Secounds +=1;
}
setInterval(timer,1000);
trace(Secounds);
Use Timer instead of setInterval.
var timer:Timer = new Timer(1000, 60);
1000 — is a delay in ms.
60 — is a repeat count.
Timer will fire every second for one minute.
Next, add an event listener.
timer.addEventListener(TimerEvent.TIMER, tick);
timer.addEventListener(TimerEvent.TIMER_COMPLETE, complete);
and start the timer:
timer.start();
The tick method will fired (called) every second.
After 1 minute the complete method will fired (called).
To stop the timer call timer.stop();

Timer Event TIMER_COMPLETE not taking into account added time

Hey everyone so basically I have a main timer which keeps track of how long the player has until the game is over. If the timer is done counting down then the game is over. I also have items on the stage as well as purchases to add more time to the clock so they can keep playing.
The bug that I am encountering is that when the player adds more time to the timer it shows on the text field that time was added and it continues to count down but it doesn't actually add more time and still ends after 60 seconds.
What could be causing this issue here is my current code below thanks!
In my constructor:
//Count down timer
count = 60; //Count down from Number
tCountDownTimer = new Timer(1000, count);// Timer intervall in ms
updateCountdownTimer();
tCountDownTimer.addEventListener(TimerEvent.TIMER, countdown);//EventListener for intervalls
tCountDownTimer.addEventListener(TimerEvent.TIMER_COMPLETE, countdownComplete);
Here is my countdown Timer event:
private function countdown(e:TimerEvent):void
{
//Display Time in a textfield on stage
updateCountdownTimer();
}
public function updateCountdownTimer():void
{
cntDowntxt.countDownTextField.text = String((count) - tCountDownTimer.currentCount);
}
In a seperate function when the player touches the clock this is how I add more time:
count += 10;
updateCountdownTimer();
Could you see what I am doing wrong?
If you try to update the repeatCount of the Timer instance?
count += 10;
tCountDownTimer.repeatCount = count;
updateCountdownTimer();

Timer given for Enemy spawning is not pausing when the game is in paused mode

I am creating a simple shooting game, in which the enemies will spawn at each clock tick, for which I have used a simple TimerEvent.. And now I am having a issue, when I press Pause button the game pauses properly, but the timer of enemy spawning is not getting paused. The enemy continues to get spawn. My pause code contains only stage.framerate = 0. So please help me for the above issue.. Thanks in advance..
My main code is -
package
{
import........
..............
public class Main extends MovieClip
{
//var declearations...
//Timer Events declearation....
private function timeStart():void
{
Timer1 = new Timer(1000,180);
Timer1.addEventListener(TimerEvent.TIMER,TimerStart);
Timer1.start();
}
private function TimerStart(e:TimerEvent):void
{
createEnemy(enemy1,Enemy1,enemies1);
}
private function createEnemy(enemy:MovieClip,enemyClass:Class, enemyArray:Array)
{
enemy = new enemyClass(bg_mc);
enemyArray.push(enemy);
bg_mc.addChild(enemy);
}
}
}
It should not pause, it does not depend on stage.frameRate. In order to align your framerate and enemies appearance, use a listener to Event.ENTER_FRAME which will both count frames until next enemy, move everything frame by frame (you most likely have this part made), spawn new enemies and control paused state - you can do a simple if (weArePaused) return; at the beginning of the listener to make the pause without altering stage.frameRate.

How do i convert the value of a timer event to a number?

I'm making a game where the reaction time to killing an enemy is shown and used in later calculations. The number needs to be precise and in seconds, with three significant figures. So i need the current value of a timer to be converted to a numerical value.
I have it so the enemy spawns, a timer (reactionTimer) event begins and when it's killed a variable (reactionTime) will be set to the reactionTimer.
a new one then spawns and it resets the timer and variable.
I've had a few tries at it but it always ends up showing the timer value as either "Object Timer" or 0.
I've also tried using currentCount but this doesn't give me decimal numbers and just works oddly.
New ActionScript developers often mix up the purpose of the Timer class and TimerEvent. It's really for triggering actions at a specific time, not for measuring time.
For measuring time, you use the getTimer function. Whenever you call it, it returns the number of milliseconds that have elapsed since Flash player started. To measure an elapsed time you just call getTimer() twice and subtract the first measurement from the second.
There are a lot of ways you could organize your particular situation. One would be to store the spawn timestamp of the enemy right on the enemy class, capturing it the constructor:
public class Enemy extends MovieClip {
public function get spawnTime():int { return _spawnTime; }
private var _spawnTime:int;
// Constructor
public function Enemy() {
super();
_spawnTime = getTimer();
}
}
Then when enemy is killed you call getTimer() again to get the reaction time:
// Somewhere, where you are processing the enemy killed event
var killTime:int = getTimer();
var reactionTime:int = killTime - killedEnemy.spawnTime; // in whole ms
var reactionSeconds:Number = reactionTime / 1000; // in seconds

Having difficulty in understanding ActionScript 3 Timer class

I'm trying to make a dice game in Flash/ActioScript 3. I did all the essentials and it works smoothly. Now I want to improve the user experience. For instance, when it's computer's turn (to roll and do things according to die value) I want to animate the die. The die has 6 keyframes. So, for, say, 2 seconds the die will loop those 6 frames then it will stop on a value (depending on random generator). Somehow I can't do it as I want. How can I write a function(s) so that when I say,
animateDice()
it will do nothing but just animate the dice for a specified interval?
Update:
var timer:Timer = new Timer(10, 50);
myButton.addEventListener(MouseEvent.CLICK, onClick);
timer.addEventListener(TimerEvent.TIMER, animateDice);
function onClick(event: Event):void {
timer.start();
}
function animateDice(event: Event):void {
dice.play();
}
For instance, I don't understand why the above code doesn't work properly. It does work properly on first click, but not there after.
Update 2: I guess I'm still having problems. How do I suspend the running code until the timer stops? (Yes there is a work around---putting timer handlers inside other timers, etc. Is there an easy way?
Maybe, this will help:
First we see the die rolling (and a message box informs the user that the game will decide whom starts). Then it's either Human's or Computer's turn. When it's computer's turn, first we see the rolling die again for, say, 1 second. Then it stops and and we see the outcome. I'm a beginner and I nay be missing something, but from what I see it seems that all these simple steps (just showing the die rolling for some time) means lots and lots of lines.
If I use a simple timer for die animation, the script continues and the whole show goes away.
The timer object has three properties:
delay, or how often the event should fire
repeatCount, or how many times the event should fire
currentCount, or how many times the timer's event has fired thus far
You are creating the timer with new Timer(10, 50), which sets delay to 10 and repeatCount to 50. This means that, once you call timer.start(), timer will fire TimerEvent.TIMER every 10 milliseconds. Each time it is fired, it adds 1 to currentCount. When currentCount is greater than or equal to repeatCount (50), it stops looping the timer.
Once your timer has stopped, if you call timer.start() again, it will only fire the event once, because currentCount has not been reset to zero, and is still >= repeatCount.
If you call timer.reset() before calling timer.start(), it will set this value to zero and things should behave as expected.
var timer:Timer = new Timer(2000, 1);
myButton.addEventListener(MouseEvent.CLICK, onClick);
timer.addEventListener(TimerEvent.TIMER_COMPLETE, onTimerComplete);
function onClick(event: Event):void {
timer.reset();
timer.start();
dice.play();
}
function onTimerComplete(event:TimerEvent):void {
var roll:int = int(Math.floor(Math.rand()*6))+1;
dice.gotoAndStop(roll);
}
The timer is set to run only once, for 2000 milliseconds (which are 2 seconds). When Click occurs, the timer is reset (so that if it's not the first time it was clicked, it will run as if it was the first time) and started, and the animation starts a well. After 2 seconds, TIMER_COMPLETE will be fired by the timer, and we catch it and determine a final number for the die, then gotoAndStop to that frame.
I didn't try to compile the code, but the gist of it should work for you.
P.S, dice is the plural of 'die' :) you're skipping a great opportunity for the type of variable names we all want to use but can't!
You could try something a little more like this:
var t:Timer = new Timer(10, 50);
t.addEventListener(TimerEvent.TIMER, timerHandler);
t.addEventListener(TimerEvent.TIMER_COMPLETE, timerCompleteHandler);
t.start();
function timerHandler(e:TimerEvent):void {
gotoRandomFrame();
}
private function timerCompleteHandler(e:TimerEvent):void {
var finalNum:int = gotoRandomFrame();
// Using finalNum
}
private function gotoRandomFrame():int {
var num:int = Math.floor(Math.random() * 6) +1;
dice.gotoAndStop(num);
return num;
}
So use gotoAndStop to set your frame rather than using play