Add/Subtract Items from an Array

Posted: January 31st, 2010 | Filed under: AS3, Flash | 6 Comments » Here is something I used for a sorting/filtering application. I needed a way to add and remove items from an array and have the button know whether or not it had been added. When a button is clicked, the event handler sends a boolean property to the clicked button setting it as added or not added. Because the button stores that property, when you click the button it can toggle it’s properties in and out of the array based on whether or not it has been added.

The basic flow is as follows. Create buttons based on an array and position them. Here our “_itemsArr” has five items. Set up an event listener for the buttons and store the boolean property in the button and update it when it has been clicked. Depending on whether the property is true or false, add or subtract that button’s label property to an array while updating the text area with the array.

Download Source

ArrayExample.as
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package
{
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import fl.controls.TextArea;
 
	public class ArrayExample extends Sprite
	{
		private static var buttonOffset:Number = 141;
		private var _itemsArr:Array = ["Earth", "Fire", "Wind", "Water", "Chicken"];
		private var _activeItemArr:Array = new Array;
 
		private var container:Sprite;
		private var traceArea:TextArea;
		private var button:ButtonClass;
 
		public function ArrayExample()
		{
			buildDisplay();
			positionDisplay();
		}
 
		private function buildDisplay():void
		{
			container = new Sprite();
			addChild(container);
			container.addEventListener(MouseEvent.MOUSE_DOWN, buttonHandler);
 
			var buttonPosition:Number = 0;
			for(var i:uint = 0; i < _itemsArr.length; i++)
			{
				button = new ButtonClass();
				container.addChild(button);
				button.label = _itemsArr[i];
				button.x = buttonPosition;
				buttonPosition += buttonOffset;
			}
 
			traceArea = new TextArea();
			addChild(traceArea);
		}
 
		private function positionDisplay():void
		{
			traceArea.x = container.x;
			traceArea.y = container.y + 30;
			traceArea.width = container.width;
		}
 
		private function buttonHandler(e:MouseEvent):void
		{
			var activeItem:ButtonClass = e.target as ButtonClass;
 
			if(!activeItem.added){
				addToArray(e.target.label);
				activeItem.added = true;
			} else { 
				removeFromArray(e.target.label);
				activeItem.added = false;
			}
 
			traceArea.text = String(_activeItemArr);
		}
 
		private function addToArray(type:String)
		{
			var dupNumber:Number = 0;
			_activeItemArr.push(type);
 
			for (var i : int = 0; i < _activeItemArr.length; i++) {
				if (_activeItemArr[i] == type) dupNumber++;
			}
 
			if (dupNumber > 1) _activeItemArr.pop();
		}
 
		private function removeFromArray(type:String)
		{
			for (var i : int = 0; i < _activeItemArr.length; i++) {
				if (_activeItemArr[i] == type) _activeItemArr.splice(i,1);
			}
		}
 
 
	}
}
ButtonClass.as
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package
{
	import fl.controls.Button;
 
	public class ButtonClass extends Button
	{
		private var _added:Boolean = false;
 
		public function get added():Boolean
		{
			return _added;
		}
 
		public function set added(added:Boolean):void
		{
			_added = added;
		}
 
		public function ButtonClass(){}
 
	}
}

6 Comments on “Add/Subtract Items from an Array”

  1. 1 dental hygienist said at 6:40 pm on April 18th, 2010:

    This is such a great resource that you are providing and you give it away for free. I enjoy seeing websites that understand the value of providing a prime resource for free. I truly loved reading your post. Thanks!

  2. 2 Emily said at 12:17 pm on June 2nd, 2010:

    great post as usual!

  3. 3 Amy said at 8:58 am on June 5th, 2010:

    This is such a great resource that you are providing and you give it away for free. I enjoy seeing websites that understand the value of providing a prime resource for free. I truly loved reading your post. Thanks!

  4. 4 student grants said at 11:16 pm on June 9th, 2010:

    Great, I never knew this, thanks.

  5. 5 minority scholarship said at 4:36 am on June 29th, 2010:

    Keep up the good work, I like your writing.

  6. 6 maranki sifali bitkiler said at 5:19 pm on June 30th, 2010:

    Hi, A fantastic blog, I have to admit this is really well thought out, this forum definitely needs bloggers like you. Filling the place with some good tips and information, I did follow A couple of your posts, they been relevant and important points were elaborated. I must say we should always be ready to post in our best knowledge to aid people. Really love your posting.


Leave a Reply