Skip to main content
BrianStapleton
Known Participant
December 2, 2009
Answered

Bring XML into flash using a variable

  • December 2, 2009
  • 2 replies
  • 541 views

Hi everyone.

I have a movieclip (jobImage) which I'm loading an image (Image1, a subnode of Job1) into using XML. But I want to be able to click on any one of a series of buttons which will have that image replaced by a new image (eg. Button2 will load Image2, another subnode of Job1, Button3 will load Image3, etc.).

So I have the selected button pass a number back to a variable called "_root.imageTarget". If Button2 was pressed, this would happen:

_root.imageTarget = "2";

This variable is then combined with the word "Image" to form a new variable (newImage)

newImage = "Image" + _root.imageTarget;

And that variable is used to load the correct node

jobImage.loadMovie("Images/" + work_arr[Job1].newImage);

Except it doesn't work. Any ideas why? When I write the actual name of the node, the script us successful:

jobImage.loadMovie("Images/" + work_arr[Job1].Image2);

Is it not possible to use a variable to target the correct node?

Thanks for your time!

Brian

This topic has been closed for replies.
Correct answer kglad

test this:



newImage = "Image" + _root.imageTarget;

jobImage.loadMovie("Images/" + work_arr[Job1][newImage]);


2 replies

kglad
Community Expert
Community Expert
December 2, 2009

what the following trace() function reveal:


newImage = "Image" + _root.imageTarget;

trace(Job1+" :: "+work_arr[Job1]+" : "+"Images/" + work_arr[Job1].newImage);

jobImage.loadMovie("Images/" + work_arr[Job1].newImage);


BrianStapleton
Known Participant
December 2, 2009

Sorry kglad, for some reason when I test this movie in Flash it experiences trouble loading my XML file but not when I open the exported SWF in Flash player. Which means I'm unable to use the trace function. However, I did load your script into a text box in the SWF and got this back:

0 :: [object Object] : Images/undefined

By the way, this was what I used to load your script into the text box:

traceTxt = Job1+" :: "+work_arr[Job1]+" : "+"Images/" + work_arr[Job1].newImage;

kglad
Community Expert
Community Expert
December 2, 2009

what's your button code?

BrianStapleton
Known Participant
December 2, 2009

Hi kglad, thanks for getting back to me.

num2.onRelease = hit2;

function hit2() {

    _root.imageTarget = "2";
    _root.Work.gotoAndPlay("NewImage");
}