FORUMS: list search recent posts

Problem with URLRequest and variables

COW Forums : Adobe Flash

<< PREVIOUS   •   FAQ   •   VIEW ALL   •   PRINT   •   NEXT >>
Claudio Cisneros
Problem with URLRequest and variables
on Feb 16, 2011 at 3:28:40 pm

Hi everybody, im new here (and my english sucks). Also im kind of newbie in AS3. Im having problems trying to pass a variable as a value of URL in the URLRequest method. Here's part of my code:

var sourceImage:String;
var request:URLRequest = new URLRequest(sourceImage);
contenedorImagen.load(new URLRequest(request));


sourceImage receives his content from an xml.
When I compile a get an error saying that the "url" parameter can't be null, I guess it's because the method doesn't recognizes the variable as a valid parameter.

I'll apreciate any help. Thanks
Claudio


Return to posts index

demetri tashie
Re: Problem with URLRequest and variables
on Feb 16, 2011 at 5:40:28 pm

Claudio,

there are a few things wrong with that snippet of code. one is that you never declared what the String value is, so the 'request' is for an empty/null String

second is that 'contenedorImagen' is trying to load a new URL request, that has as its parameter another new URL Request ( the 'request' variable ). you should just have
contenedorImagen.load(request);

here is some complete code you can look at and play around with. it will load the first node of the xml file
into the Loader:
var myXML:XML;
var myLoader:URLLoader = new URLLoader();
myLoader.load(new URLRequest("myXMLfile.xml"));
myLoader.addEventListener(Event.COMPLETE, processXML);

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

var sourceImage:String;
sourceImage = myXML.image[0];

var contenedorImagen:Loader = new Loader();
var myRequest:URLRequest = new URLRequest(sourceImage);
contenedorImagen.load(myRequest);
addChild(contenedorImagen);
}


and the 'myXMLfile' file used:
(?xml version="1.0" encoding="utf-8"?)

(images)

(image)pic1.jpg(/image)

(image)pic2.jpg(/image)

(/images)


i hope that helps out


Return to posts index

Claudio Cisneros
Re: Problem with URLRequest and variables
on Feb 16, 2011 at 6:54:51 pm

Im passing a value to the variable in other part of the code. But I'll try this solution.

Thanks!

Claudio


Return to posts index


Claudio Cisneros
Re: Problem with URLRequest and variables
on Feb 16, 2011 at 8:55:24 pm

Well, im getting some problems yet. I put the code, but when I compile I get this error:

TypeError: Error #1034: Type Coercion failed: cannot convert flash.display::MovieClip@3df3eb81 to flash.display.Loader.
at flash.display::Sprite/constructChildren()
at flash.display::Sprite()
at flash.display::MovieClip()
at homes1_fla::mc_Prod_51()
at flash.display::Sprite/constructChildren()
at flash.display::Sprite()
at flash.display::MovieClip()
at homes1_fla::mod_noticias_33()
at flash.display::Sprite/constructChildren()
at flash.display::Sprite()
at flash.display::MovieClip()
at homes1_fla::MainTimeline()

The code is:

var contenedorImagen:Loader = new Loader();
var myRequest:URLRequest = new URLRequest(rutaImagen);
contenedorImagen.load(myRequest);
addChild(contenedorImagen);


Im setting the variable rutaImagen in a different section of the file. It's a very complex file so I can't put the source to be reviewed. I guess im missing too the concept of the addChild method.

Any idea?


Return to posts index

demetri tashie
Re: Problem with URLRequest and variables
on Feb 16, 2011 at 11:37:40 pm

OK. without more, or specific information, there is not much more i can say.

however, if you are defining 'rutaImagen' someplace else in the file, then you can't call and access it in the code before it is declared, or there will be errors.

addChild actually places the Loaader in the 'display list' so that it can be seen. otherwise it is created, but will not show up.


Return to posts index

Claudio Cisneros
Re: Problem with URLRequest and variables
on Feb 17, 2011 at 1:01:22 pm

Well, im trying some variants. Now i have almost all the code in the same frame, so im setting the variables and "trying" to load the image in the same place:

noticias.mc_obj2.contenedor.rutaImagen = newsData.noticia[index1].articulo[1].image;
var noticias.mc_obj2.contenedorImagen:Loader = new Loader();
var myRequest:URLRequest = new URLRequest(noticias.obj2.contenedor.rutaImagen);
noticias.mc_obj2.contenedorImagen.load(myRequest);
addChild(noticias.mc_obj2.contenedorImagen);


Now the error I get is: Error:1086: Syntax error: expecting semicolon before dot. It seems to be in the 2nd line of the code.


I think I'll cry...a little


Return to posts index


demetri tashie
Re: Problem with URLRequest and variables
on Feb 17, 2011 at 2:29:39 pm

you are probably right.

look at this - the 2nd line:
var noticias.mc_obj2.contenedorImagen:Loader = new Loader();
don't declare a variable as an object pathway. just declare your variable simply like this:var contenedorImagen:Loader = new Loader();
and THEN add it to the pathway you want. so change the last line to this :
noticias.mc_obj2.addChild(contenedorImagen);
you will also need to adjust your 4th line from this:
noticias.mc_obj2.contenedorImagen.load(myRequest);
to this:
contenedorImagen.load(myRequest);
try that and let us know


Return to posts index

Claudio Cisneros
Re: Problem with URLRequest and variables
on Feb 18, 2011 at 1:36:05 pm

Hi, well that solve the error, but now I have another issue. In the code below is where Im setting the variable for the UrlRequest:

noticias.mc_obj2.urlImage = newsData.noticia[index1].articulo[1].image;

and then:

var contenedorImagen:Loader = new Loader();
var myRequest:URLRequest = new URLRequest(noticias.mc_obj2.urlImage);
contenedorImagen.load(myRequest);
noticias.mc_obj2.addChild.(contenedorImagen);


The problem here appears when I try to assign the value to a variable, for some extrange reason it don't work. I try assigning the value to a dynamic text field and works fine. I also try declaring a variable in the root, and doesn't work neither.

I know you have the answer! ;)


Return to posts index

demetri tashie
Re: Problem with URLRequest and variables
on Feb 18, 2011 at 4:23:30 pm

i am not following you. i am not sure what you are trying to do with this line here:
noticias.mc_obj2.urlImage = newsData.noticia[index1].articulo[1].image;

so what is 'noticias.mc_obj2.urlImage' ? is it a Sting or an Object ( i.e. movieclip)?
a URLRequest is looking for a String ( as a pathway to what you are trying to load). i don't see how this: new URLRequest(noticias.mc_obj2.urlImage) will work.

i am just guessing that you are actually trying to just load 'urlImage'. but you can't call to get it from inside the movieclip 'noticias.mc_obj2'. call to it from its source. if it is something within your flash movie, then you probably want to just addChild instead - but i am guessing now as i said before i am not following your train of logic right now.


Return to posts index


Claudio Cisneros
Re: Problem with URLRequest and variables
on Feb 18, 2011 at 5:06:15 pm

Im going a step backward. Why this:

noticias.mc_obj2.urlImage = newsData.noticia[index1].articulo[1].image;

don't work, and this does ?:

someTextField.text = newsData.noticia[index1].articulo[1].image;


In the first line im trying to assign the value of "image" in the xml to the urlImage variable, declared inside of noticias.mc_obj2. In the 2nd line im assigning that value to a dynamic text field.

Im pretty sure im mixing stuffs from AS2.0


Return to posts index


demetri tashie
Re: Problem with URLRequest and variables (long reply)
on Feb 22, 2011 at 2:13:07 am

claudio, i am having a very hard time understanding your needs.

i still can not get what you want to accomplish with this line of code:noticias.mc_obj2.urlImage = newsData.noticia[index1].articulo[1].image;so i will post this simple example i made which you or anyone else can look at and see how information in an XML file can be accessed and utilized in an AS3 file.

here is the complete simple XML file, which i called "imageXML.xml":(?xml version="1.0" encoding="utf-8"?)

(images)

(image) pic1.jpg (/image)
(image) xmlSymbol (/image)

(/images)


where the first node will be used to load an image 'pic1.jpg' to show you how it can load an image ( which is good for making image galleries) AND it will also load that as text into a dynamic text box, to show how that can be done also.

the second node contains a reference to the instance name of a symbol on the stage. i have a feeling that you are trying to do something with a symbol from that first line of code which i don't understand. if so, this shows you one way that you can store the names in the XML file to reference later.

the flash file has nothing on the stage except a dynamci text field called ' txt_txt' and a movieclip symbol named 'xmlSymbol'

you will see from the code below that once the XML is loaded, that
  • the Loader named 'contenedorImagen' will be loaded with pic1.jpg

  • that the file name will be added as text to the dynamic text field'

  • that the file name will be added as text to the dynamic text field

  • that the movieclip symbol will change it's alpha :

  • var myXML:XML;
    var myLoader:URLLoader = new URLLoader();
    myLoader.load(new URLRequest("imageXML.xml"));
    myLoader.addEventListener(Event.COMPLETE, processXML);


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

    var sourceImage:String;
    sourceImage = myXML.image[0];

    txt_txt.text = sourceImage;

    var contenedorImagen:Loader = new Loader();
    var myRequest:URLRequest = new URLRequest(sourceImage);
    contenedorImagen.load(myRequest);
    addChild(contenedorImagen);


    var sourceImage2:String;
    sourceImage2 = myXML.image[1];
    this[sourceImage2].alpha = .3;

    }


    i figure ' a picture is worth a thousand words' so here is a zip archive folder containing the FLA, the XML, and the JPG: 1662_archive.zip

    if you will look at and understand this, then it will answer your question of why you could put the xml node info into a dynamic text field, but get an error when trying your other line of code.

    hope this helps


    Return to posts index

    Claudio Cisneros
    Re: Problem with URLRequest and variables (long reply)
    on Feb 22, 2011 at 12:49:32 pm

    It works fine. In my file I have the function that parse the xml in the root, but the mc in which I need to load the image is in root > noticias > mc_obj2. It'll be easier if I put the code to load the image into another function?. Can you give an example of the correct way to do it?
    Thanks


    Return to posts index

    demetri tashie
    Re: Problem with URLRequest and variables (long reply)
    on Feb 22, 2011 at 11:16:40 pm

    keep ALL your code always in the root - not inside of movieclips.

    if you want to add the image somewhere down a chain of movieclips,then add the loader using whatever pathway to that movieclip that you need. so instead of just this:
    addChild(contenedorImagen);
    you would do something like this:
    movieclip1.movieclip2.movieclip3.addChild(contenedorImagen);


    Return to posts index

    << PREVIOUS   •   VIEW ALL   •   PRINT   •   NEXT >>
    © 2018 CreativeCOW.net All Rights Reserved
    [TOP]