Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 02-10-2012, 06:54 PM   #1
cf2004
Registered User
 
Join Date: Aug 2010
Posts: 36
Default using an array cross class

Hi all

I have the following code

Main

Code:
package 
{
	import ArrayClass;
	import flash.display.MovieClip;

	public class stuff extends MovieClip
	{
		private var _ac:ArrayClass;
		
		public function stuff() 
		{
			_ac = new ArrayClass();
			other();


		public function other()
		{
			var temp:MovieClip;
			for (var j:Number =0; j<this.numChildren; j++)
			{				
				temp = this.getChildAt(j) as MovieClip;
				_ac.addAC(temp);
			}
		}
	}
}
ArrayClass

Code:
package
{
	import flash.display.MovieClip;

	public class ArrayClass extends MovieClip
	{
		private var _ac:Array;
		
		public function ArrayClass()
		{
			_ac = [];
		}
		
		public function addAC($ac:MovieClip):void
		{
			_ac.push($ac);
		}
		
		public function getACArray():Array
		{
			return _ac;
		}
	}
}
Other Class has a function to getACArray

Code:
	_ac = new ArrayClass();
	var tempACArray:Array = _ac.getACArray();
	trace(tempACArray);
"trace(tempACArray);" always returns empty. I don't know why. I was expecting to return the array created in the main class
What am I doing wrong?

Thanks!
cf2004 is offline   Reply With Quote
Old 02-10-2012, 10:07 PM   #2
arkitx
Senior Member
 
Join Date: Jan 2011
Posts: 703
Default

ActionScript Code:
_ac = new ArrayClass();     var tempACArray:Array = _ac.getACArray();     trace(tempACArray);


Where you put this Code. This might trace before pushing to the array.

Post your project in zip CS4 CS3 version.




arkitx
arkitx is offline   Reply With Quote
Old 02-11-2012, 12:29 AM   #3
cf2004
Registered User
 
Join Date: Aug 2010
Posts: 36
Default

Hi, thanks for your reply. I'm confused with your code now, I thought the issue is in ArrayClass not in the OtherClass. Inside the ArrayClass, one public function 'addAC' populates the array and I traced it and looks OK, and later in time, 'return _ac' returns empty, in the same class.

My goal is to create a class (ArrayClass) that can be accessed and updated from/to other classes (like Main and Other classes)

Code:
public function addAC($ac:MovieClip):void
{
	_ac.push($ac); //ALL OK
}
public function getACArray():Array
{
	return _ac; //RETURNS EMTPY
}
cf2004 is offline   Reply With Quote
Old 02-11-2012, 12:23 PM   #4
arkitx
Senior Member
 
Join Date: Jan 2011
Posts: 703
Default

I suggest you to read Observer Pattern and also Custom EventDispatcher and Broadcaster for your solution.
__________________
Regards

arkitx

Last edited by arkitx; 02-11-2012 at 09:42 PM.
arkitx is offline   Reply With Quote
Old 02-11-2012, 09:09 PM   #5
cf2004
Registered User
 
Join Date: Aug 2010
Posts: 36
Default

I just realized that the Other Class doesn't need to see the Array Class, it will complicate unnecessarily. What Other Class needs is to change a flag that can be read in the Main Class, and based on that Main Class and Array Class will happily interact with each other . So I'll just update my code accordingly...
cf2004 is offline   Reply With Quote
Old 02-11-2012, 09:37 PM   #6
arkitx
Senior Member
 
Join Date: Jan 2011
Posts: 703
Default

This is a conceptual code of Observer:

ActionScript Code:
var observers:Array = new Array(); function addObserver(o:MovieClip):Boolean {     if (o == null) {         return false;     }     for (var i:Number = 0; i<observers.length; i++) {         if (observers[i] == o) {             return false;         }     }     observers.push(o);     return true; } var sq:Square; for (var i = 0; i<16; i++) {     sq = new Square();     addChild(sq);     sq.name="Square"+i;     sq.x = 100+(sq.width+3)*i;     sq.y = sq.height;     sq.addEventListener(MouseEvent.CLICK,update);     trace(addObserver(sq)); } function update(evt:MouseEvent):void {     evt.target.y -=evt.target.y-2;     notifyObservers(MovieClip(evt.target)); } function notifyObservers(o:MovieClip) {     for (var i = 0; i<observers.length; i++) {         if (observers[i] == o) {             trace(observers[i].name);             for (var j = 0; j<observers.length; j++) {                 if (observers[j] != o) {                     observers[j].y =  o.y+o.height-2;                 }             }         }     } }

Post your code so that I can have a look of your setup.
__________________
Regards

arkitx

Last edited by arkitx; 02-11-2012 at 10:09 PM.
arkitx is offline   Reply With Quote
Old 02-10-2012, 10:21 PM   #7
cf2004
Registered User
 
Join Date: Aug 2010
Posts: 36
Default

Thanks for your reply! I just noticed my problem is here

Code:
public function addAC($ac:MovieClip):void
		{
			_ac.push($ac); //ALL OK
		}
		
		public function getACArray():Array
		{
			return _ac; //RETURNS EMTPY
		}
I don't know why it returns empty. The getACArray() runs after populating the stage and after populating the _ac array, this function runs when I interact with an MC on stage.
But "return _ac" should have returned something, right?

thanks!
cf2004 is offline   Reply With Quote
Old 02-10-2012, 10:43 PM   #8
arkitx
Senior Member
 
Join Date: Jan 2011
Posts: 703
Default

ActionScript Code:
public function other() {             trace(this.numChildren);             var temp:MovieClip;             for (var j:Number =0; j<this.numChildren; j++) {                 temp = this.getChildAt(j) as MovieClip;                 _ac.addAC(temp);             }             trace(_ac.getACArray());         }


You might notice that the trace from here is tracing the value added Array.

You can dispatch an event from this point to listen for the COMPLETE Event.



arkitx
arkitx is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 08:33 AM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.