PDA

View Full Version : Help with a for statement in AS3/Flash9


kpeluso
05-04-2007, 10:34 PM
Ive been developing a project for a little over a month that I now need to convert to AS3 due to a qtvr "plugin" my client is insisting I use (flashpanoramas.com) which is AS3 only.

In AS2 i had the following code:


onEnterFrame = function () {
for (i=1; i<=buttons.length; i++) {
buttons[i]._x = (buttons[i-1]._x+buttons[i-1]._width);
t[i]._x = (buttons[i-1]._x+buttons[i-1]._width+3);
}
};

buttons = new Array(square1, square2, square3, square4, square5, square6, square7, square8, square9);
t = new Array(t1, t2, t3, t4, t5, t6, t7, t8, t9);

function Animate(itemRelease) {
for (i=0; i<=buttons.length; i++) {
buttons[i].tween('_y', 0,0.5,'easeOutQuad',0.55);
t[i].tween('_y', 0,0.25,undefined,0.75);
buttons[i].tween(xy, amountBack, time, ease, 0.5);
}
itemRelease.tween('_width', 360,0.5,'easeOutQuad',1);
itemRelease.tween('_height', 360,0.5,'easeOutQuad',1);
itemRelease.ySlideTo(-110, 0.5, "easeOutQuad",1);
reset();
itemRelease.enabled = false;
itemRelease._y = -20;
textNumber._y = -23;
textNumber.alphaTo(0,0.25,"easeOutSine",0);
}


I know that Ill need to rewrite Animate(), but my problem seems to be with my for statements. When I rewrote the onEnterFrame function to the code below I get this error:
TypeError: Error #1010: A term is undefined and has no properties.
at total/onEnterFrame()


var buttons:Array = new Array(square1, square2, square3, square4, square5, square6, square7, square8, square9);
var t:Array = new Array(t1, t2, t3, t4, t5, t6, t7, t8, t9);

addEventListener(Event.ENTER_FRAME, onEnterFrame);
function onEnterFrame(event:Event):void
{
for (i=1; i<=buttons.length; i++) {
buttons[i].x = (buttons[i-1].x+buttons[i-1].width);
t[i].x = (buttons[i-1].x+buttons[i-1].width+3);
}
}


thanks in advance for any insight.

kpeluso
05-04-2007, 11:55 PM
ok, I was able to supress the error using try-catch, but when I trace(i), it only counts to 8 where the AS2 version couts to 9, which is correct.

joshchernoff
05-05-2007, 03:57 AM
ok, I was able to supress the error using try-catch, but when I trace(i), it only counts to 8 where the AS2 version couts to 9, which is correct.
I think in AS3 you have to declare var i out side of the for statment.

so something like this.

var i:unit;
for (i = 0; i <then whatever; i++){

"I think, dont know for sure"

Dazzer
05-05-2007, 04:27 AM
not true

that said, you do have to declare 'i' somewhere

either var i:int outside or
similar to java

for (var i:int = 0; i < 10; i++)
{

}

joshchernoff
05-06-2007, 12:09 AM
not true

that said, you do have to declare 'i' somewhere

either var i:int outside or
similar to java
ActionScript Code:
for (var i:int = 0; i < 10; i++)
{

}





so could it be safe to say that maybe his problem was do to improper var declaration of "i" ?

Dazzer
05-06-2007, 12:44 AM
I'm not sure. Because he says that he can trace i inside the for loop. So I'm really not sure until I actually have the code, and do a full debug :)

Sher Ali
11-16-2007, 06:11 AM
You have to use unint because int starts from 1 whereas unint starts from 0: See documentation for more help:

So correct way is to write and it will output 10 itmes:

for (var i:unint = 0; i < 10; i++)
{

}

Aquilonian
11-16-2007, 09:13 AM
shouldn't be
for (var i:int=1 ; i< buttons.length ; i++) {
instead of i <= buttons.length
?

Dazzer
11-16-2007, 09:26 AM
You have to use unint because int starts from 1 whereas unint starts from 0: See documentation for more help:
Absolutely false.
ints can be 0 as well.
uints cannot be less than 0. (Unsigned Integers)

I believe it has been generally documented that ints are faster to process than uints, and I tend to stay away from uints if I can.

Aquilonian seems to be right. You should have < buttons.length, instead of <= buttons.length.

Charleh
11-16-2007, 09:38 AM
But should it not be

for (var i:int = 0 ; i < buttons.length ; i++)

or are arrays 1 based in as3?

453.0
11-16-2007, 09:46 AM
IMHO, he shouldn't use int and nor uint, but Number. Try reading up on the efficiency of these 3 types and their performance. In almost all cases Number is much faster and reliable then int or uint.

for(var i:Number = 0; i < buttons.length; i++) { // code comes here }

Good luck.

Aquilonian
11-16-2007, 10:20 AM
But should it not be

for (var i:int = 0 ; i < buttons.length ; i++)

or are arrays 1 based in as3?


As inside the loop he is going
buttons[i].x = (buttons[i-1].x+buttons[i-1].width);
i supose it has to start with 1, not sure whats this code was suposed to do, havent looked at the as2 version yet


IMHO, he shouldn't use int and nor uint, but Number. Try reading up on the efficiency of these 3 types and their performance. In almost all cases Number is much faster and reliable then int or uint.

I've tested it many times, int is the fastest for loops.

Gundark
11-16-2007, 10:23 AM
No, arrays are most definetly not 1-based in AS3. They are all 0-based.

As for using Number instead of int. An int is slightly faster for integer addition (which is what a for loop's iteration is). They are slower for any other operation that might result in a floating point (plus multiplication for some reason).

Since he does "buttons[i].x = buttons[i-1].x+buttons[i-1].width);"

The array will have to start from 1, but it's still not gonna work right because when i = buttons.length, buttons[i] is gonna be null.

Edit: Darn, beaten to it!

453.0
11-16-2007, 11:31 AM
Yeps, sorry my mistake, int is slightly faster then Number when it comes to addition ( but don't even think on using uint because that's totally bad ).

More info about them here:
http://www.kirupa.com/forum/showpost.php?p=2243590&postcount=6

Good luck.

Dazzer
11-16-2007, 11:42 AM
Probably any operation that results in a floating point number will have to cast the float down to an int, hence the extra overhead. But then again it is insignificantly small.

Unless you're building your own papervision3d