all groups > flash actionscript > april 2007 >
You're in the

flash actionscript

group:

Variable in loadMovie



Variable in loadMovie Tiller
4/1/2007 3:52:46 PM
flash actionscript: I've been battling with this for a whole day now. I'm using a database to store
information and then calling it back into Flash using a PHP script which
outputs all the necessary variables. All the text comes in fine but I'm having
difficulty with loading a picture into a blank movie clip (called mcNews_pic).

The picture variable (called news_image) that's output is the JPEG's path - it
comes out as: "images/news/news_1.jpg"

The ActionScript that loads in the variables is on the first frame of the
movie.

Putting the image path directly into the loadMovie line works fine, but the
variable name doesn't seem to work in there...

mcNews_pic.loadMovie(news_image); // doesn't work
mcNews_pic.loadMovie("images/news/news_1.jpg"); // works fine

The variable displays fine in a test text field I've made as well.

Any ideas? It's driving me mad... I'm sure I've done something wrong but
whatever I try doesn't seem to work.

I also want to make sure I retain Flash 5 compatibility if possible.

Any help is appreciated!

Cheers,

Andy
Re: Variable in loadMovie myIP
4/1/2007 4:06:33 PM
?news_image? is a variable name you created, when the loadMovie() is invoked it
probably is not defined at this point. Put the following trace statement right
before the loadMovie();

trace(?news_image >>> ?+news_image);

Does it come up as ?undefined?? If so you probably need to use an event
handler to invoke a function that contains the loadMovie() only when news_image
is defined.
Re: Variable in loadMovie Tiller
4/1/2007 4:24:25 PM
Hi,

Thanks for your reply.

The variable name is definitely defined as I have a text field that is set up
to receive the contents of the variable which works fine.

I did try the trace command, but since it has to be run from the server I'm
not sure how to get any output from the command through Safari.

Andy
Re: Variable in loadMovie myIP
4/1/2007 4:43:45 PM
If you are unable to test the swf thru Flash?s IDE then create a textfield that
will render the value of the variable. Like the code below;

//var news_image = "images/photos/ball.jpg"

this.createTextField("my_txt", 1, 100, 100, 300, 100);
var my_fmt:TextFormat = new TextFormat();
my_fmt.size = 16;
my_fmt.color = 0xFF0000;
my_txt.text = news_image;
my_txt.setTextFormat(my_fmt);

Re: Variable in loadMovie myIP
4/1/2007 4:47:07 PM
Re: Variable in loadMovie myIP
4/1/2007 5:03:08 PM
Re: Variable in loadMovie Tiller
4/1/2007 6:08:36 PM
Thanks for your replies!

Yes, the textfield does come out as "images/news/news_1.jpg" so that's all
fine.

I've tried outputting as Flash 6 and Flash 7 and still no joy.

I'm using loadVariablesNum (not LoadVars) as I wanted to keep compatibility
with older Flash players. Is that still okay?

Andy
Re: Variable in loadMovie Rothrock
4/1/2007 6:49:10 PM
My guess is that there is a return or some such at the end of the variable. So
it looks like it is the correct value, but isn't.

I'm not familiar with PHP, but this is a common problem for folks using
regular text files.
Re: Variable in loadMovie myIP
4/1/2007 9:05:51 PM
Rothrock brought up an interesting point that I was not aware of. If that is
the case, perhaps you can extract a certain number of characters to that
string. The code below will find the ?.? that is right before the file
extension name and will start to extract the filename with the extension. Once
it did that it will reassign/recondition the variable.

You may also want to look into the valueOf() that is a part of the String
Object. The link to the livedocs is below;

http://livedocs.adobe.com/flash/8/main/wwhelp/wwhimpl/common/html/wwhelp.htm?con
text=LiveDocs_Parts&file=00002713.html


//nonconditioned variable
var news_image = "images/news/news_1.jpg%20";

var backSlashLocation;
var dotLocation;
var my_str:String = new String(news_image);
var fileExtension:String = new String();

this.dotLocation = my_str.indexOf(".", -1);
//trace(dotLocation);

function findClosetBackSlash():Number
{
for(var i=0; i<= 20; i++)
{
if(news_image.charAt(dotLocation-i) == "/")
{
return i-1;
}
}
}
backSlashLocation = findClosetBackSlash();
//trace(backSlashLocation);

fileName = my_str.substring(dotLocation-backSlashLocation,dotLocation+4);
trace(fileName)

if(fileName == "news_1.jpg")
{
news_image = new String("images/news/"+fileName);
}

//reconditioned variable
trace(news_image);
Re: Variable in loadMovie Tiller
4/1/2007 9:16:59 PM
There doesn't appear to be any space/returns after the variable. I checked the
PHP code and that's fine. The variables produced look like the following (if it
helps):

heading=Heading here&date=1st April
2007&news_image="images/news/news_1.jpg"&caption=Text is here

Thanks for all your help so far!

Andy
Re: Variable in loadMovie Tiller
4/1/2007 10:34:09 PM
I've been looking on Google (a lot!) and found a snippet of information that
suggested a loadMovie command may need a bit of time before the data gets fed
to it.

To test this theory I set up a button (that just loaded the picture after
being clicked using the same variable as before) and it worked fine!

So by the sounds of it I need to tell it to wait until the data is loaded
before doing loadMovie. Something like onClipEvent sounds like what I need to
use. I'm not really up to speed with Flash, so I'll spend ages trying to work
that out now!

Andy
Re: Variable in loadMovie Rothrock
4/1/2007 10:45:58 PM
AHA! I completely forgot the most popular reason.

Yes, there is a time delay between when you request the loadVariablesNum and
the data arrives ??just like everything on the net.

However, Flash doesn't wait until that data comes back before it proceeds.

If you use the newer LoadVars class there is an onLoad event that is called
when the data comes back and then you can issue your loadMovie (or better yet
MovieClipLoader) command.

Otherwise you will need to use some other test to determine that the data has
loaded.
Re: Variable in loadMovie Tiller
4/1/2007 11:05:14 PM
Yes, in theory it's a great idea! I can't seem to get it to work though.

LoadVariables is on frame 1 of the timeline and I've got the OnClipEvent code
on the picture MovieClip.

This should work, shouldn't it?

on main timeline, frame 1:

loadVariables("flash_test.php", "_root");

on the mcNews_pic instance:

onClipEvent (data) {
mcNews_pic.loadMovie(news_image);
}

Thanks for all your help so far... and yes, I'd love to use some of the newer
LoadVars commands and so on, but I'm trying to keep it at Flash 5 (although I
may change my mind on that if this proves too much more hassle!).

Andy
Re: Variable in loadMovie Tiller
4/1/2007 11:16:02 PM
Ahh... I need to target the LoadVariables to the same movie clip that's got the
OnClipEvent.

So now I have to get this movie clip to pass all the variables back to the
timeline so the text boxes all fill in as they should.

Nothing's ever easy!

Andy
Re: Variable in loadMovie Rothrock
4/1/2007 11:30:40 PM
Glad this is going to work out for you.

However, if you continue to use loadVariables and and the onClipEvent type of
structure you are halfway making it harder for yourself than it needs to be.

The new styles of coding make Flash a lot easier. :)
Re: Variable in loadMovie Tiller
4/1/2007 11:33:26 PM
Yes, I can tell that! I think most people have newer versions of Flash player
these days so I'm sure I can get away with Flash 6 at least. Is there a website
that tells these sorts of stats do you know?

I'll be redeveloping the website one of these days so I'll go through and
streamline for a newer Flash at the same time.

Anyway, thanks to you and myIP very much for helping me - really appreciated!

Andy
Re: Variable in loadMovie myIP
4/2/2007 12:09:25 AM
Below is a link show Flash Player's penetration.

http://www.adobe.com/products/player_census/flashplayer/version_penetration.html

I would assume your target audience would have something greater then Flash
Player 5 by now. If you do choose to use something greater then FP 5, then use
the LoadVars class. And be sure you know the differences between onLoad and
onData event handler.
AddThis Social Bookmark Button