Welcome, Guest
  • Author Topic: Cannot load a jpg dynamically  (Read 14448 times)

    Mek

    • Server what's that
    • *
    • Posts: 3
      • View Profile
      • Email
    Cannot load a jpg dynamically
    « on: 06/14/02, 08:27 »
    Hello,
    Let me start off by saying i am a beginner to actionscript, although i have a decent amount of experience in programming.
    I am currently using Flash MX6.
    I have reviewed all the help files available in flash and read most of the recent topics on this forum, but i am still stuck with a problem.  This is probably due to my inexperience with actionscript, so i propose this question to the Forum...
    (This should be simple enough for all you Hardcore action scripters!  :)

    My aim is to load a list of JPEGS at run time and display them on a viewer(Created by Flash) one by one.
    To make matters simple ill cut down to the problem...

    When i try to load the variable list with the names of the jpegs from a text file and then ask flash to display them, this does not work.
    For example lets just load one piture name variable...

    loadVariablesNum("Index.txt", 0);
    //This loads a variable loadname=picture1.jpg

    Viewer.loadMovie(loadname);
    //This should display the jpeg "picture1.jpg" in the object "Viewer"

    So basically by assiging the contents of the text file to the variable "loadname" i should then be able to use this variable to call the movie load file.
    This does not work...

    I have tried several things...
    I tried inserting a loadname="picture1.jpg"; in the actionscript before the load, and obviously this did work.  I also tried displaying the contents of the variable "loadname" in a dynamic text field, and it displayed it correctly.
    This means that the variable is loading from the text file and that the jpeg should be displayed if the variable was being handed correctly.

    SO WHY DOES THE .loadMovie NOT RECOGNISE MY VARIABLE loadname EVEN WHEN THE DYNAMIC TEXT FIELD DOES >:(
    ???

    Thanks for your time, i tried not to be long winded, but you have to explain these things correctly.
    Cheers,


    Mek

    BurtonRider1983

    • Global Moderator
    • Systems Administrator
    • *****
    • Posts: 864
    • Rider-4-Life
      • AOL Instant Messenger - BurtonRider1983
      • View Profile
      • Flash Developer's Corner (being built)
    Re:Cannot load a jpg dynamically
    « Reply #1 on: 06/16/02, 00:04 »
    Right click on the first frame of your movie.  Put the code to declare your variable....

    var picture;

    Now right after that put the code to load the textfile....

    var picture;
    loadVariablesNum("Index.txt", 0);

    Now you should have a blank MC on your main timeline, give it the instance name "pic".   Put a button on your maintimline to trigger the load.  Now right click the button and put this code under it....

    on(release){
           loadMovie(_root.picture + ".jpg", _root.pic);
    }

    This is what the contents of Index.txt should look like....

    &picture=Johnny&

    This would cause the click of the button to load Johnny.jpg into the blank MC with the instance name of "pic".....If you run into probs them post them.  Lates.
                                                                                    -Ian

    « Last Edit: 07/17/02, 21:27 by BurtonRider1983 »

    BurtonRider1983

    • Global Moderator
    • Systems Administrator
    • *****
    • Posts: 864
    • Rider-4-Life
      • AOL Instant Messenger - BurtonRider1983
      • View Profile
      • Flash Developer's Corner (being built)
    Re:Cannot load a jpg dynamically
    « Reply #2 on: 06/16/02, 00:05 »
    Oh and one more thing....I did this in a test file to make sure that it would work because i have had problems with it before.  The first time it was recognized by a dynamic textbox and not the load command, just like with you.  I dunno what was going on, but it works for me now.

    .: Pavel :.

    • Server what's that
    • *
    • Posts: 34
    • Czech Flash Fun
      • MSN Messenger - matc12@hotmail.com
      • View Profile
      • Email
    Re:Cannot load a jpg dynamically
    « Reply #3 on: 07/10/02, 16:44 »
     ??? I tried it but it doesn't work for some reason. I would like to learn how to load an image from external file into a movieclip by pressing a button. Can anybody drop a line? Thanks, Pavel

    BurtonRider1983

    • Global Moderator
    • Systems Administrator
    • *****
    • Posts: 864
    • Rider-4-Life
      • AOL Instant Messenger - BurtonRider1983
      • View Profile
      • Flash Developer's Corner (being built)
    Re:Cannot load a jpg dynamically
    « Reply #4 on: 07/11/02, 01:58 »
    Ok...first of all...this is easier than you think.

    Just put a blank MC on your main timline of your main movie.  Give it an instance name of "pic".  Now under the button put this action....

    Code: [Select]
    on(release){
    loadMovie("picture.jpg", _root.pic, "POST");

    Mek

    • Server what's that
    • *
    • Posts: 3
      • View Profile
      • Email
    Re:Cannot load a jpg dynamically
    « Reply #5 on: 07/11/02, 03:19 »
    Hello,
    I am the guy who started this post off.  Since then i have figured out why my pictures and files do not load.  This i learnt the hard way.

    Basically i was doing everything right as far as the code was concerned, but the problem lies with the fact that you HAVE TO WAIT for the files to load.
    Flash takes time to load files and if you address them before the variables are loaded, the variables will be undefined in that moment in time.

    What you have to do to solve this is to create a few frames on your code and do a check to see if the variables or pictures have loaded.   Loop till the check confirms the loading of the variables from file and then let the code proceed to your main code block.

    Hope this helps,


    Mek

    megha

    • Server what's that
    • *
    • Posts: 2
      • View Profile
      • Email
    Re:Cannot load a jpg dynamically
    « Reply #6 on: 07/11/02, 04:19 »
    hi burton

    i just want more information on this topic of loading jpg image dynamically i just want to know how to load jpg images dynamically each time the site is refreshed for more details i would like to explain u my flash file

    Details
    my flash file contains text & image i am calling text dynamically from text file & the image remains constant but i want to change only image each time when the site is refreshed any help u can provide me

    megsy





    Right click on the first frame of your movie.  Put the code to declare your variable....

    var loadMovie;

    Now right after that put the code to load the textfile....

    var loadMovie;
    loadVariablesNum("Index.txt", 0);

    Now you should have a blank MC on your main timeline, give it the instance name "pic".   Put a button on your maintimline to trigger the load.  Now right click the button and put this code under it....

    on(release){
        loadMovie(_root. + "jpg", _root.pic);
    }

    This is what the contents of Index.txt should look like....

    &loadMovie=Johnny&

    This would cause the click of the button to load Johnny.jpg into the blank MC with the instance name of "pic".....If you run into probs them post them.  Lates.
                                                                                   -Ian



    .: Pavel :.

    • Server what's that
    • *
    • Posts: 34
    • Czech Flash Fun
      • MSN Messenger - matc12@hotmail.com
      • View Profile
      • Email
    Re:Cannot load a jpg dynamically
    « Reply #7 on: 07/11/02, 10:41 »
    I did as you said but this error came up when I published the file,

    Scene=Scene 1, Layer=Layer 2, Frame=1: Line 2: String literal was not properly terminated
        loadVariablesNum(index.txt",0);

    Scene=Scene 1, Layer=Layer 2, Frame=1: Line 3: ')' or ',' expected
       

    Generator is not enabled for this movie. C:\WINDOWS\Desktop\Untitled-1.swf
    Error opening URL "file:///C|/WINDOWS/Desktop/jpg"


    can you make a simple flash file to demonstrate the script? I have tryied all the comments and nothing really worked :( , Thanks, Pavel

    megha

    • Server what's that
    • *
    • Posts: 2
      • View Profile
      • Email
    Re:Cannot load a jpg dynamically
    « Reply #8 on: 07/12/02, 06:12 »

    hi burton

    its great to hear from u i have gone through ur reply but there is some problem for me in understanding can u pls brief the process how to dynamically change images(jpg) in flash each time the site is refreshed i got ur code according to ur code i had created 5 jpg files naming 0.jpg,1.jpg ..........etc & i had given this action loadMovie(random(4)+"jpg", _root.pic); on frame itself as i dont want it to be on click of a button but i was unable to see any images i suppose i am doing something wrong ??? can u pls brief me the procedure of doing it  

    thanks
    megsy





    Megsy....
    Man...I think that I was up a few hours too late when I posted that one you just quoted from.  Either that or it was a reply to an email I got and I made a few mistakes in my answer....Topic Summary  
    Posted by: megha Posted on: Today at 05:19:29am
    hi burton

    Right click on the first frame of your movie.  Put the code to declare your variable....

    var loadMovie;

    Now right after that put the code to load the textfile....

    var loadMovie;
    loadVariablesNum("Index.txt", 0);

    Now you should have a blank MC on your main timeline, give it the instance name "pic".  Put a button on your maintimline to trigger the load.  Now right click the button and put this code under it....

    on(release){
       loadMovie(_root.loadMovie + "jpg", _root.pic);
    }

    I seemed to have left the variable out of the load command, which probably confused quite a few people.

    Now for your text file it should be formatted like this.....

    &loadMovie=Johny&

    This should load "Johny.jpg" into your blank MC "pic".

    You can do such by using cookies....that is, if you want it to load a new pic each time for sure.  You count have the value of the cookie increment each time it is loaded and add a little actionscript to load a different pic each time.....or you could just do it the easier way and use the Math(random()) function and put the max number of pics minus one for the parameter.  You could just name your pics 0.jpg, 1.jpg, 2.jpg, etc....   You load command could be something like this if you had 5 pics....

    loadMovie(random(4)+"jpg", _root.pic);

    Mek....
    If you are using MX, you could just load into the MC like I said and make a simple preloader with the .getBytesTotal() and .getBytesLoaded() properties.  You could just use an if statement to see if the loading is complete.  Test if bytes total is not zero and is equal to bytes loaded.



    BurtonRider1983

    • Global Moderator
    • Systems Administrator
    • *****
    • Posts: 864
    • Rider-4-Life
      • AOL Instant Messenger - BurtonRider1983
      • View Profile
      • Flash Developer's Corner (being built)
    Re:Cannot load a jpg dynamically
    « Reply #9 on: 07/17/02, 20:07 »
    Megsy......

    Not sure why you posted the same quote twice without saying anything.  I got your PM.  My source file was wiped out so I will have to remake it right now.  I will post it on my server along with the FLA when I finish.

    BurtonRider1983

    • Global Moderator
    • Systems Administrator
    • *****
    • Posts: 864
    • Rider-4-Life
      • AOL Instant Messenger - BurtonRider1983
      • View Profile
      • Flash Developer's Corner (being built)
    load a jpg dynamically: source file
    « Reply #10 on: 07/17/02, 21:15 »
    http://www.mw9.net/Flash-DB/flas/dynamic_image/

    I just made a little swf for loading an image dynamically.  It takes a formatted textfile and loads its value into a variable.  That variable is the name of the jpg to be loaded on the click of the button.  Really quite simple and basic.  I am going to go work on another one to post that loads a random picture of a selected group.........



    Just added a couple more for random pictures.  If you guys would like any specific source files on this topic then post it and I will try and make them for you ASAP.
    « Last Edit: 07/17/02, 22:00 by BurtonRider1983 »

    toohinc

    • Server what's that
    • *
    • Posts: 7
      • View Profile
      • Email
    Re: Cannot load a jpg dynamically
    « Reply #11 on: 09/28/06, 03:46 »
    HI

    BurtonRider 1983, I´ve downloaded the .fla that does need the text file and it works fine. But how can I change the directory from where the image is beeing loaded and how can make a random image no matter what the name is. Just select one random  image from one directory, simple as that. Thanks very much.

     :)

    Ronald Wernecke

    • Global Moderator
    • Systems Administrator
    • *****
    • Posts: 6203
      • View Profile
      • Professional Support
      • Email
    Re: Cannot load a jpg dynamically
    « Reply #12 on: 09/28/06, 07:26 »
    Hi,
    in which environment are you working?
    If youre host supports php scripting, you can read the directory with php and send the result to flash.
    Flash generates an array and reads it with random index.
    I dont know, where you got stuck. If it is the basics, work yourself through the read/write tutorials until you understand them - and then go step by step further.
    If you have a certain question, just ask here at the boards in the appropriate section.
    happy flashing
    8)
    Ronald

    BurtonRider1983

    • Global Moderator
    • Systems Administrator
    • *****
    • Posts: 864
    • Rider-4-Life
      • AOL Instant Messenger - BurtonRider1983
      • View Profile
      • Flash Developer's Corner (being built)
    Re: Cannot load a jpg dynamically
    « Reply #13 on: 09/29/06, 15:13 »
    I added one for you at the end.  There is a link provided after the example and small explanation.  It is all of the files zipped up.  If you need it in a more Mac friendly format then post here and I will stuff-it also.  It uses a php script to loop througha specified directory and take all of the file names and send them to flash in a comma separated array.  Flash splits the array and loads an image when you press the button.  It is a random image from the directory and file names do not matter with this solution.
    http://www.mw9.net/Flash-DB/flas/dynamic_image/

    ShilohWarrior

    • Moderator
    • Senior Programmer
    • *****
    • Posts: 289
      • AOL Instant Messenger - shilohwarrio12
      • View Profile
    Re: Cannot load a jpg dynamically
    « Reply #14 on: 10/16/06, 23:13 »
    haha ian you still got mw9 up. oh man good times
    -Ryan