Arrays: Simple tools of the trade. (AS2 & AS3 style)

16 Jul
The following code is in ActionScript 2.
_root.myArr = [1,2,3,4,5];
_root.myArr.push(6);
//This should result in "1,2,3,4,5,6"
trace(myArr);
_root.myArr.splice(1,2,"X","Y");
//This should result in "1,X,Y,4,5,6"
trace(myArr);

Simply put, in ActionScript 2, an array can be made
as simply as just creating a name (like "myArr") and
making it equal a bracket of preset values.
However, unlike languages like Java
(arrays... not ArrayLists),
arrays in ActionScript2 and ActionScript3 can "pushed".
This means you can place values at the end of the array.
This is incredibly helpful for situations where you
need to group a set of values together.
Especially values that are created mid-game.
(Think particles which is a future topic)

The following code is for ActionScript 3.

package {
	import flash.display.Sprite;
	public class MainDoc extends Sprite {
		private var myArr:Array = new Array(1,2,3,4,5);
		public function MainDoc() {
			myArr.push(6);
			trace("After pushing '6': " + myArr);
			myArr.shift();
			trace("After Shift: " + myArr);
			//shift removes the first value
			myArr.unshift(1);
			trace("After Unshift of 1: " + myArr);
			//by UNshifting a value (this time it happened to be the original first value of '1')
			//you put it at the front of the array.
			trace("Popped: " + myArr.pop());
			//Pop is a method that removes the last value of the array ('6') and returns it.

			//You can find more methods/properties of arrays in the link below!
		}
	}
}

The link for more array fun. But I'm not quite sure
if all these methods/properties would work in AS2.
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/Array.html

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: