PDA

View Full Version : XML AS3 Image Hyperlinks


Manny
05-22-2008, 10:20 PM
Hi all, a newbie to AS3, trying to move on from AS2 and learn some new things. Unfortunately I'm having a few difficulties implementing a new feature to my website.

Take a look at what I currently have, this is done using AS2.
My Web Page (http://www.burndenaces.co.uk/premierleague/)

The crests across the page are linked to other pages, but what I want to do is move on and use XML to bring the crests in from my website, still linked to other pages.

I have created the following XML document, but would anybody be able to help me with coding of the AS3 file.

<?xml version="1.0" encoding="UTF-8"?>
<root>

<COMPETITION NAME="Premier League">

<CLUBS>

<CLUB NAME="Arsenal" CREST="/images/crests/40x40/arsenal.png" LINK="/football/team/0,19734,11670,00.html" />

<CLUB NAME="Aston Villa" CREST="/images/crests/40x40/astonvilla.png" LINK="/football/team/0,19734,11677,00.html" />

<CLUB NAME="Birmingham City" CREST="/images/crests/40x40/birminghamcity.png" LINK="/football/team/0,19734,11694,00.html" />

<CLUB NAME="Blackburn Rovers" CREST="/images/crests/40x40/blackburnrovers.png" LINK="/football/team/0,19734,11676,00.html" />

<CLUB NAME="Bolton Wanderers" CREST="/images/crests/40x40/boltonwanderers.png" LINK="/football/team/0,19734,11672,00.html" />

<CLUB NAME="Chelsea" CREST="/images/crests/40x40/chelsea.png" LINK="/football/team/0,19734,11668,00.html" />

<CLUB NAME="Derby County" CREST="/images/crests/40x40/derbycounty.png" LINK="/football/team/0,19734,11696,00.html" />

<CLUB NAME="Everton" CREST="/images/crests/40x40/everton.png" LINK="/football/team/0,19734,11671,00.html" />

<CLUB NAME="Fulham" CREST="/images/crests/40x40/fulham.png" LINK="/football/team/0,19734,11681,00.html" />

<CLUB NAME="Liverpool" CREST="/images/crests/40x40/liverpool.png" LINK="/football/team/0,19734,11669,00.html" />

<CLUB NAME="Manchester City" CREST="/images/crests/40x40/manchestercity.png" LINK="/football/team/0,19734,11679,00.html" />

<CLUB NAME="Manchester United" CREST="/images/crests/40x40/manchesterunited.png" LINK="/football/team/0,19734,11667,00.html" />

<CLUB NAME="Middlesbrough" CREST="/images/crests/40x40/middlesbrough.png" LINK="/football/team/0,19734,11680,00.html" />

<CLUB NAME="Newcastle United" CREST="/images/crests/40x40/newcastleunited.png" LINK="/football/team/0,19734,11678,00.html" />

<CLUB NAME="Portsmouth" CREST="/images/crests/40x40/portsmouth.png" LINK="/football/team/0,19734,11674,00.html" />

<CLUB NAME="Reading" CREST="/images/crests/40x40/reading.png" LINK="/football/team/0,19734,11673,00.html" />

<CLUB NAME="Sunderland" CREST="/images/crests/40x40/sunderland.png" LINK="/football/team/0,19734,11695,00.html" />

<CLUB NAME="Tottenham Hotspur" CREST="/images/crests/40x40/tottenhamhotspur.png" LINK="/football/team/0,19734,11675,00.html" />

<CLUB NAME="West Ham United" CREST="/images/crests/40x40/westhamunited.png" LINK="/football/team/0,19734,11685,00.html" />

<CLUB NAME="Wigan Athletic" CREST="/images/crests/40x40/wiganathletic.png" LINK="/football/team/0,19734,11682,00.html" />

</CLUBS>
</COMPETITION>
</root>

Thanks in advance,
Manny

jarmanje
05-22-2008, 11:42 PM
an example

var xml_loader:URLLoader = new URLLoader();
var db:XML = new XML();
xml_loader.addEventListener(Event.COMPLETE, db_yuklendi);
xml_loader.load(new URLRequest("images/gallery.xml"));

function db_yuklendi(event:Event):void {
db = new XML(event.target.data);
var image_count:int = db.item.length();
olustur(image_count,db);
}
var main_ekran:ana_ekran = new ana_ekran();
function olustur(image_count:int,data:XML) {

amovieclip.sexylady.text = data.item. yourimage.text()[0].toString();


//etc
//etc


Your xml seems to done badly. do it like

<list>

<item>
<yourimage>images/crests/40x40/arsenal.png</yourimage>
<link>football/team/0,19734,11677,00.html</link>
</item>

<item>
<yourimage>images/crests/40x40/middlesbrough.png</yourimage>
<link>football/team/0,19734,11677,00.html</link>
</item>

</list>

access item numbers like this


for (var i:int; i<image_count; i++) {


var thumbnail:kucuk_ekran = new kucuk_ekran();

var adres:String = data.item. yourimage.text()[i].toString();
var xml_adres1:URLRequest = new URLRequest(adres);

var setLink = data.item.collar.text()[i].toString();
thumbnail.link = setLink

thumbnail.load(xml_adres1);
thumbnail.bos_buton.addEventListener(MouseEvent.MO USE_DOWN, clicked);
thumbnail.bos_buton.addEventListener(MouseEvent.RO LL_OVER, rollover);
thumbnail.bos_buton.addEventListener(MouseEvent.RO LL_OUT, rollout);
bos_mc.addChild(thumbnail);
}
}

if you set a variable inside each thumbnail called "link" like i have done with this bit thumbnail.link = setLink

then you can just call the link easily by referring to bos_mc.thumbnail.link typed thing

Manny
05-23-2008, 02:30 PM
Thanks for the feedback. I have modified my XML code and had a go at the AS code given, but no end result.

Here is my new XML code.
<?xml version="1.0" encoding="UTF-8"?>
<root>
<teams>

<club>
<crest>/images/crests/40x40/arsenal.png</crest>
<link>/news/arsenal.htm</link>
</club>

<club>
<crest>/images/crests/40x40/astonvilla.png</crest>
<link>/news/astonvilla.htm</link>
</club>

<club>
<crest>/images/crests/40x40/birminghamcity.png</crest>
<link>/news/birminghamcity.htm</link>
</club>

<club>
<crest>/images/crests/40x40/blackburnrovers.png</crest>
<link>/news/blackburnrovers.htm</link>
</club>

<club>
<crest>/images/crests/40x40/boltonwanderers.png</crest>
<link>#</link>
</club>

<club>
<crest>/images/crests/40x40/chelsea.png</crest>
<link>/news/chelsea.htm</link>
</club>

<club>
<crest>/images/crests/40x40/derbycounty.png</crest>
<link>/news/derbycounty.htm</link>
</club>

<club>
<crest>/images/crests/40x40/everton.png</crest>
<link>/news/everton.htm</link>
</club>

<club>
<crest>/images/crests/40x40/fulham.png</crest>
<link>/news/fulham.htm</link>
</club>

<club>
<crest>/images/crests/40x40/liverpool.png</crest>
<link>/news/liverpool.htm</link>
</club>

<club>
<crest>/images/crests/40x40/manchestercity.png</crest>
<link>/news/manchestercity.htm</link>
</club>

<club>
<crest>/images/crests/40x40/manchesterunited.png</crest>
<link>/news/manchesterunited.htm</link>
</club>

<club>
<crest>/images/crests/40x40/middlesbrough.png</crest>
<link>/news/middlesbrough.htm</link>
</club>

<club>
<crest>/images/crests/40x40/newcastleunited.png</crest>
<link>/news/newcastleunited.htm</link>
</club>

<club>
<crest>/images/crests/40x40/portsmouth.png</crest>
<link>/news/portsmouth.htm</link>
</club>

<club>
<crest>/images/crests/40x40/reading.png</crest>
<link>/news/reading.htm</link>
</club>

<club>
<crest>/images/crests/40x40/sunderland.png</crest>
<link>/news/sunderland.htm</link>
</club>

<club>
<crest>/images/crests/40x40/tottenhamhotspur.png</crest>
<link>/news/tottenhamhotspur.htm</link>
</club>

<club>
<crest>/images/crests/40x40/westhamunited.png</crest>
<link>/news/westhamunited.htm</link>
</club>

<club>
<crest>/images/crests/40x40/wiganathletic.png</crest>
<link>/news/wiganathletic.htm</link>
</club>

</teams>
</root>

And here is the AS code.
var xml_loader:URLLoader = new URLLoader();
var db:XML = new XML();
xml_loader.addEventListener(Event.COMPLETE, db_yuklendi);
xml_loader.load(new URLRequest("/XML AS/teams.xml"));

function db_yuklendi(event:Event):void {
db = new XML(event.target.data);
var image_count:int = db.item.length();
olustur(image_count,db);
}
var main_ekran:ana_ekran = new ana_ekran();
function olustur(image_count:int,data:XML) {

amovieclip.clubs.text = data.item. crest.text()[0].toString();

for (var i:int; i<image_count; i++) {


var thumbnail:kucuk_ekran = new kucuk_ekran();

var adres:String = data.item. crest.text()[i].toString();
var xml_adres1:URLRequest = new URLRequest(adres);

var setLink = data.item.collar.text()[i].toString();
thumbnail.link = setLink

thumbnail.load(xml_adres1);
thumbnail.bos_buton.addEventListener(MouseEvent.MO USE_DOWN, clicked);
thumbnail.bos_buton.addEventListener(MouseEvent.RO LL_OVER, rollover);
thumbnail.bos_buton.addEventListener(MouseEvent.RO LL_OUT, rollout);
bos_mc.addChild(thumbnail);
}
}

I have linked the .fla and .as file together through the document class and when I test the movie in Flash CS3 there is an error on line 6.

1046: Type was not found or was not a compile-time constant: Event.

amarghosh
05-24-2008, 06:55 AM
import flash.events.Event;

Manny
05-24-2008, 12:59 PM
Right, I added that line of code to my AS file and put in the package line at the beginning.

Now, with the following code, it returns one error, 1084: expecting rightbrace before end of program.

Here is the code:
package {

var xml_loader:URLLoader = new URLLoader();
var db:XML = new XML();
xml_loader.addEventListener(Event.COMPLETE, db_yuklendi);
xml_loader.load(new URLRequest("http://www.burndenaces.co.uk/XML AS/teams.xml"));

import flash.events.Event;
function db_yuklendi(event:Event):void {
db = new XML(event.target.data);
var image_count:int = db.item.length();
olustur(image_count,db);
}
var main_ekran:ana_ekran = new ana_ekran();
function olustur(image_count:int,data:XML) {

amovieclip.clubs.text = data.item. crest.text()[0].toString();

for (var i:int; i<image_count; i++) {


var thumbnail:kucuk_ekran = new kucuk_ekran();

var adres:String = data.item. crest.text()[i].toString();
var xml_adres1:URLRequest = new URLRequest(adres);

var setLink = data.item.collar.text()[i].toString();
thumbnail.link = setLink

thumbnail.load(xml_adres1);
thumbnail.bos_buton.addEventListener(MouseEvent.MO USE_DOWN, clicked);
thumbnail.bos_buton.addEventListener(MouseEvent.RO LL_OVER, rollover);
thumbnail.bos_buton.addEventListener(MouseEvent.RO LL_OUT, rollout);
bos_mc.addChild(thumbnail);
}
}

I have tried adding a rightbrace to the end of my code and in several other places, but I then receive 17 errors. All of which are shown below, what am I doing that is so wrong.

http://img528.imageshack.us/img528/9236/errorskm3.jpg

amarghosh
05-26-2008, 04:19 AM
is the code on timeline or document class?
u haven't imported anything.
when u see 'Access of undefined property MouseEvent', u should import flash.events.MouseEvent;
similarly for URLLoader, URLRequest etc

Manny
05-26-2008, 07:15 PM
I decided against using Flash in the end.

It was just throwing out error after error and seeing as I'm inexperienced in it I thought I'd leave it.

Decided to use PHP instead to interact with an SQL database. I got the result I wanted in 10 minutes.

jarmanje
05-26-2008, 10:12 PM
if you attach it, i can try to fix it. doesnt look too bad..