PDA

View Full Version : Loading .txt files trough xml.



Starrk
April 7th, 2009, 07:27 AM
Hi. i am doing a flash website, and i am trying to do a news page that loads the news from .txt files trough a xml file.

i am basing my code in this tutorial
http://www.republicofcode.com/tutori...lery/index.php (http://www.republicofcode.com/tutorials/flash/as3gridgallery/index.php)

i can load the images for the thumbs, and the big news, but i cant make the text appear. i get the error:

Error #2044: Unhandled IOErrorEvent:. text=Error #2124: Loaded file is an unknown type.

my code is:
Code:
stop();


function goHome(e:MouseEvent):void {
gotoAndStop("Home");
home_btn.terraButton.emphasized = true;
about_btn.terraButton.emphasized = false;
links_btn.terraButton.emphasized = false;
contact_btn.terraButton.emphasized = false;
}
home_btn.addEventListener(MouseEvent.CLICK, goHome);
home_btn.buttonText.text = "home";
home_btn.mouseChildren = true;
home_btn.buttonMode = true;


function goAbout(e:MouseEvent):void {
gotoAndStop("About");
about_btn.terraButton.emphasized = true;
home_btn.terraButton.emphasized = false;
links_btn.terraButton.emphasized = false;
contact_btn.terraButton.emphasized = false;
this.removeChild(container_mc);

}
about_btn.addEventListener(MouseEvent.CLICK, goAbout);
about_btn.buttonText.text = "apresentação";

function goLinks(e:MouseEvent):void {
gotoAndStop("Links");
links_btn.terraButton.emphasized = true;
home_btn.terraButton.emphasized = false;
about_btn.terraButton.emphasized = false;
contact_btn.terraButton.emphasized = false;
this.removeChild(container_mc);
}
links_btn.addEventListener(MouseEvent.CLICK, goLinks);
links_btn.buttonText.text = "projectos";

function goContact(e:MouseEvent):void {
gotoAndStop("Contact");
contact_btn.terraButton.emphasized = true;
home_btn.terraButton.emphasized = false;
about_btn.terraButton.emphasized = false;
links_btn.terraButton.emphasized = false;
this.removeChild(container_mc);
}
contact_btn.addEventListener(MouseEvent.CLICK, goContact);
contact_btn.buttonText.text = "contactos";

//++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++

var my_x:Number;
var my_y:Number;
var my_thumb_width:Number;
var my_thumb_height:Number;
var my_news:XMLList;
var my_total:Number;


var container_mc:MovieClip;


var myXMLLoader:URLLoader = new URLLoader();
myXMLLoader.addEventListener(Event.COMPLETE, processXML);
myXMLLoader.load(new URLRequest("noticias.xml"));


function processXML(e:Event):void {
var myXML:XML = new XML(e.target.data);


my_thumb_width = myXML.@WIDTH;
my_thumb_height = myXML.@HEIGHT;

my_news = myXML.NOTICIA;
my_total = my_news.length();

createContainer();
callThumbs();
callTitle();
}


function createContainer():void {
container_mc = new MovieClip();
container_mc.x = my_x;
container_mc.y = my_y;
addChild(container_mc);

container_mc.addEventListener(MouseEvent.CLICK, callFull);
}

function callThumbs():void {
for (var i:Number = 0; i < my_total; i++) {

var my_x = my_news[i].@POSITIONX;
var my_y = my_news[i].@POSITIONY;
var thumb_url = my_news[i].@THUMB;

var thumb_loader = new Loader();
thumb_loader.contentLoaderInfo.addEventListener(Ev ent.COMPLETE, thumbLoaded);
thumb_loader.load(new URLRequest(thumb_url));

thumb_loader.name = i;

thumb_loader.x = my_x;
thumb_loader.y = my_y;
}
}

function callTitle():void {
for (var i:Number = 0; i < my_total; i++) {

var my_x = my_news[i].@POSITIONX;
var my_y = my_news[i].@POSITIONY;
var my_title_url = my_news[i].@TITLE;

var title_loader:Loader = new Loader();

title_loader.addEventListener(Event.COMPLETE, titleLoaded);
title_loader.load(new URLRequest(my_title_url));
}
}

/*function callTitle():void {
for (var i:Number = 0; i < my_total; i++) {

var my_x = my_news[i].@POSITIONX;

var my_y = my_news[i].@POSITIONY;

var my_title_url = my_news[i].@TITLE;

var title_loader = new Loader();
title_loader.contentLoaderInfo.addEventListener(Ev ent.COMPLETE, titleLoaded);
title_loader.load(new URLRequest(my_title_url));


}
}*/

function thumbLoaded(e:Event):void {
var my_thumb:Loader = Loader(e.target.loader);
container_mc.addChild(my_thumb);

}

function titleLoaded(e:Event):void {
var myTitleField_txt:TextField = new TextField();
var title_loader:Loader = Loader(e.target.loader);

myTitleField_txt.x = my_x;
myTitleField_txt.y = my_y;
myTitleField_txt.wordWrap=true;
myTitleField_txt.autoSize=TextFieldAutoSize.LEFT;

container_mc.addChild(myTitleField_txt);
myTitleField_txt.text = e.target.data;
}

function callFull(e:MouseEvent):void {


var full_loader:Loader = new Loader();
var full_url = my_news[e.target.name].@FULL;
full_loader.load(new URLRequest(full_url));
full_loader.contentLoaderInfo.addEventListener(Eve nt.INIT, fullLoaded);
}

function fullLoaded(e:Event):void {
var my_loader:Loader = Loader(e.target.loader);
addChild(my_loader);
my_loader.x = 200;
my_loader.y = 25;
my_loader.height = 350;
my_loader.width = 775;
//container_mc.mythumb.addEventListener(MouseEvent.C LICK,removeFull);
//my_loader.addEventListener(MouseEvent.CLICK,remove Full);
}

function removeFull(e:MouseEvent):void{
var my_loader:Loader = Loader (e.currentTarget);
my_loader.unload();
removeChild(my_loader);
}

stop();
and the xml is

Code:
<?xml version="1.0" encoding="utf-8"?>
<NOTICIAS WIDTH="240" HEIGHT="125">
<NOTICIA FULL="news/noticia_1.jpg" THUMB="thumb/noticia_1.jpg" TEXTO="texto/noticia_1.txt" POSITIONX="200" POSITIONY="400" TITLE="titulo/noticia_1.txt" />
<NOTICIA FULL="news/noticia_2.jpg" THUMB="thumb/noticia_2.jpg" TEXTO="texto/noticia_2.txt" POSITIONX="465" POSITIONY="400" TITLE="titulo/noticia_2.txt" />
<NOTICIA FULL="news/noticia_3.jpg" THUMB="thumb/noticia_3.jpg" TEXTO="texto/noticia_3.txt" POSITIONX="730" POSITIONY="400" TITLE="titulo/noticia_3.txt" />
</NOTICIAS>
the file structure is good, and the .txt are saved in ANSI encoding

any help is appreciated

Daganev
April 7th, 2009, 08:17 PM
Error #2044: Unhandled IOErrorEvent:. text=Error #2124: Loaded file is an unknown type.

You'll have to tell us which function this error appears in. Normally it means that your file isn't named correctly (maybe it has a double sufffix?) or perhaps it's not really an xml file but just a plain .txt file? really hard to say.

Starrk
April 8th, 2009, 08:06 AM
I am positive that it has to do with the TITLE="titulo/noticia_1.txt".
the file is there, and it is a normal .txt file.
also when i change the name of the files in the "titulo" folder, the error changes to "URL not found".

Daganev
April 13th, 2009, 03:01 PM
you have to make the file a .xml file, not a .txt file I believe.

Starrk
April 14th, 2009, 06:58 AM
the XML is in a XML file, but you are saying that i should put my news text in a .xml instead of a .txt?

Daganev
April 14th, 2009, 09:05 PM
oh hmm no.

Ok, sorry I didn't understand you before. Looking over your code again what you want to do is change

var title_loader:Loader = new Loader();

To

var title_loader:URLLoader = new URLLoader();

Loader is for graphics, URLLoader is for text.

Starrk
April 15th, 2009, 08:21 AM
that was the problem, its solved.

now i have a doubt, when i create a var inside a function, it only works inside that function?


function callTitle():void {
for (var i:Number = 0; i < my_total; i++) {

var tit_x = my_news[i].@POSITIONX;
var tit_y = my_news[i].@POSITIONY;
var my_title_url = my_news[i].@TITLE;

var title_loader:URLLoader = new URLLoader();

title_loader.addEventListener(Event.COMPLETE, titleLoaded);
title_loader.load(new URLRequest(my_title_url));


}
}

function titleLoaded(e:Event):void {
var myTitleField_txt:TextField = new TextField();

myTitleField_txt.wordWrap=true;
myTitleField_txt.autoSize=TextFieldAutoSize.LEFT;
myTitleField_txt.x = tit_x;
myTitleField_txt.y = tit_y;
container_mc.addChild(myTitleField_txt);
myTitleField_txt.text = e.target.data;
}


how can i make the vars tit_x and tit_y pass to the titleLoaded function?

Starrk
April 16th, 2009, 08:44 AM
important things about that last post:

i have declared the vars tit_x and tit_y outside the function.

i was trying to adapt this code that loads images trough a xml file

function callThumbs():void {
for (var i:Number = 0; i < my_total; i++) {

var my_x = my_news[i].@POSITIONX;
var my_y = my_news[i].@POSITIONY;
var thumb_url = my_news[i].@THUMB;

var thumb_loader = new Loader();
thumb_loader.contentLoaderInfo.addEventListener(Ev ent.COMPLETE, thumbLoaded);
thumb_loader.load(new URLRequest(thumb_url));

thumb_loader.name = i;

thumb_loader.x = my_x;
thumb_loader.y = my_y;
}
}

function thumbLoaded(e:Event):void {
var my_thumb:Loader = Loader(e.target.loader);
container_mc.addChild(my_thumb);

}


with this code the images go to the positions that i assign them in the xml file, but the text (code in last post) allways goes to position 0,0.

i guess this has got to do with the use of different Loader types, as i cant assign a x or y position to the URLLoader...

Starrk
April 17th, 2009, 08:45 AM
from a sugestion in another forum i traced the variables tit_x and tit_y

results:

when i trace inside function callTitle i get the right values, but when i trace inside function titleLoaded i get NaN (not a number).

i tried something, i declared the variables tit_x and tit_y only inside function callTitle,

var tit_x:Number = my_news[i].@POSITIONX;
var tit_y:Number = my_news[i].@POSITIONY;
and i got the error " 1120: Access of undefined property tit_x" in the lines of the titlleLoaded function. i guess that the variables are only being defined inside the callTitle function. how can i change that?

Pier25
April 17th, 2009, 02:11 PM
Why would you want to load a txt when you have an xml?

Put all the info you need on the xml and problem solved.

Daganev
April 17th, 2009, 06:19 PM
Ok, what you want to do know is inside your for loop, create an array. In that array you will store the x and y position of all your titles. Then when you make your thumb for loop, you can ue something like titleArray[i].x and titleArray[i].y