8 Replies - 2708 Views - Last Post: 20 July 2011 - 12:27 PM

#1 NiteFlame  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 34
  • Joined: 11-June 10

2D Array in AS3

Posted 17 July 2011 - 05:51 PM

went to a website and did everything they said to do just to tell me at the end "this is how you dnt do it" witch pissed me off and i came here looking for help.

i want to make a 2D Array that is filled with random terrain and set the x,y and add it all at once if i can

is there a way to use what i have now and just tweek it to work if so can i be pointed the rite way to do that
or if this is just a mess i cnt use can someone point me in the way i should be going

package  {
	
	import flash.display.*;
	import flash.events.*;
	
	public class TerrainSetUp extends MovieClip{
		
		public var Snow : MovieClip = new MovieClip;
		public var Grass : MovieClip = new MovieClip;
		public var Sand : MovieClip = new MovieClip;
		public var Rocky : MovieClip = new MovieClip;
		public var Water : MovieClip = new MovieClip;
		public var RanNum : Number = 0;
		public var i : Number = 0;
		public var j : Number = 0;
		public var TerrainArray :Array = new Array(20 , 20);
		public var Set : Boolean = true;
		
		public function TerrainSetUp() {
			Snow = new SnowImg;
			Grass = new GrassImg;
			Sand = new SandImg;
			Rocky = new RockyImg;
			Water = new WaterImg;
			
			Snow.scaleX = 50;
			Grass.scaleX = 50;
			Sand.scaleX = 50;
			Rocky.scaleX = 50;
			Water.scaleX = 50;
			
			Snow.scaleY = 50;
			Grass.scaleY = 50;
			Sand.scaleY = 50;
			Rocky.scaleY = 50;
			Water.scaleY = 50;
			
			if(Set == true){
			   Set = false
			   SetTheTerrain();
			   }
			
			
		}
		
		public function SetTheTerrain():void{
			
			
			for( i = 0 ; i < 20 ; i ++){
				RanNum = Math.floor(Math.random() * (5))
				trace (i,j)
				if(RanNum == 0){
					TerrainArray[i,j] = Grass;
					TerrainArray[i,j].x = (i * 50);
					TerrainArray[i,j].y = (j * 50);
					addChild(TerrainArray[i,j]);
				}
				if(RanNum == 1){
					TerrainArray[i,j] = Snow;
					TerrainArray[i,j].x = (i * 50);
					TerrainArray[i,j].y = (j * 50);
					addChild(TerrainArray[i,j]);
				}
				if(RanNum == 2){
					TerrainArray[i,j] = Rocky;
					TerrainArray[i,j].x = (i * 50);
					TerrainArray[i,j].y = (j * 50);
					addChild(TerrainArray[i,j]);
				}
				if(RanNum == 3){
					TerrainArray[i,j] = Water;
					TerrainArray[i,j].x = (i * 50);
					TerrainArray[i,j].y = (j * 50);
					addChild(TerrainArray[i,j]);
				}
				if(RanNum == 4){
					TerrainArray[i,j] = Sand;
					TerrainArray[i,j].x = (i * 50);
					TerrainArray[i,j].y = (j * 50);
					addChild(TerrainArray[i,j]);
				}else{
					TerrainArray[i,j] = Grass;
					TerrainArray[i,j].x = (i * 50);
					TerrainArray[i,j].y = (j * 50);
					addChild(TerrainArray[i,j]);
				}
				if(i == 5){
					i = 0
					j++;
					if( j == 5){
						break;
					}
				}
			}
		}
	}
	
}



Is This A Good Question/Topic? 0
  • +

Replies To: 2D Array in AS3

#2 chinchang  Icon User is offline

  • Indie Game Developer
  • member icon

Reputation: 192
  • View blog
  • Posts: 725
  • Joined: 22-December 08

Re: 2D Array in AS3

Posted 18 July 2011 - 03:18 AM

So what problem are you facing with the current code. Any compiler errors or not getting what you want ?? Mention them.
Was This Post Helpful? 0
  • +
  • -

#3 lordofduct  Icon User is offline

  • I'm a cheeseburger
  • member icon


Reputation: 2533
  • View blog
  • Posts: 4,633
  • Joined: 24-September 10

Re: 2D Array in AS3

Posted 18 July 2011 - 06:27 AM

Unless newer versions of AS3 which I haven't used have support for it... the straight 2D array as written in your code:

new Array(20,20)

isn't possible... instead that'd just be an array with 2 values in it.

You'd need to create what is called a jagged array (an array of arrays). Or create one array that is 20 * 20 (400) in length, and access on a rolling index.
Was This Post Helpful? 0
  • +
  • -

#4 NiteFlame  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 34
  • Joined: 11-June 10

Re: 2D Array in AS3

Posted 18 July 2011 - 12:24 PM

View Postlordofduct, on 18 July 2011 - 08:27 AM, said:

Unless newer versions of AS3 which I haven't used have support for it... the straight 2D array as written in your code:

new Array(20,20)

isn't possible... instead that'd just be an array with 2 values in it.

You'd need to create what is called a jagged array (an array of arrays). Or create one array that is 20 * 20 (400) in length, and access on a rolling index.


that straight array was part of a tutorial that i based my code off of but when i got to the end of the tutorial it told me "this is how you dnt do it" so i came here to c if there was a way to edit it to fix it.

so for the nested array would it look like this

public var row1 : array = new array [20];
public var row2 : array = new array [20];
public var row3 : array = new array [20];
public var row4 : array = new array [20];
.....
public var row20 : array = new array [20];
public var column [row1, row2, row3, row4, ....., row20];



this is for a tactics game so i need to be able to find the location of each unit and have them move correctly and i cnt seem to wrap my head around doing that with the nested array or the 400 length array.

also will i be able to still add random terrain to the nest array with my current code or will that need to be completely redone
Was This Post Helpful? 0
  • +
  • -

#5 lordofduct  Icon User is offline

  • I'm a cheeseburger
  • member icon


Reputation: 2533
  • View blog
  • Posts: 4,633
  • Joined: 24-September 10

Re: 2D Array in AS3

Posted 18 July 2011 - 02:07 PM

that's from a tutorial that said...

"this is how you dont do it"

get a new tutorial (also, can I see this tutorial...?)
Was This Post Helpful? 0
  • +
  • -

#6 NiteFlame  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 34
  • Joined: 11-June 10

Re: 2D Array in AS3

Posted 18 July 2011 - 06:02 PM

the tutorial isnt on D.I.C. it was a random one i found while searching google for Flash as3 2d array
Was This Post Helpful? 0
  • +
  • -

#7 NiteFlame  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 34
  • Joined: 11-June 10

Re: 2D Array in AS3

Posted 18 July 2011 - 06:14 PM

var array:Array = new Array();
array[0] = ["cat.jpg", "1", "Here is a picture of a cat"];
array[1] = ["dog.jpg", "2", "Here is a picture of a dog"];
array[2] = ["frog.jpg", "3", "Here is a picture of a frog"];

To access the data, use array[0][0] to access “cat.jpg”, array[1][1] to access “2″, and array[2][2] to access “Here is a picture of a frog”.



found this by digging around and it looks like the easiest way to explain a 2D array in flash but what happens when i want a 3D array now from here anyone got any ideas
Was This Post Helpful? 0
  • +
  • -

#8 lordofduct  Icon User is offline

  • I'm a cheeseburger
  • member icon


Reputation: 2533
  • View blog
  • Posts: 4,633
  • Joined: 24-September 10

Re: 2D Array in AS3

Posted 19 July 2011 - 07:17 AM

3d array would just be an array of arrays of arrays...

note though, it gets very complicated to manage all the arrays out at that dimension... a lot of the time there's an easier way to manage data then with 3d arrays (especially 3d jagged arrays seeing as flash doesn't support 3d straight arrays).
Was This Post Helpful? 0
  • +
  • -

#9 NiteFlame  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 34
  • Joined: 11-June 10

Re: 2D Array in AS3

Posted 20 July 2011 - 12:27 PM

i got the code some what working
it makes a 2d array and and sets and re-sizes the imgs and puts everything how it should
but when i run the program i only have 5 squares at the end of my board
so it would seem instead of making a new terrain square for each [i][j] its just moving them
, and when i try to do "TerrainArray[i][j] = new grass" it gives me the error of "Call to a possibly undefined method Grass"
idk where to go from here anyone got any ideas?
package  {
	
	import flash.display.*;
	import flash.events.*;
	
	public class TerrainSetUp extends MovieClip{
		
		public var Snow : MovieClip = new MovieClip;
		public var Grass : MovieClip = new MovieClip;
		public var Sand : MovieClip = new MovieClip;
		public var Rocky : MovieClip = new MovieClip;
		public var Water : MovieClip = new MovieClip;
		public var RanNum : Number = 0;
		public var i : Number = 0;
		public var j : Number = 0;
		public var TerrainArray :Array = new Array(20 , 20);
		public var Set : Boolean = true;
		public var TempTerrain : MovieClip = new MovieClip;
		
		public function TerrainSetUp() {
			Snow = new SnowImg;
			Grass = new GrassImg;
			Sand = new SandImg;
			Rocky = new RockyImg;
			Water = new WaterImg;
			
			TempTerrain = new GrassImg;
			
			Snow.scaleX = .25;
			Grass.scaleX = .25;
			Sand.scaleX = .25;
			Rocky.scaleX = .25;
			Water.scaleX = .25;
			
			Snow.scaleY = .25;
			Grass.scaleY = .25;
			Sand.scaleY = .25;
			Rocky.scaleY = .25;
			Water.scaleY = .25;
			
			if(Set == true){
			   Set = false
			   SetTheTerrain();
			   }
			
			
		}
		
		public function SetTheTerrain():void{
			
			for( i = 0 ; i < 30 ; i ++){
				TerrainArray[i] = [TempTerrain, TempTerrain, TempTerrain, TempTerrain, TempTerrain, TempTerrain, TempTerrain, TempTerrain, TempTerrain, TempTerrain, TempTerrain, TempTerrain, TempTerrain, TempTerrain, TempTerrain, TempTerrain, TempTerrain, TempTerrain, TempTerrain, TempTerrain, TempTerrain, TempTerrain, TempTerrain, TempTerrain, TempTerrain, TempTerrain, TempTerrain, TempTerrain, TempTerrain, TempTerrain]
			}
			SetTheTerrain2();
		}
		public function SetTheTerrain2():void{
			for( i = 0 ; i < 20 ; i ++){
				RanNum = Math.floor(Math.random() * (5))
				trace (i,j)
				if(RanNum == 0){
					TerrainArray[i][j] = Grass;
					TerrainArray[i][j].x = (i * 50);
					TerrainArray[i][j].y = (j * 50);
					addChild(TerrainArray[i][j]);
				}
				if(RanNum == 1){
					TerrainArray[i][j] = Snow;
					TerrainArray[i][j].x = (i * 50);
					TerrainArray[i][j].y = (j * 50);
					addChild(TerrainArray[i][j]);
				}
				if(RanNum == 2){
					TerrainArray[i][j] = Rocky;
					TerrainArray[i][j].x = (i * 50);
					TerrainArray[i][j].y = (j * 50);
					addChild(TerrainArray[i][j]);
				}
				if(RanNum == 3){
					TerrainArray[i][j] = Water;
					TerrainArray[i][j].x = (i * 50);
					TerrainArray[i][j].y = (j * 50);
					addChild(TerrainArray[i][j]);
				}
				if(RanNum == 4){
					TerrainArray[i][j] = Sand;
					TerrainArray[i][j].x = (i * 50);
					TerrainArray[i][j].y = (j * 50);
					addChild(TerrainArray[i][j]);
				}else{
					TerrainArray[i][j] = Grass;
					TerrainArray[i][j].x = (i * 50);
					TerrainArray[i][j].y = (j * 50);
					addChild(TerrainArray[i][j]);
				}
				if(i == 10){
					i = 0
					j++;
					if( j == 11){
						break;
					}
				}
			}
		}
	}
	
}



Was This Post Helpful? 0
  • +
  • -

Page 1 of 1