Error loading web address XML file

Asked

Viewed 277 times

2

I have a flash game where I load the words through an XML file:

var carregaPalavras:XML = new XML();
carregaPalavras.ignoreWhite = true;
carregaPalavras.onLoad = function(success) {
    if (success) {
        parsePalavras();
    } else {
        guessWord_txt.text = "Ocorreu um erro!";
    }
};
carregaPalavras.load("palavras.xml");

Now I intended to give an added value to this, and load the words through a URL I have. I have tried it as follows but it does not load:

carregaPalavras.load("http://meusite.com/palavras.xml");

I have tried this, in AS3, but also error:

var myXML:XML;
var carregaPalavras:URLLoader = new URLLoader();
carregaPalavras.load(new URLRequest("http://meusite.com/palavras.xml"));
  • What mistake happens when?

  • I just edited above, I basically found that I am implementing in AS2, not As3 so the above code does not give. There is some load url for AS2?

  • I tested the first example in AS2 and it worked correctly... What error appears? I would recommend you use Actionscript 3.0, this is not possible?

  • Yes it is, but to convert after me error "1046: Type was not found or was not a Compile-time Constant: Void."

  • The first example, namely to load locally works well. But I want to host xml on the web server and I can’t get it through the url

1 answer

1

Actionscript 2.0 to 3.0

The language ActionScript 2.0 / 1.0 is limited by having few classes, assuming that it was created only to supply some actions of the Flash, including is not event-oriented, which makes it difficult to handle errors. We can practically say that, in relation to ActionScript 3.0, just like the name.

The errors occur because many actions you did on AS2 are nothing like AS3, so by converting your project to a different version, the conflict will actually happen. Not to mention that version 2.0 of the language has not been supported since 2006, when the release of version 3.0 took place.

At this link wikipedia you can find more information.

Code AS2

Regarding your question, the code to load an XML file into ActionScript 2.0 is just like below:

var xml:XML = new XML(); //Cria o objeto XML
//Define a função que será executada ao carregar o arquivo
xml.onLoad = function(resposta) { 
    if(resposta) {
       trace("Carregou XML"); //Mostra no Output que carregou o arquivo xml
       trace(xml);
    }
    else {
       trace("Erro ao carregar XML!");
    }
}

//Ao ocorrer um evento do servidor, como um erro, ele executará esta função:
xml.onData = function(resposta) {
    trace("Resposta do servidor: ");
    trace(resposta);
}    

xml.load("arquivo.xml"); //Executa a ação para carregar. Qualquer URI, seja externa ou local, é aceita.

Errors that may occur will be handled by the method onData. You can access the language reference 2.0 at this link.

Code AS3

The code to load the XML into ActionScript 3.0 is a little sharper, but more functional in relation to the treatment of events.

var xml:XML; //Cria a referência ao objeto XML
var urlLoader:URLLoader = new URLLoader(); //Responsável por carregar arquivos
var urlRequest:URLRequest = new URLRequest("arquivo.xml"); //Objeto que contém e trata URLs

urlLoader.load(urlRequest);
urlLoader.addEventListener(Event.COMPLETE, carregouXML);
urlLoader.addEventListener(IOErrorEvent.IO_ERROR, erroXML);

//Evento ao carregar o XML com sucesso!
function carregouXML(e:Event):void {
    xml = new XML(urlLoader.data);
    trace("Carregou XML");
    trace(xml);
}

//Erro ao carregar XML
function erroXML(e:IOErrorEvent):void {
   trace("Erro ao carregar XML: "+e);
}

Errors can be handled by events IOErrorEvent and SecurityError. You can access the language reference 3.0 at this link.

Completion

If you intend to change versions of Actionscript, keep in mind that the entire code of your game will have to be adapted. I particularly recommend that you use the latest version 3.0, with more web resources, used in Flex and Flash and with Adobe AIR Desktop and Mobile support.

Now, if conversion is not possible, treat errors with the methods cited and look for some actions in the code reference in the Adobe links.

  • Converting to AS3 da me erro in the following lines where I have "Function parsePalavras():Void {" and the error that gives is "1046: Type was not found or was not a Compile-time Constant: Void." I searched solutions, tried to put Imports but keeps the problem

  • Void is a reserved Actionscript 2.0 word, however, the reserved words in Actionscript 3.0 are now written in lowercase! Switch to void, in lower case. This error usually happens when it does not find a definition for a class/object/variable you are using.

  • solved! thanks, was another from loadvars have the code "Topicoinicio.onload = Function(Success:Boolean) { if (Success) { startScreen_mc.message_txt.text = Benvindo1+this.meuTopico+Benvindo2; } Else { startScreen_mc.message_txt.text = "Error occurred!" ; } };"

  • Yes, in AS3, methods such as onLoad are no longer used. Try using the example of Code AS3 in response!

  • Correcting the error is specified in: "var Topicoinicio:Loadvars = new Loadvars();" in Loadvars

  • This link can help you! http://evolve.reintroducing.com/2008/01/27/as2-to-as3/as2-%E2%86%92-as3-loadvars-as3-equivalent/

  • I can’t solve it, basically I load a TXT file (which has only one word, on the theme of the game) var Benvindo1:String = "Welcome to Hangman! The theme is about "; var Benvindo2:String = ". Press the PLAY button to start." ; var Topicoinicio:Loadvars = new Loadvars(); Topicoinicio.onload = Function(Success:Boolean) { if (Success) { startScreen_mc.message_txt.text = Benvindo1+this.meuTopico+Benvindo2; } Else { startScreen_mc.message_txt.text = "Error occurred!" ; } }; Topicoinicio.load("topic.txt");

  • Yes, it is clear that you did not quite understand the difference between AS2 and AS3. The problem is that you still continue to use methods of version 2.0. If you have converted your project to version 3.0, none of these methods will work. The object that loads files in version 3.0 is called Urlloader. As stated in the answer, AS3 is event-oriented, so you have to use the method addEventListener, for the event Event.COMPLETE, to "listen" that the file was loaded successfully.

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.