2d Array in Flash

Having trouble with variable declaration

Page 1 of 1

3 Replies - 28367 Views - Last Post: 05 December 2007 - 09:54 PM

#1 FernFerret   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 14-April 05

2d Array in Flash

Post icon  Posted 05 December 2007 - 06:50 PM

Hello everyone, I am having a bit of trouble with my actionscript. I've put down the as for 4 years and learned java and CPP so i've near forgotten everything i didn't know. I would like to create a 2d array in flash and I can't seem to find the right syntax to do so, here is what I have:

_root.arr = new Array();
_root.arr[0][0] = 1;
_root.arr[0][1] = 2;
_root.arr[1][0] = 3;
_root.arr[1][1] = 4;
trace(_root.arr.length);
trace(_root.arr[0][1]);
trace(_root.arr[1][0]);
for(var i = 0; i < _root.arr.length; i++)
{
	trace("I: " + i);
	x = i*20;
	_root.tri_down.duplicateMovieClip("tri_down"+i, i);
	_root["tri_down"+i]._x = x+10;
	_root["tri_down"+i]._y = 50;
}



I get the following printout in my console:
0
undefined
undefined



Is This A Good Question/Topic? 0
  • +

Replies To: 2d Array in Flash

#2 theRemix   User is offline

  • D.I.C Regular

Reputation: 15
  • View blog
  • Posts: 440
  • Joined: 19-October 05

Re: 2d Array in Flash

Posted 05 December 2007 - 07:15 PM

View PostFernFerret, on 5 Dec, 2007 - 06:50 PM, said:

Hello everyone, I am having a bit of trouble with my actionscript. I've put down the as for 4 years and learned java and CPP so i've near forgotten everything i didn't know. I would like to create a 2d array in flash and I can't seem to find the right syntax to do so, here is what I have:

_root.arr = new Array();
_root.arr[0][0] = 1;
_root.arr[0][1] = 2;
_root.arr[1][0] = 3;
_root.arr[1][1] = 4;
trace(_root.arr.length);
trace(_root.arr[0][1]);
trace(_root.arr[1][0]);
for(var i = 0; i < _root.arr.length; i++)
{
	trace("I: " + i);
	x = i*20;
	_root.tri_down.duplicateMovieClip("tri_down"+i, i);
	_root["tri_down"+i]._x = x+10;
	_root["tri_down"+i]._y = 50;
}



I get the following printout in my console:
0
undefined
undefined



		var new_ar:Array = new Array(
			new Array(1,2,3,4),
			new Array("red","green","blue")
									 );
		trace("length : " + new_ar.length);
		trace("new_ar[0][2] : " + new_ar[0][2]);
		trace("new_ar[1][2] : " + new_ar[1][2]);
		new_ar[1][1] = "yellow";
		trace("new_ar[1][1] : " + new_ar[1][1]);


output:
length : 2
new_ar[0][2] : 3
new_ar[1][2] : blue
new_ar[1][1] : yellow


hth
Was This Post Helpful? 1

#3 FernFerret   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 14-April 05

Re: 2d Array in Flash

Posted 05 December 2007 - 08:51 PM

AWESOME thank you so much, i'd been looking all over to find that out, now one other question:
in Java i would say this.current.length to get the width and this.current[x].length to get height, how would i get the h and w out of arr[][] since technically I haven't named the subarrays anything

Thanks again,
Eric
Was This Post Helpful? 0
  • +
  • -

#28 DilutedImage   User is offline

  • D.I.C Addict
  • member icon

Reputation: 15
  • View blog
  • Posts: 646
  • Joined: 20-November 06

Re: 2d Array in Flash

Posted 05 December 2007 - 09:54 PM

Arrays have a length property in AS as well, so you can do it the same way as you would in Java.

The reason it traced a length of zero before was because you were attempting to write to arrays that did not exist, and therefore wrote nothing (hence the 0, undefined, undefined). You first must create the subarrays:

_root.arr = new Array();
_root.arr[0] = new Array();
_root.arr[0][0] = 1;
_root.arr[0][1] = 2;
_root.arr[1] = new Array();
_root.arr[1][0] = 3;
_root.arr[1][1] = 4;


Or, the shorthand version:
_root.arr = [];
_root.arr[0] = [];
_root.arr[0][0] = 1;
_root.arr[0][1] = 2;
_root.arr[1] = [];
_root.arr[1][0] = 3;
_root.arr[1][1] = 4;


Or, the way theRemix suggested:
_root.arr = new Array(new Array(1,2), new Array(3,4));


Or, even more shorthand:
_root.arr = [[1,2],[3,4]];


Etc, etc, etc ...
Was This Post Helpful? 1

Page 1 of 1