Solved

problem passing a variable from one function to another AS3

Posted on 2008-06-21
4
2,054 Views
Last Modified: 2013-11-11
Hi
it is my first try of writing in AS3 in a class format. so i`m building a gallery and i built a grid so now i wanted to insert the dynamic values in. i bring the xml into flash (it works) and i want the number of "images" to take its value from the xml. but i cannot make it work.
i have the value from the xml store in a variable but i cannot take it and pass it into another method.

i attach my code as a whole because maybe the problem is with my writing of the class.

best regards

ron
package{

	

	import flash.display.MovieClip;

	import flash.net.URLLoader;

	import flash.net.URLRequest;

	import flash.display.Loader;

	import flash.events.Event;

	import flash.display.Sprite;

	

	

	public class SGclass extends MovieClip{

		

		private var siteLoader:URLLoader;

		private var siteRequest:URLRequest;

		private var imageLoader:Loader;

		private var doxml:XML;

		private var boxNum:int;

		private var cols:int= 6;

		private var rows:int = Math.ceil(boxNum / cols);

		private var boxCount:int = 0;

		private var box:Sprite;

		

		

		public function SGclass(){

			

			siteLoader = new URLLoader();

			siteRequest = new URLRequest("sites2.xml");

			siteLoader.load(siteRequest);

			

			siteLoader.addEventListener(Event.COMPLETE , takesites);

			placeImages();

			//trace(boxNum);

			

		}

		

		

		public function takesites (e:Event):void {

			

			doxml = XML(e.target.data);

			//trace(doxml.site);

			

			boxNum = doxml.site.length();

			//trace(boxNum);

			

			

		

		}

		

	private function placeImages(){

			

			for (var py:int = 0; py < rows; py ++) {

				for (var px:int = 0 ; px < cols ; px ++) {

					//see that you are not exiding the boxNum variable

					if(boxCount < boxNum){

						box= new Sprite();

						box.graphics.beginFill(0xCCCCCC);

						box.graphics.drawRoundRect(0,0,80,80,15,15);

						box.graphics.endFill();

						box.buttonMode = true;

						box.x = 100 + px * (box.width + 10);

						box.y = 40 + py * (box.height+ 10);

						addChild(box);

						boxCount++;

						

						}

					}

				}

				

			
 

		}
 

		

		

	}

	

}

Open in new window

0
Comment
Question by:derrida
  • 2
  • 2
4 Comments
 
LVL 34

Expert Comment

by:Aneesh Chopra
ID: 21839841
problem is you are callling "placeImages()" function just with XML load call, which is not correct.
XML load is an asyncronous function that so we have 'complete' event to check when XML gets loaded and data is available for use.

solution is:
You should call "placeImages()" function after XML get loaded.
below is the updated code which should work, if it does not, I suggest to upload your sample source with XML file for review:


package{

	

	import flash.display.MovieClip;

	import flash.net.URLLoader;

	import flash.net.URLRequest;

	import flash.display.Loader;

	import flash.events.Event;

	import flash.display.Sprite;

	

	

	public class SGclass extends MovieClip{

		

		private var siteLoader:URLLoader;

		private var siteRequest:URLRequest;

		private var imageLoader:Loader;

		private var doxml:XML;

		private var boxNum:int;

		private var cols:int= 6;

		private var rows:int = Math.ceil(boxNum / cols);

		private var boxCount:int = 0;

		private var box:Sprite;

		

		

		public function SGclass(){

			

			siteLoader = new URLLoader();

			siteRequest = new URLRequest("sites2.xml");

			siteLoader.load(siteRequest);

			

			siteLoader.addEventListener(Event.COMPLETE , takesites);

			

			//trace(boxNum);

			

		}

		

		

		public function takesites (e:Event):void {

			

			doxml = XML(e.target.data);

			//trace(doxml.site);

			

			boxNum = doxml.site.length();

			//trace(boxNum);

			placeImages();		

		}

		

	private function placeImages(){

			

			for (var py:int = 0; py < rows; py ++) {

				for (var px:int = 0 ; px < cols ; px ++) {

					//see that you are not exiding the boxNum variable

					if(boxCount < boxNum){

						box= new Sprite();

						box.graphics.beginFill(0xCCCCCC);

						box.graphics.drawRoundRect(0,0,80,80,15,15);

						box.graphics.endFill();

						box.buttonMode = true;

						box.x = 100 + px * (box.width + 10);

						box.y = 40 + py * (box.height+ 10);

						addChild(box);

						boxCount++;

						

						}

					}

				}

				

			

 

		}

 

		

		

	}

	

}

Open in new window

0
 
LVL 1

Author Comment

by:derrida
ID: 21839856
hi
it still does not work. i uploaded the files here:
https://filedb.experts-exchange.com/incoming/ee-stuff/7187-mysitegalleryWithClass.zip

and can you explain why i should call that function there and not in the constructor please?


ron
0
 
LVL 34

Accepted Solution

by:
Aneesh Chopra earned 500 total points
ID: 21839906
Got the fix:

another problem was you were intializing variable 'rows' also before setting boxNum, and 'rows' value is dependent over 'boxNum' variable
so now I have intialized the variable 'rows' also after XML get loaded and all is working fine.

BELOW is the working code:

and
here I am explaining it again,
very basic logic is you can use a variable only once have its source available,
here you want to set the boxNum using XML, and XML is being loaded externally, so XML data will be available only once flash gets it loaded into memory.
and we have 'complete' event to know the XML is loaded,
hence we have to initiate the boxNum and rows variable inside 'takesites' function, because it gets executed when we have XML ready.

and 'placeImages()' function is also using variables 'boxNum' and 'rows' which are being set inside function 'takesites()' that so we have to call function 'placeImages()' only after setting variables 'boxNum' and 'rows'

I hope all would be clear.
package{

	

	import flash.display.MovieClip;

	import flash.net.URLLoader;

	import flash.net.URLRequest;

	import flash.display.Loader;

	import flash.events.Event;

	import flash.display.Sprite;

	

	

	public class SGclass extends MovieClip{

		

		private var siteLoader:URLLoader;

		private var siteRequest:URLRequest;

		private var imageLoader:Loader;

		private var doxml:XML;

		private var boxNum:int;

		private var cols:int= 6;

		private var rows:int;

		private var boxCount:int = 0;

		private var box:Sprite;

		

		

		public function SGclass(){

			

			siteLoader = new URLLoader();

			siteRequest = new URLRequest("sites2.xml");

			siteLoader.load(siteRequest);

			

			siteLoader.addEventListener(Event.COMPLETE , takesites);

			

			//trace(boxNum);

			

		}

		

		

		public function takesites (e:Event):void {

			

			doxml = XML(e.target.data);

			//trace(doxml.site);

			

			boxNum = doxml.site.length();

			rows = Math.ceil(boxNum / cols);

			placeImages();

			trace(boxNum);

		}

		

	private function placeImages(){

			

			for (var py:int = 0; py < rows; py ++) {

				for (var px:int = 0 ; px < cols ; px ++) {

					//see that you are not exiding the boxNum variable

					if(boxCount < boxNum){

						box= new Sprite();

						box.graphics.beginFill(0xCCCCCC);

						box.graphics.drawRoundRect(0,0,80,80,15,15);

						box.graphics.endFill();

						box.buttonMode = true;

						box.x = 100 + px * (box.width + 10);

						box.y = 40 + py * (box.height+ 10);

						addChild(box);

						boxCount++;

						

						}

					}

				}

				

			
 

		}
 

		

		

	}

	

}

Open in new window

0
 
LVL 1

Author Comment

by:derrida
ID: 21840016
Hi
thanks it works and i do get the explaination. while i`m writing AS not as class the flowing of the code is more natural for me then this sort of writing, but i hope to learn this type of writing.

thank you for your efforts.

best regards

ron
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Flash media server 1 327
Adobe Flash Player won't run in IE or Firefox but will in Chrome 20 327
Flash Player Action Script Error 1090 5 197
Removing Flash from Website 6 45
I come across a lot of question about how to access things in the document class from a movieclip, or accessing something from a movieclip in the document class. It took me a while to figure this out but once I did it makes life so much easier. …
Recently, I was asked to recommend a tracking system to be implemented on a clients website. As the entire site was built on flash, my first thought was to suggest custom built tracking system. However, our company at that point of time didn't h…
In this tutorial viewers will learn how to create a basic motion tween animation in Flash Open a new document in Flash: Draw/import an image: Press CTRL + F8 to convert it into a graphic symbol: Select a frame (how long you want the tween to last): …
The goal of the tutorial is to teach the user how to select the video input device. Make sure you have an input device that in connected and work and recognized by Adobe Flash Media Live Encoder and select it in the “video input” menu.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now