Flash timeline elapsed time calculation - actionscript-3

I am doing an animation and it has around 5000 frames and the frame rate is 24. I want to calculate the elapsed time in seconds.
Consider I navigated to 1000 th frame. Now what was the elapsed time?
var fps:Number = 24;
var totolF:Number = 5000;
var sec:Number = 5000/24;
var cF:Number = 1000;
var elapsedTime:Number = ???

First way:
var fps:Number = 24;
var totolF:Number = 5000;
var sec:Number = 5000/24;
var cF:Number = 1000;
var elapsedTime:Number = Math.floor(1/24 *currentFrame);
Second way(more accurate but if you pause your movie, elapsed time won't pause):
var fps:Number = 24;
var totolF:Number = 5000;
var sec:Number = 5000/24;
var cF:Number = 1000;
var elapsedTime:Number =Math.floor(getTimer()/1000);
I think better to use first one.
Hopes this post helps.☻

Related

AS3: FPS get down

Ahoy. When i want update information with ENTER_FRAME like this:
import flash.events.Event;
var ticks:uint = 0;
var last:uint = getTimer();
var food:uint = 10;
var wood:uint = 10;
var stone:uint = 10;
stage.addEventListener(Event.ENTER_FRAME, update); // getFPS
stage.addEventListener(Event.ENTER_FRAME, list); //get Materials Info
function update(e:Event){
ticks++;
var now:uint = getTimer();
var delta:uint = now - last;
if (delta >= 1000) {
var fps:int = ticks / delta * 1000;
fpsText.text = String(fps+"fps");
ticks = 0;
last = now;
}
}//
function list(e:Event){
foodText.text = String(food+"food");
woodText.text = String(wood+"wood");
stoneText.text = String(stone+"stone");
}//
fps drop down.
When i change code like this:
import flash.events.Event;
var ticks:uint = 0;
var last:uint = getTimer();
var food:uint = 10;
var wood:uint = 10;
var stone:uint = 10;
stage.addEventListener(Event.ENTER_FRAME, update); // getFPS
function update(e:Event){
ticks++;
var now:uint = getTimer();
var delta:uint = now - last;
if (delta >= 1000) {
var fps:int = ticks / delta * 1000;
fpsText.text = String(fps+"fps");
list();
ticks = 0;
last = now;
}
}//
function list(){
foodText.text = String(food+"food");
woodText.text = String(wood+"wood");
stoneText.text = String(stone+"stone");
}//
fps drop down after 15 minutes.
I know problem is in function list(), but how I list materials quick without slowing fps.
How i change this for clean run?
Thx for hlp.
I'm assuming there is more code somewhere else that will change the values of food, wood, and stone. No need to update the values on enterframe if they aren't changing. Perhaps you could update the UI only when you change the values? So remove list() from the enter frame and do something like this when you change the values?
function getWood(){
wood++;
list();
}
function useWood(){
wood--;
list();
}

NaN from speed test

I'm trying to test the time it takes to calculate and create a certain matrix. The code being used is below.
import flash.geom.Matrix;
import flash.utils.getTimer;
var iter:int = 0;
var titer:int = 10000;
var time:Number;
var ttime:Number;
var zm:Number = 1.5;
var sx:Number = 0, sy:Number = 0.5;
var rot:Number = Math.PI/6;
var tx:Number = 10, ty:Number = 0;
var w:Number = 50, h:Number = 50;
var cr:Number = Math.cos(rot), sr:Number = Math.sin(rot);
var m3:Matrix, mt:Matrix;
for(iter; iter<titer; iter++) {
time = getTimer();
m3 = new Matrix(cr*(sx+zm), -sr*(sx+zm), sr*(sy+zm), cr*(sy+zm), tx+w-cr*w*(sx+zm)-sr*h*(sy+zm), ty+h+sr*w*(sx+zm)-cr*h*(sy+zm));
ttime += getTimer() - time;
}
trace('total:', ttime, 'avg:', Number(ttime)/Number(titer));
I'm SURE I set this up properly, but the trace is returning 'NaN'. What could cause this?
It will be very short and simple response: var ttime: Number = 0; ;) It's a good programming practice to initialise variables.
Also, I don't understand your construction for for performance testing. What do you think about such construction?
var time:uint = getTimer();
//your 'for' with Matrix
time = getTimer() - time;
//trace results...

Countdown timer not working in AS3

Since Im new in AS3 and I just converted AS2 to AS3. The countdown doesnt work. Right now the 4 digits are looping all in the same time very fast (The three digits animation is fine - ignore it)
See countdown - http://magnixsolutions.com/clients/OT/media-buys_scoreboard-redux_160x600_5-8-2009.html
AS3
// get the current date and time as it exists at
// this instance in time when the frame is entered
var currentDate:Date = new Date();
var thisYear:int = currentDate.getFullYear();
var thisMonth:int = currentDate.getMonth();
var thisDate:int = currentDate.getDate();
var thisHour:int = currentDate.getHours();
var thisMinute:int = currentDate.getMinutes();
var thisSecond:int = currentDate.getSeconds() + 12;
var thisMSecond:int = currentDate.getMilliseconds();
// Date( year, month-1, date [, hour [, minute [, second [, millisecond]]]])
var eventDate = new Date(thisYear, thisMonth, thisDate, thisHour, thisMinute, thisSecond, thisMSecond);
var eventMillisecs = eventDate.getTime();
// get the current date and time as it exists at
// this instance in time when the frame is entered
this.addEventListener(TimerEvent.TIMER, enterFrameHandler);
function enterFrameHandler() {
currentDate = new Date();
var currentMillisecs = currentDate.getTime();
this.msecs = eventMillisecs - currentMillisecs;
if (this.msecs <= 0){
play();
return;
}
// if the date hasn't been reached, continue to
// devise seconds, minutes, hours and days from
// the calculated milliseconds
this.secs = Math.floor(this.msecs/1000); // 1000 milliseconds make a second
this.mins = Math.floor(this.secs/60); // 60 seconds make a minute
this.hours = Math.floor(this.mins/60); // 60 minutes make a hour
this.days = Math.floor(this.hours/24); // 24 hours make a second
this.msecs = int(this.msecs % 1000);
this.secs = int(this.secs % 60);
this.mins = int(this.mins % 60);
this.hours = int(this.hours % 24);
this.days = int(this.days);
while (this.msecs.length < 3) this.msecs = "0" + this.msecs;
if (this.secs.length < 2) this.secs = "0" + this.secs;
if (this.mins.length < 2) this.mins = "0" + this.mins;
if (this.hours.length < 2) this.hours = "0" + this.hours;
while (this.days.length < 3) this.days = "0" + this.days;
for(var movie in this){
if (this[movie]._parent == this) this[movie].evaluateFrameFrom(this);
}
};
MovieClip.prototype.evaluateFrameFrom = function(variableClip){
var nameArray = this._name.split("_");
var numberSet = variableClip[nameArray[0]];
var character:int = parseInt(nameArray[1]);
var frame = 1 + parseInt(numberSet.charAt(character));
if (this._currentframe != frame) this.gotoAndStop(frame);
};
This is probably what you mean:
// There is no number type in AS3. Use parseInt to cast string to int
var character:int = parseInt(nameArray[1]);
var frame = 1 + parseInt(numberSet.charAt(character));
Also, there's no such thing as _root in ActionScript 3.0. Try this:
this.avgscore_mc.gotoAndPlay(2);
And you need to add your enterFrame like this:
this.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
function enterFrameHandler() {
// Stuff in your enter frame
}
Sounds like you're still thinking AS 2.0!

adding movieclips from random generated no in array

hey i am trying to add movie clips to the stage randomly from a list of cards, no 1- 10,
this is what i have tried so far but i get an error saying its my randomly selected card is not a function, just wondering if anybody can help or know the proper way of accomplishing it
thank you
var printArray:Array =new Array();
var randPrint:String;
var rand
for(var n:int = 1; n <= 28; n++)
{
randNo=Math.round(Math.random() * 10+.5);
randPrint = "cardPrint"+randNo;
printArray.push(randPrint);
}
var cardPrint1:MovieClip = new card_1();
var cardPrint2:MovieClip = new card_2();
var cardPrint3:MovieClip = new card_3();
var cardPrint4:MovieClip = new card_4();
var cardPrint5:MovieClip = new card_5();
var cardPrint6:MovieClip = new card_6();
var cardPrint7:MovieClip = new card_7();
var cardPrint8:MovieClip = new card_8();
var cardPrint9:MovieClip = new card_9();
var cardPrint10:MovieClip = new card_10();
for(var p:int = 1; p <= 1; p++)
{
trace(printArray[p]);
addChild(printArray[p]);
}
some help would be great, thank you so much
I think something like the following will do what you want. I populated an array with all the available assets and then filled an array with random numbers between 0-9. The last for loop just creates the movieclips and adds them to the stage.
var printArray:Array = [];
var mcs:Array = [card_1, card_2, card_3, card_4, card_5, card_6, card_7, card_8, card_9, card_10];
for(var n:int = 1; n <= 28; n++)
{
var randNo:int = int(Math.random() * 10);
printArray.push(randNo);
}
for(var p:int = 0; p < printArray.length; p++)
{
trace(printArray[p]);
var mc:MovieClip = new mcs[printArray[p]];
addChild(mc);
}

Intermittent/staggered loading of an object

I've just recently tried my hand at actionscript 3 and have come across a road block.
How do I go about rendering the cubes (cube1) intermittently, ie. staggered loading. I need the cubes to load a split second from each other.
Below is a snippet of what I have so far:
var rows:int = 5;
var cols:int = 3;
var spacery:int = 100;
var spacerx:int = 120;
var box_count:int = 8;
for(var i:int; i < box_count; i++) {
cube1 = new Cube(ml,100,10,80,1,1,1);
cube1.y = ((i % rows)) * (cube1.x + spacery);
cube1.x = Math.floor(i/rows) * (cube1.x +spacerx);
cube1.z = 0;
bigBox.addChild(cube1);
}
//Create an array out side the function; as a global (instance) variable:
var cubes:Array = [];
//instead of bigBox.addChild(cube1), store them in the array:
cubes.push(cube1);
//initialize a timer outside after for loop
//Fire every 100 milliseconds, box_count times
var timer:Timer = new Timer(100, box_count);
timer.addEventListener(TimerEvent.TIMER, onTick);
timer.addEventListener(TimerEvent.TIMER_COMPLETE, onTickDone);
function onTick(e:Event):void
{
bigBox.addChild(cubes[timer.currentCount]);
}
function onTickDone(e:Event):void
{
cubes = null;
timer.removeEventListener(TimerEvent.TIMER, onTick);
timer.removeEventListener(TimerEvent.TIMER_COMPLETE, onTickDone);
timer = null;
}