• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 259
  • Last Modified:

AS3 trouble communicating between classes

I have my xml being called in one class in an array, and I need to call a section of that array to be used as the url for a button.  If you refer to the code below I need to be able to use //  link': image.url.text()  // as the url that the clicked button will go to.  How do I get 'link' into URLRequest?  Merely using link in the URLRequest does not work.

 
//code that sets the array in one class
public static function loadFromNormalizedXml(i__xml:XML):CarouselData {
			var l__data:Array = new Array();


			for each (var image:* in i__xml.contents.image) {
				l__data.push({'title': image.title.text(), 'src': image.pic.text(), 'link': image.url.text(), 'description':image.description.text()});
				trace("image.url = " + image.url);
				var link:String = "link";
			}

			return new CarouselData(l__data);
		}


//code that calls the URLRequest in another class
m__containerDescription.addEventListener(MouseEvent.CLICK, ClickIt);
			function ClickIt(event:MouseEvent):void {
				navigateToURL(new URLRequest(link), "_self");
			}


//a snippet of the XML
<contents>
    <image>
      <title>
       <![CDATA[<b>Example Band1</b> Dec 20, 2009]]>
      </title>
      <pic>images/gallery/img1.jpg</pic><url>http://www.google.com</url>
      <description>For more information and to buy tickects click here</description>
    </image>
    <image>
      <title>
        <![CDATA[<b>Example Band2</b > Dec 20, 2009]]>
      </title>
      <pic>images/gallery/img2.jpg</pic><url>http://www.google.com</url>
      <description>For more information and to buy tickects click here</description>
    </image>
</content>

Open in new window

0
cubical38
Asked:
cubical38
  • 5
  • 4
1 Solution
 
ruidovisualCommented:
your var link is equal to "link" string, so you have to make link = image.url.text();

and you have to define link autside your function so you can use it later

so define

var link:String;

and in your function give it the proper value

link = image.url.text();

Hope it helps!

; )
0
 
cubical38Author Commented:
Thanks for the reply but unfortunately I am getting an error...

With this code below (class) i am getting the error:
1120: Access of undefined property link.
on line 18

also under that is where the function is being called in my main class.  I know it is working because the text fields and images are all being loaded correctly.  The problem is I need to be able to navugatToURL from the link = image.url.text();

Any ideas why it is undefined?  Because it is static?


//class to load the xml

package {

	public class CarouselData {
		public var m__data:Array;
		var link:String = new String();
		public function CarouselData(images:Array = null) {
			m__data = new Array();
			if (images!=null) {
				setImages(images);
				trace("images !");
				resetFlow();
			}
		}
		public static function loadFromNormalizedXml(i__xml:XML):CarouselData {
			var l__data:Array = new Array();
			for each (var image:* in i__xml.contents.image) {
				l__data.push({'title': image.title.text(), 'src': image.pic.text(), 'link': image.url.text(), 'description':image.description.text()});
				link=image.url.text();
			}
			return new CarouselData(l__data);
		}

		public function setImages(images:Array):void {
			m__data=images;
		}
		public function resetFlow():void {

		}
		public function addImage(src:String, additionalData:Object = null):void {
			if (additionalData==null) {
				additionalData = new Object();
			}
			additionalData.src=src;
			m__data.push(additionalData);
		}

		public function getImages():Array {
			return m__data;
		}
	}
}


//main class function

m__carousel.data=CarouselData.loadFromNormalizedXml(m__mainXML);

Open in new window

0
 
ruidovisualCommented:
make the link var public : )
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
cubical38Author Commented:
Yea I tried but alas I am still getting the error:
1120: Access of undefined property link.
link=image.url.text();
package {

	public class CarouselData {
		public var m__data:Array;
		public var link:String = new String();
		public function CarouselData(images:Array = null) {
			m__data = new Array();
			if (images!=null) {
				setImages(images);
				trace("images !");
				resetFlow();
			}
		}
		public static function loadFromNormalizedXml(i__xml:XML):CarouselData {
			var l__data:Array = new Array();
			for each (var image:* in i__xml.contents.image) {
				l__data.push({'title': image.title.text(), 'src': image.pic.text(), 'link': image.url.text(), 'description':image.description.text()});
				link=image.url.text();
			}
			return new CarouselData(l__data);
		}

		public function setImages(images:Array):void {
			m__data=images;
		}
		public function resetFlow():void {

		}
		public function addImage(src:String, additionalData:Object = null):void {
			if (additionalData==null) {
				additionalData = new Object();
			}
			additionalData.src=src;
			m__data.push(additionalData);
		}

		public function getImages():Array {
			return m__data;
		}
	}
}

Open in new window

0
 
ruidovisualCommented:
Do one of the following depending on your convinience:

1) Make the var link static (public static var link:String;)
2) or make the function loadFromNormalizedXml not static
0
 
cubical38Author Commented:
This all makes sence but the main issue I am having is being able to then call link in the main.as file as such:
m__containerDescription.addEventListener(MouseEvent.CLICK, ClickIt);
            function ClickIt(event:MouseEvent):void {
            navigateToURL(new URLRequest(link), "_self");
}

I thought it might be easier for you to just view my files so I have zipped all files that are involved in the loading of the xml and rendering it.  These are inherited files and I would have gone a different way, but no since in reinventing the wheel, especially with deadlines.  Thanks for all the help, hopefully if you have a chance to look at it you will see the simple solution.  Remove .txt of coarse...

Thanks Again!
Archive.zip.txt
0
 
ruidovisualCommented:
Can you send a CS3 version of the fla please?? ; )

I know what to do, may be you can achieve it.

You need to name your photos with an index (an integer), based on their position in the xml. You need to declare l__data array as a public var outside your loadFromNormalizedXml function and make that function non static. Then you push the values to the array as you are doing it now but the values are going to be accesible from outside the function since the array is outside too.

When you click an element you're going to ask it for it's name and retrieve the url from the array by passing the name as a reference, then navigate to it's url (all this in code below)

You're more used to the code, so may be you can tell me where you create each element, so we can name 'em.

Or post me a cs3 version. And the papervision libraries : )
m__containerDescription.addEventListener(MouseEvent.CLICK, ClickIt);
function ClickIt(event:MouseEvent):void {
   var elemIndex:int = parseInt(event.target.name);
   var thisURL:String = l__data[elemIndex].link;
   navigateToURL(new URLRequest(thisURL), "_self");
} 

Open in new window

0
 
cubical38Author Commented:
Thanks for all your help!  I was able to get it working but alas the client want something different now...  Big surprise.  Points awarded regardless!

Thanks!
0
 
cubical38Author Commented:
Thanks!
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now