View Full Version : Problem with arrays
LeonardoZimbres
November 4th, 2008, 06:17 AM
I feel a little dumb, because the thing is very simple but Im not figuring out whats wrong.
I want to set an array value inside a function, but when I check this lenght in another function, the array is empty! Someone know whats wrong?
The code:
var AAny:Array = new Array()
AAny = ["one","two","tree"]
startComboBox(AAny)
var aCbs:Array = new Array()
function startComboBox(arr:Array):void {
//aCbs=arr
aCbs = arr
trace("len:"+aCbs.length); /////// OUTPUT: len:3
cb_first.addEventListener(MouseEvent.CLICK, moveComboBox);
}
function moveComboBox(evt:MouseEvent):void {
trace(aCbs.length+" dammit"); OUTPUT: 0 dammit
}//
Ranoka
November 4th, 2008, 06:47 AM
Are you declaring the Array inside a function? Looks like you might be 'cos of the closing curly brace.
Try declaring it outside of the function, and giving it a value inside the function.
LeonardoZimbres
November 4th, 2008, 07:23 AM
Yes, I am doing exactely that, look:
var AAny:Array = new Array()
AAny = ["one","two","tree"]
var aCbs:Array = new Array()
These lines are outside functions
The next ones, inside a function:
function startComboBox(arr:Array):void {
//aCbs=arr
aCbs = arr
}
But when I check the array lenght in another function, it returns 0:
function moveComboBox(evt:MouseEvent):void {
trace(aCbs.length+" dammit"); OUTPUT: 0 dammit
}//
Valaran
November 4th, 2008, 07:39 AM
Its pretty simple..
var AAny:Array = new Array();
AAny = ["one","two","tree"];
startComboBox(AAny);
var aCbs:Array = new Array();
Thats what you did, your defining the aCbs array AFTER you have already run the function, you should be defining it before you run the function like:
var AAny:Array = ["one","two","tree"];
var aCbs:Array = new Array();
startComboBox(AAny);
LeonardoZimbres
November 4th, 2008, 08:52 AM
Its... its strange... the aCbs was defined inside the function... it was to work... but I did some changes, exported the swf, and now its working... so I did undo to the changes, and now its working too... this does not make any sense! Maybe flash cs3 on mac have a issue? I will try to go back more times and see the problem.
LeonardoZimbres
November 4th, 2008, 08:55 AM
damm, now I see, I really did called the function before defining the variable. How... damm. Thanks for the light guys.
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.