Categories
Featured jobs
» More ActionScript, Flash and Flex jobs.
» Advertise a job for free
Our network
Advertisement

 »  Home  »  Tutorials  »  Flash  »  Intermediate  »  Loading Images Via XML into Flash MX 2004

Loading Images Via XML into Flash MX 2004

By Neil Webb | Published 02/2/2007 | Intermediate | Rating:
Neil Webb
Neil Webb is a Flash Platform Developer currently living and working in England. He joined the site when it was just a baby - look at it now! Neil has previously worked for Cambridge University, FIFA and Hutchinson Whampoa among others.
 

View all articles by Neil Webb
Page 1 of 1

Written by: Neil Webb (www.nwebb.co.uk)
Difficulty Level: Intermediate
Requirements: Flash MX 2004
Assumed Knowledge: Having read the associated CSS in Flash & Flash/XML tutorials
File(s) to Download: imagesxml.zip

Loading Images in to Flash Using XML

Bonjour!

In my previous tutorial (the one catchily titled "Loading External XML formatted Content into Flash MX 2004") I mentioned that I would be covering how to load images into Flash via XML. I've been taking a bit of time out from Flash recently in order to party hard down at club-PHP, but I've been getting so many emails asking how to do the image thing that I thought it was about time I made good on my promise. This is just a primer but it should be enough to get you on the right tracks.

The problem with spending so much time in front of a computer is that your social life will invariably suffer, and don't fool yourself - SexyGurl_1979 who you've been chatting to on ICQ for the last two months is really a balding middle-aged male janitor from Basingstoke - and you were foolish enough to give out your address? Ha ha! So, with this in mind what better use could there possibly be for loading images in flash than trying to bag yourself a fine 'real' lady, eh?

Loading a picture in to Flash

In Flash Player 7 and later you can use the <img> tag to embed JPEG files, SWF files, and movie clips inside dynamic and input text fields. This tag is supported only in dynamic and input text fields that are multiline and wrap their text. The Flash Player does not support progressive JPEG files.

If we were keeping all of our code inside of Flash then to load a simple image into a textfield we would do something like this:

myTextBox.htmlText = "This is a picture of my best feature: <img src='backofhead.jpg'>";

This assumes you've got an html-enabled dynamic textbox and you're loading an image called "backofhead.jpg" in to Flash.
If you're loading the image into a TextArea component of the same name you would use myTextBox.text and not myTextBox.htmlText

Pictures can only be aligned left or right of the textbox. The default value is left, and if you have a fair bit of text then it should automatically wrap around the image. What you may find however, is that when you test your movie on the web your picture doesn't load in instantly, so what happens is that the text loads, doesn't wrap (because it has nothing to wrap around), and then the picture loads over the text, and it all looks a horrible mess! Never fear, you can reserve the space that the image will take up by specifying the width and height attributes for your image tag, just like you do in HTML. You will see this in the example.

Something to note about the above code is the use of quotation marks. The whole line is enclosed in double quotes, so for the SRC attribute I used single quotes, so as not to confuse the Flash parser. If you like you could use the escape charater (\) instead like this:

"This is a picture of my best feature: <img src=\"backofhead.jpg\">"; //use whatever floats your boat

That was simple enough, but when you're as gorgeous as myself, the real problem you'll encounter is deciding just which sexy picture of yourself to use ... er obviously, so, wouldn't it be nice if you could load a swf full of different images or animation into the textbox instead? Well guess what... you simply create a movieclip, give it an identifier (right-click the movieclip in the library and choose 'linkage'), and then specify the linkage-id as the source:

myTextBox.htmlText = "This is a swf: <img src='movieclip_linkage_ID'>";

When you use a linked movieclip make sure the clip's registration point is set to 0,0 else you may get some odd results.

Okay so that's the basics covered, and the rest is down to layout. That should be easy, but I've mentioned a problem with ignoreWhite in one of my other tutorials - it doesn't seem to work. I've just installed the new MM patch so that I'm using Flash 7.01 and it still doesn't seem to work, which puts a spin on this tutorial beccause the way your HTML file will appear in Flash will ultimately be affected by linebreaks and this may mean some fiddling about to get what you want.

I also tried taking advantage of whitespace by setting ignoreWhite to false, and laying out my file in several different ways, but each presented a problem, so here's what we're going to do: Rather than me writing a lengthy speil that covers every single linebreak and tag you will need to download the tutorial files nowand look at them in conjunction with the guidelines below so you can see how I had to lay it out in order for it to display properly.

First let's take a look at the finished file so we can see what we're going to do (you will need the flash 7 player to view this)

Sexy eh!
All the details regarding the date.css file have been covered in my previous tutorial(s), so we can ignore that, so has the basic setup of the Flash file, so the only thing we need to concentrate on now is sexyboy.html

You'll notice that the whole thing is surrounded in <textformat> tags. These tags have padding attributes (leftmargin='30' rightmargin='30') that I've used to pad the text on the left and right to keep it away from the sides of the textbox (TextArea component). Our headline text is enclosed in our XML <headine> tags, and so will be affected according to the Headline style we've created in our date.css

Next we have the <img> tag. Let's look at that in a bit of detail:

<img align='left' src='pics' width='100' height='100' hspace='30' vspace='20' />

I've specified the alignment first, although this isn't strictly necessary as it will default to 'left' anyway. Next we have the src, which is the linkage identifier for our movieclip in the library.

If we were loading an external image here we would simply include the image file in the same folder as the html and change the source to say realrealrealrealsrc="mypic.jpg" src="http://www.actionscript.org/resources/admin/mypic.jpg" src="http://www.actionscript.org/resources/admin/mypic.jpg" src="http://www.actionscript.org/resources/admin/mypic.jpg" src="http://www.actionscript.org/dev/admin/mypic.jpg" .

The width and height attributes are specified in order to reserve the space for the image as discussed above, and finally vspace and hspace add extra padding around the image.

Alignment is one of the trickier things about HTML in Flash, so what I did for this file is to position the text where I wanted it next to the image, and then adjusted the image's vspace attribute until both were aligned.

Stuff such as my name, age etc are enclosed within the <details> tags for css formatting purposes, and nested inside those I used my infoText class in conjuntion with <span> tags to change the colour of the values (this should all be making sense to you. If it isn't then you'll need to have a look at my previous two tutorials concerning stylesheets and XML in Flash).

You'll notice that everything inside <details></details> is on one line. This is due to the ignoreWhite problem in Flash. It's the only way I could get it to display properly without losing the text below it. Finally, I've included a few breaks and a URL link with css formatting to round things off.

This has been a whirlwind tour of using images in Flash via XML, but used in conjunction with my other tutorials it should easily give you enough to be going on with, and it may even help you find that special person ;)

Hope it helped.

Cheers,
Neil.

If you have any suggestions or comments about this tutorial please email me at neil@nwebb.co.uk
This, and other tutorials can be found on nwebb.co.uk


How would you rate the quality of this article?
1 2 3 4 5
Poor Excellent

Verification:
Enter the security code shown below:
img


Add comment

Spread The Word / Bookmark this content

Clesto Digg it! Reddit Furl del.icio.us Spurl Yahoo!

Related Articles
Comments
  • Comment #1 (Posted by Edub9 - ericwalton9edub9 at gmail.com)
    Rating
    I have built examples like these from scratch, recompiled other designers source files etc. etc. and this really is the best I have seen. I am working on these files in order to do a multi page example and if anyone wants info on it please let me know.


    Edub9
    www.HollywoosFineART.com
     
  • Comment #2 (Posted by sumaiya - info at phpjavascript.com)
    Rating
    The image is not loading if i store <img src='backofhead.jpg'> in a mysql table. I am using XML to retrieve the data.
     
  • Comment #3 (Posted by Ishaq)
    Rating
    to cope new lines I normally use
    xml.toString().replace(/[rn]+/g, "")

    this way I don't have to format individual html files.
     
  • Comment #4 (Posted by Chad - chad at 7030studios.com)
    Rating
    MMM...did I miss something or where did Neil (who is excellent by the way at this kind of stuff) put the XML file? All he did was title the thing XML and leave that part out..?
     
Submit Comment



Search Entire Site
Add to Google
Advertisements
Article Options
Latest New Articles
Set up a simple IIS Server for Flash
by Peter McBride

Day 1 at FITC Toronto 2008
by Anthony Pace

Simple reflection effect with AS2
by Jean André Mas

ActionScript.org Meets Josh Tynjala (aka dr_zeus)
by ActionScript.org Staff

Rapidly Create Online Flash Movies to Help Users Market, Sell and Support Software and Hardware
by Sabrina F

mailing list
Enter your email address:
mailing list
Subscribe Unsubscribe
© 2000-2007 actionscript.org! All Rights Reserved.
Read our Privacy Statement and Terms of Use...
Our dedicated server is hosted and managed by WebScorpion Webhosting.