[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 574
  • Last Modified:

How to set a image link from xml to Flash AS3

Hello,

with XML i load some text and images into Flash (AS3). Now i need to extend the structure with a link. In XML i have added a <link> attribute. But i do not know how to extend the AS3 code. This link should belong to the loaded image. That menas, if you click the image, the URL loads.

Please can somebody help me?

See my added code

import caurina.transitions.*;

var loader:URLLoader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.TEXT;
loader.addEventListener(Event.COMPLETE, onLoadXML);
loader.load(new URLRequest("flash_home.xml"));
function onLoadXML(ev:Event){
   try{
		var myXML:XML = new XML(ev.target.data);
		var list:XMLList = myXML..title;
		for(var i=0; i<list.length(); i++){
			//trace(list[i].@name+"-"+list[i].comments+" - "+list[i].image)
			this["Title_txt"+i].text = list[i].@name;
			this["Title_txt"+i].text = this["Title_txt"+i].text.toUpperCase();
			this["Title_txt"+i].autoSize = TextFieldAutoSize.LEFT;
			this["Comments_txt"+i].text = list[i].comments;
			this["Comments_txt"+i].text = this["Comments_txt"+i].text.toUpperCase();
			this["Comments_txt"+i].autoSize = TextFieldAutoSize.LEFT;
			var loader:Loader = new Loader();
			this["holder_mc"+i].addChild(loader);
			loader.load(new URLRequest(list[i].image))	;	
		}
	} catch (e:TypeError){
		trace("Could not parse the XML");
		trace(e.message);
	}
}

/// LINKS

holder_mc0.buttonMode = true;
holder_mc0.addEventListener(MouseEvent.CLICK, callBig);
function callBig(event: MouseEvent) {
var request:URLRequest = new URLRequest("http://flashgameu.com");
navigateToURL(request, '_self');
} 

holder_mc1.buttonMode = true;
holder_mc1.addEventListener(MouseEvent.CLICK, callSide1);
function callSide1(event: MouseEvent) {
var request:URLRequest = new URLRequest("http://flashgameu.com");
navigateToURL(request, '_self');
} 

holder_mc2.buttonMode = true;
holder_mc2.addEventListener(MouseEvent.CLICK, callSide2);
function callSide2(event: MouseEvent) {
var request:URLRequest = new URLRequest("http://flashgameu.com");
navigateToURL(request, '_self');
}

holder_mc3.buttonMode = true;
holder_mc3.addEventListener(MouseEvent.CLICK, callSide3);
function callSide3(event: MouseEvent) {
var request:URLRequest = new URLRequest("http://flashgameu.com");
navigateToURL(request, '_self');
}

Open in new window


Thank´s!

Best,
druplash
0
druplash
Asked:
druplash
  • 17
  • 8
  • 8
1 Solution
 
deepanjandasCommented:
try this:
 
for(var i=0; i<list.length(); i++){
                        //trace(list[i].@name+"-"+list[i].comments+" - "+list[i].image)
                        this["Title_txt"+i].text = list[i].@name;
                        this["Title_txt"+i].text = this["Title_txt"+i].text.toUpperCase();
                        this["Title_txt"+i].autoSize = TextFieldAutoSize.LEFT;
                        this["Comments_txt"+i].text = list[i].comments;
                        this["Comments_txt"+i].text = this["Comments_txt"+i].text.toUpperCase();
                        this["Comments_txt"+i].autoSize = TextFieldAutoSize.LEFT;
                        var loader:Loader = new Loader();
                        this["holder_mc"+i].addChild(loader);
                        loader.load(new URLRequest(list[i].image)) ;
                        loader.addEventListener( MouseEvent.CLICK, function( e:MouseEvent):void{
var request:URLRequest = new URLRequest(list[i].link);//parse the xml based on the link attribute or tag
navigateToURL(request, '_self');
}, false, 0, true );    ;       
                }

Open in new window


Warm Regards
Deepanjan Das
0
 
druplashAuthor Commented:
Hi,

thank´s for quick reply! But it do not work. Please see my attached code:

var loader:URLLoader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.TEXT;
loader.addEventListener(Event.COMPLETE, onLoadXML);
loader.load(new URLRequest("flash_home.xml"));
function onLoadXML(ev:Event){
   try{
		var myXML:XML = new XML(ev.target.data);
		var list:XMLList = myXML..title;
		for(var i=0; i<list.length(); i++){
                        //trace(list[i].@name+"-"+list[i].comments+" - "+list[i].image)
                        this["Title_txt"+i].text = list[i].@name;
                        this["Title_txt"+i].text = this["Title_txt"+i].text.toUpperCase();
                        this["Title_txt"+i].autoSize = TextFieldAutoSize.LEFT;
                        this["Comments_txt"+i].text = list[i].comments;
                        this["Comments_txt"+i].text = this["Comments_txt"+i].text.toUpperCase();
                        this["Comments_txt"+i].autoSize = TextFieldAutoSize.LEFT;
                        var loader:Loader = new Loader();
                        this["holder_mc"+i].addChild(loader);
                        loader.load(new URLRequest(list[i].image)) ;
                        loader.addEventListener( MouseEvent.CLICK, function( e:MouseEvent):void{
						var request:URLRequest = new URLRequest(list[i].link);//parse the xml based on the link attribute or tag
						navigateToURL(request, '_self');
						}, false, 0, true );         
                		}
	} catch (e:TypeError){
		trace("Could not parse the XML");
		trace(e.message);
	}
}

Best,
druplash

Open in new window

0
 
druplashAuthor Commented:
I get this error. Sorry...

TypeError: Error #1010: Ein Begriff ist nicht definiert und hat keine Eigenschaften.
      at Function/<anonymous>()
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.

 
deepanjandasCommented:
That might be due to link tag placement in xml

Kindly show me your xml

Warm Regards
Deepanjan Das
0
 
druplashAuthor Commented:
Sure. Thank´s

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE data[
	<!ELEMENT title (comments, image)>
	<!ATTLIST title name CDATA #REQUIRED>
	<!ELEMENT comments (#PCDATA)>
	<!ELEMENT image (#PCDATA)>
]>
<data>
	<title name="Styling by Strassl">
		<comments>You look fabulous</comments>
		<image>images/big.jpg</image>
		<link>http://www.xxxxx.at</link>
	</title>
	<title name="Strassl News">
		<comments>XXXXXX</comments>
		<image>images/side1.jpg</image>
		<link>http://www.xxxxx.at</link>
	</title>
	<title name="Side 2">
		<comments>xxxxx</comments>
		<image>images/side2.jpg</image>
		<link>http://www.xxxxx.at</link>
	</title>
	<title name="Side 3">
		<comments>xxxxx</comments>
		<image>images/side3.jpg</image>
		<link>http://www.xxxxx.at</link>
	</title>
</data>

Open in new window

0
 
deepanjandasCommented:
Update the line as this:
trace(String(list[i].link));//see if this trace is coming properly.
loader.addEventListener( MouseEvent.CLICK, function( e:MouseEvent):void{
       var request:URLRequest = new URLRequest( String(list[i].link));//parse the xml based on the link attribute or tag
       navigateToURL(request, '_self');
   }, false, 0, true );

Open in new window


Warm Regards
Deepanjan Das
0
 
druplashAuthor Commented:
Thank´s!
The trace seems to be correct. But if i click the images i get still the error:

http://www.xxxxx.at
http://www.xxxxx.at
http://www.xxxxx.at
http://www.xxxxx.at
TypeError: Error #1010: Ein Begriff ist nicht definiert und hat keine Eigenschaften.
      at Function/<anonymous>()
0
 
deepanjandasCommented:
Is there any way you can just forward the part fla which loops and creates the images so that its easier for me to debug as I see no errors in the code, may be I am missing something.

Warm Regards
Deepanjan Das
0
 
druplashAuthor Commented:
This the whole code. I can´t attach the fla, get the following error:
The extension of one or more files in the archive is not in the list of allowed extensions: home.xfl


import caurina.transitions.*;

var loader:URLLoader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.TEXT;
loader.addEventListener(Event.COMPLETE, onLoadXML);
loader.load(new URLRequest("flash_home.xml"));
function onLoadXML(ev:Event){
   try{
		var myXML:XML = new XML(ev.target.data);
		var list:XMLList = myXML..title;
		for(var i=0; i<list.length(); i++){
                        //trace(list[i].@name+"-"+list[i].comments+" - "+list[i].image)
                        this["Title_txt"+i].text = list[i].@name;
                        this["Title_txt"+i].text = this["Title_txt"+i].text.toUpperCase();
                        this["Title_txt"+i].autoSize = TextFieldAutoSize.LEFT;
                        this["Comments_txt"+i].text = list[i].comments;
                        this["Comments_txt"+i].text = this["Comments_txt"+i].text.toUpperCase();
                        this["Comments_txt"+i].autoSize = TextFieldAutoSize.LEFT;
                        var loader:Loader = new Loader();
                        this["holder_mc"+i].addChild(loader);
						this["holder_mc"+i].buttonMode = true;
                        loader.load(new URLRequest(list[i].image)) ;
                        trace(String(list[i].link));//see if this trace is coming properly.
loader.addEventListener( MouseEvent.CLICK, function( e:MouseEvent):void{
       var request:URLRequest = new URLRequest( String(list[i].link));//parse the xml based on the link attribute or tag
       navigateToURL(request, '_self');
   }, false, 0, true );     
                }
	} catch (e:TypeError){
		trace("Could not parse the XML");
		trace(e.message);
	}
}

this.holder_mc0.addEventListener(flash.events.MouseEvent.ROLL_OVER, this.tintButtonOver);
this.holder_mc0.addEventListener(flash.events.MouseEvent.ROLL_OUT, this.tintButtonOut);

this.holder_mc1.addEventListener(flash.events.MouseEvent.ROLL_OVER, this.tintButtonOver);
this.holder_mc1.addEventListener(flash.events.MouseEvent.ROLL_OUT, this.tintButtonOut);

this.holder_mc2.addEventListener(flash.events.MouseEvent.ROLL_OVER, this.tintButtonOver);
this.holder_mc2.addEventListener(flash.events.MouseEvent.ROLL_OUT, this.tintButtonOut);

this.holder_mc3.addEventListener(flash.events.MouseEvent.ROLL_OVER, this.tintButtonOver);
this.holder_mc3.addEventListener(flash.events.MouseEvent.ROLL_OUT, this.tintButtonOut);

function tintButtonOver(arg1:flash.events.Event):void {
	var loc1:*=flash.display.MovieClip(arg1.currentTarget);
	caurina.transitions.Tweener.addTween(loc1, {"alpha":0.6, "time":1.2, "transition":"easeoutsine"});
	return;
}
		
function tintButtonOut(arg1:flash.events.Event):void {
	var loc1:*=flash.display.MovieClip(arg1.currentTarget);
	caurina.transitions.Tweener.addTween(loc1, {"alpha":1, "time":1.2, "transition":"easeoutsine"});
	return;
}

Open in new window

0
 
deepanjandasCommented:
Save the fla as CS4 and then kindly attach.

Warm Regards
Deepanjan Das
0
 
druplashAuthor Commented:
Thank´s for the info! home.fla
0
 
deepanjandasCommented:
Here you go:
function onLoadXML(ev:Event){
   try{
		var myXML:XML = new XML(ev.target.data);
		var list:XMLList = myXML..title;
		for(var i=0; i<list.length(); i++){
                        //trace(list[i].@name+"-"+list[i].comments+" - "+list[i].image)
                        this["Title_txt"+i].text = list[i].@name;
                        this["Title_txt"+i].text = this["Title_txt"+i].text.toUpperCase();
                        this["Title_txt"+i].autoSize = TextFieldAutoSize.LEFT;
                        this["Comments_txt"+i].text = list[i].comments;
                        this["Comments_txt"+i].text = this["Comments_txt"+i].text.toUpperCase();
                        this["Comments_txt"+i].autoSize = TextFieldAutoSize.LEFT;
                        var loader:Loader = new Loader();
                        this["holder_mc"+i].addChild(loader);
						this["holder_mc"+i].buttonMode = true;
                        loader.load(new URLRequest(list[i].image)) ;
                        var link:String = (String(list[i].link));
loader.addEventListener( MouseEvent.CLICK, function( e:MouseEvent):void{
       var request:URLRequest = new URLRequest( link);//parse the xml based on the link attribute or tag
       navigateToURL(request, '_self');
   }, false, 0, true );     
                }
	} catch (e:TypeError){
		trace("Could not parse the XML");
		trace(e.message);
	}
}

Open in new window


Warm Regards
Deepanjan Das
0
 
druplashAuthor Commented:
Thank´s it works perfect! You´re great!!
Have a nice day.

Best,
druplash
0
 
druplashAuthor Commented:
Sorry, it do not work correctly. It opens on every image the same url. The last xml entry url.

Please can you check this?

Thank´s
0
 
deepanjandasCommented:
Pls check, all your links are same I guess.

Warm Regards
Deepanjan Das
0
 
druplashAuthor Commented:
no they are not, definitly
0
 
deepanjandasCommented:
Yeah you are right:
Here you go:
function onLoadXML(ev:Event){
   try{
		var myXML:XML = new XML(ev.target.data);
		var list:XMLList = myXML..title;
		for(var i=0; i<list.length(); i++){
                        //trace(list[i].@name+"-"+list[i].comments+" - "+list[i].image)
                        this["Title_txt"+i].text = list[i].@name;
                        this["Title_txt"+i].text = this["Title_txt"+i].text.toUpperCase();
                        this["Title_txt"+i].autoSize = TextFieldAutoSize.LEFT;
                        this["Comments_txt"+i].text = list[i].comments;
                        this["Comments_txt"+i].text = this["Comments_txt"+i].text.toUpperCase();
                        this["Comments_txt"+i].autoSize = TextFieldAutoSize.LEFT;
                        var loader:Loader = new Loader();
                        this["holder_mc"+i].addChild(loader);
						this["holder_mc"+i].buttonMode = true;
                        var link:String = (String(list[i].link));
						this["holder_mc"+i].link = link;
						//trace(link);
						loader.addEventListener( MouseEvent.CLICK, function( e:Event):void{
							  var request:URLRequest = new URLRequest( e.currentTarget.parent.link );//parse the xml based on the link attribute or tag
							  navigateToURL(request, '_self');
							
						   }, false, 0, true );
						loader.load(new URLRequest(list[i].image)) ;
                }
	} catch (e:TypeError){
		trace("Could not parse the XML");
		trace(e.message);
	}
}

Open in new window


Warm Regards
Deepanjan Das
0
 
dgofmanCommented:
The error "TypeError: Error #1010: Ein Begriff ist nicht definiert und hat keine Eigenschaften.
      at Function/<anonymous>() "

You are getting if sme fields are undefined. You have to validate before setting any values to your variable.

Also you have to attach link to your compoent otherwice you will run problem by clicking on your image you will execute last URL

I will suggest to clean your code and create custom Locader class

Example:

ImageLoader.as

package{
	import flash.display.Loader;

	public class ImageLoader extends Loader{
		public var link:String;
		public var image:String;
	}
}

Open in new window



your ActionScript code is

import ImageLoader;

function updateTitle(index:uint, xml:XML):void{
	if(this["Title_txt" + index] == undefined){
		trace("Component Title_txt" + index + " is undefined");
	}else{
		this["Title_txt"+index].text = xml.@name;
	    this["Title_txt"+index].text = this["Title_txt"+index].text.toUpperCase();
	    this["Title_txt"+index].autoSize = TextFieldAutoSize.LEFT;
	}
}

function updateComments(index:uint, xml:XML):void{
	if(this["Comments_txt" + index] == undefined){
		trace("Component Comments_txt" + index + " is undefined");
	}else{
		this["Comments_txt"+index].text = xml.@comments;
	    this["Comments_txt"+index].text = this["Comments_txt"+index].text.toUpperCase();
	    this["Comments"+index].autoSize = TextFieldAutoSize.LEFT;
	}
}

function updateImage(index:uint, xml:XML):void{
	if(this["holder_mc" + index] == undefined){
		trace("Component holder_mc" + index + " is undefined");
	}else{
		var loader:ImageLoader = new ImageLoader();
		loader.image = xml.image;
		loader.link = xml.link;
		loader.load(new URLRequest(loader.lin));
		loader.addEventListener(MouseEvent.CLICK, function(event:MouseEvent):void{
			var link:String = event.target.link;
			trace(link);
			navigateToURL(new URLRequest(link), '_self');
		});
		this["holder_mc" + index].addChild(loader);
	}
}

var loader:URLLoader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.TEXT;
loader.addEventListener(Event.COMPLETE, onLoadXML);
loader.load(new URLRequest("flash_home.xml"));
var _this:Object = this;
function onLoadXML(ev:Event){
	try{
		var myXML:XML = new XML(ev.target.data);
		var list:XMLList = myXML..title;
		for(var i = 0; i < list.length(); i++){
			var title:XML = list[i];
			_this.updateTitle(i, title);
			_this.updateComments(i, title);
			_this.updateImage(i, title);
		}
	} catch (e:TypeError){
		trace("Could not parse the XML");
		trace(e.message);
	}
}

Open in new window

0
 
druplashAuthor Commented:
@deepanjandas:
Again thanks for the renewed correction.

@dgofman:
Thank you too for helping. Sorry but i have gave the points before you answer.
But i´m very interested in your solution too. I have tried it, but i get an error at compiling:

Could not parse the XML
Error #1010: Ein Begriff ist nicht definiert und hat keine Eigenschaften.

Please are you so helpful to look at the code again?

Thank you both for helping me!

Best,
druplash
0
 
dgofmanCommented:
I am fine you didn't give me points @deepanjandas did very good job.

But as I already mention in his solution he and you are not validating instances of exisitng component.

use my code and you will safe from this error.
0
 
druplashAuthor Commented:
Thank´s!
I have used your code but at compiling i get this following error:

Could not parse the XML
Error #1010: Ein Begriff ist nicht definiert und hat keine Eigenschaften.
0
 
dgofmanCommented:
Please zip your project (include fla and images) rename to png and attach to this thread
0
 
druplashAuthor Commented:
OK here you are. Thank´s again!
Archiv.zip
0
 
dgofmanCommented:
I don't see any errors

Styling by Strassl-You look fabulous - images/big.jpg - http://www.facebook.at
Strassl News-XXXXXX - images/side1.jpg - http://www.google.at
Side 2-xxxxx - images/side2.jpg - http://www.gmx.at
Side 3-xxxxx - images/side3.jpg - http://www.yahoo.at
0
 
druplashAuthor Commented:
But this is not your code. This is the code deepanjandas ;)

Thank´s
0
 
dgofmanCommented:
Common I just misspell your variables in two places. I expected you will able to figureout

import caurina.transitions.*;

function updateTitle(index:uint, xml:XML):void{
	if(this["Title_txt" + index] == undefined){
		trace("Component Title_txt" + index + " is undefined");
	}else{
		this["Title_txt" + index].text = new String(xml.@name).toUpperCase();
	    this["Title_txt" + index].autoSize = TextFieldAutoSize.LEFT;
	}
}

function updateComments(index:uint, xml:XML):void{
	if(this["Comments_txt" + index] == undefined){
		trace("Component Comments_txt" + index + " is undefined");
	}else{
		this["Comments_txt" + index].text = new String(xml.@comments).toUpperCase();
	    this["Comments_txt" + index].autoSize = TextFieldAutoSize.LEFT;
	}
}

function updateImage(index:uint, xml:XML):void{
	if(this["holder_mc" + index] == undefined){
		trace("Component holder_mc" + index + " is undefined");
	}else{
		var loader:ImageLoader = new ImageLoader();
		loader.image = xml.image;
		loader.link = xml.link;
		loader.load(new URLRequest(loader.image));
		loader.addEventListener(MouseEvent.CLICK, function(event:MouseEvent):void{
			var link:String = event.target.link;
			trace(link);
			navigateToURL(new URLRequest(link), '_self');
		});
		this["holder_mc" + index].addChild(loader);
	}
}

var loader:URLLoader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.TEXT;
loader.addEventListener(Event.COMPLETE, onLoadXML);
loader.load(new URLRequest("flash_home.xml"));
var _this:Object = this;

function onLoadXML(ev:Event)
{
	try{
		var myXML:XML = new XML(ev.target.data);
		var list:XMLList = myXML..title;
		for(var i = 0; i < list.length(); i++){
			var title:XML = list[i];
			_this.updateTitle(i, title);
			_this.updateComments(i, title);
			_this.updateImage(i, title);
		}
	} catch (e:TypeError){
		trace("Could not parse the XML");
		trace(e.message);
	}
};


this.holder_mc0.addEventListener(flash.events.MouseEvent.ROLL_OVER, this.tintButtonOver);
this.holder_mc0.addEventListener(flash.events.MouseEvent.ROLL_OUT, this.tintButtonOut);

this.holder_mc1.addEventListener(flash.events.MouseEvent.ROLL_OVER, this.tintButtonOver);
this.holder_mc1.addEventListener(flash.events.MouseEvent.ROLL_OUT, this.tintButtonOut);

this.holder_mc2.addEventListener(flash.events.MouseEvent.ROLL_OVER, this.tintButtonOver);
this.holder_mc2.addEventListener(flash.events.MouseEvent.ROLL_OUT, this.tintButtonOut);

this.holder_mc3.addEventListener(flash.events.MouseEvent.ROLL_OVER, this.tintButtonOver);
this.holder_mc3.addEventListener(flash.events.MouseEvent.ROLL_OUT, this.tintButtonOut);

function tintButtonOver(arg1:flash.events.Event):void
{
	var loc1:* = flash.display.MovieClip(arg1.currentTarget);
	caurina.transitions.Tweener.addTween(loc1, {"alpha":0.6, "time":1.2, "transition":"easeoutsine"});
	return;
}

function tintButtonOut(arg1:flash.events.Event):void
{
	var loc1:* = flash.display.MovieClip(arg1.currentTarget);
	caurina.transitions.Tweener.addTween(loc1, {"alpha":1, "time":1.2, "transition":"easeoutsine"});
	return;
}

Open in new window

0
 
druplashAuthor Commented:
Sorry, i´m definitly not. I´m very new to AS3.
Thank´s anyway for helping, hope that i have anytime the chance to retaliate.

Best,
druplash
0
 
dgofmanCommented:
Remeber you can get error code 1010 if you are requesting property from undefined variable
0
 
druplashAuthor Commented:
Ok, thank´s i will remember.
0
 
druplashAuthor Commented:
What i have sse now is that the comments are not displayed. Only the image, the title and the link. Do you have an idea why?

Thank´s
0
 
dgofmanCommented:
My bad,

I using attribute instead node declaration

change
this["Comments_txt" + index].text = new String(xml.@comments).toUpperCase();

to

this["Comments_txt" + index].text = new String(xml.comments).toUpperCase();
0
 
druplashAuthor Commented:
Now it works really perfect! Than´s again!!! You´re great...
0
 
dgofmanCommented:
You have to start debugging the code, before sending the question.
We are expert can help to find a solution, but fixing your code it’s your responsibility.
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

  • 17
  • 8
  • 8
Tackle projects and never again get stuck behind a technical roadblock.
Join Now