A Flash Developer Resource Site

Results 1 to 19 of 19

Thread: functions...?

  1. #1
    Member
    Join Date
    Apr 2001
    Posts
    49

    functions...?

    functions...?
    Heeelp!

    I am trying to write a function to encompass the function and lengthy image loading script below....
    but its troublesome.
    As it stands it seems the code that is written first overides that which comes second, whichever way i put it, one half is nulled by the other...
    any suggestions?




    ActionScript:--------------------------------------------------------------------------------
    //Loading initial text...

    loadVariables("../flashtestupdater/fab.txt",WBfabricstext.text );

    //Loading button relevant text....

    function lv(id) {
    trace("calling text file #" + id);
    this["txtVars" + id] = new LoadVars();
    this["txtVars" + id].load("../flashtestupdater/fab" + id + ".txt");
    this["txtVars" + id].onLoad = function(success) {
    if(success) {
    trace("this.textfilevar=" + this.textfilevar);
    WBfabricstext.text = this.textfilevar;

    }else{
    trace("Load failed");
    }
    }
    }
    numButs = 24;
    for(var i=1; i<=numButs; i++) {
    this["foliobut" + i].id = i;
    this["foliobut" + i].onRelease = function() {
    trace("Button #" + this.id + " pressed");
    lv(this.id);
    }
    }




    //Loading pics to go with txt... (numbered imaages)



    foliobut1.onRelease = function()
    {
    loadit_mc.loadMovie("images/WBfolio/folioWB1.jpg");
    }
    foliobut2.onRelease = function()
    {
    loadit_mc.loadMovie("images/WBfolio/folioWB2.jpg");
    }
    foliobut3.onRelease = function()
    {
    loadit_mc.loadMovie("images/WBfolio/folioWB3.jpg");
    }
    foliobut4.onRelease = function()
    {
    loadit_mc.loadMovie("images/WBfolio/folioWB6.jpg");
    }
    foliobut5.onRelease = function()
    {
    loadit_mc.loadMovie("images/WBfolio/folioWB5.jpg");
    }
    foliobut6.onRelease = function()
    {
    loadit_mc.loadMovie("images/WBfolio/folioWB6.jpg");
    }
    foliobut7.onRelease = function()
    {
    loadit_mc.loadMovie("images/WBfolio/folioWB7.jpg");
    }
    foliobut8.onRelease = function()
    {
    loadit_mc.loadMovie("images/WBfolio/folioWB8.jpg");
    }
    foliobut9.onRelease = function()
    {
    loadit_mc.loadMovie("images/WBfolio/folioWB9.jpg");
    }
    foliobut9.onRelease = function()
    {
    loadit_mc.loadMovie("images/WBfolio/folioWB9.jpg");
    }
    foliobut10.onRelease = function()
    {
    loadit_mc.loadMovie("images/WBfolio/folioWB10.jpg");
    }
    foliobut11.onRelease = function()
    {
    loadit_mc.loadMovie("images/WBfolio/folioWB11.jpg");
    }
    foliobut12.onRelease = function()
    {
    loadit_mc.loadMovie("images/WBfolio/folioWB12.jpg");
    }
    foliobut13.onRelease = function()
    {
    loadit_mc.loadMovie("images/WBfolio/folioWB13.jpg");
    }
    foliobut14.onRelease = function()
    {
    loadit_mc.loadMovie("images/WBfolio/folioWB14.jpg");
    }
    foliobut15.onRelease = function()
    {
    loadit_mc.loadMovie("images/WBfolio/folioWB15.jpg");
    }
    foliobut16.onRelease = function()
    {
    loadit_mc.loadMovie("images/WBfolio/folioWB16.jpg");
    }

  2. #2
    ___________________
    Join Date
    May 2004
    Posts
    3,174
    try something like this instead
    code:

    m = new LoadVars();
    m.load("fab.txt");
    m.onData = function(r) {
    p = r.split("\r\n");
    for (var i = 0; i<p.length; i++) {
    b = _level0.createEmptyMovieClip("button"+i, i);
    b._x = 100*i;
    b.createEmptyMovieClip("jpg", 0);
    b.jpg._visible=false;
    b.jpg.createEmptyMovieClip("holder",0);
    b.jpg.holder.loadMovie("images/WBfolio/folioWB"+i+".jpg");
    b.createTextField("tf"+i, 1, 0, 0, 0, 0);
    b["tf"+i].autoSize=true;
    b["tf"+i].text = i+": "+p[i];
    b.onPress=function(){
    this.jpg._visible=true;
    }
    }
    };


    this is based off fab.txt being a list of items just separated by returns, like
    Code:
    item1
    item2
    item3
    whatever
    etc
    etc
    Last edited by moagrius; 11-23-2004 at 10:41 AM.

  3. #3
    Member
    Join Date
    Apr 2001
    Posts
    49
    Hmmm..
    That looks good !
    I don't fully understand it and so as it doesn't work without fiddling... could you tell me what is the "p" bit!
    (p = r.split("\r\n")
    I've not seen the rsplit thing before.
    does that split the function (r), (or fab.txt), between its commas or something along those lines ?

    I have at the moment a text box on the stage called "WBfabricstext" and an empty movie clip called "loadit_mc" int which pics and text are loaded...... Are they now redundant with this script?

    Any tips on editing - greatly appreciated as i'm not entirely sure what to change..... how this effects the other scripted function. i presume from reading through this new script its made all the old stuff redundant, but the pics are not appearing.

  4. #4
    Member
    Join Date
    Apr 2001
    Posts
    49
    Argh...

    Like a fool i got so carried away in the code i ommitted scrolling down to :

    this is based off fab.txt being a list of items just separated by returns, like

    code:--------------------------------------------------------------------------------
    item1
    item2
    item3
    whatever
    etc
    etc

    --------------------------------------------------------------------------------

    My text is simply a passage and the buttons have corresponding text files fab1.txt... etc.

  5. #5
    ___________________
    Join Date
    May 2004
    Posts
    3,174
    that code works without anything else, just open a new fla, paste it in the frame actions of the first frame, publish, close
    code:

    m = new LoadVars();
    m.load("fab.txt");
    m.onData = function(r) {
    p = r.split("\r\n");


    r and p are both arbitrary; the data recieved from the LoadVars is r (because of function(r)); we then take that and use the split command to turn it into an array of objects, with one 'cut' each time it encounters a carriage return (\r) or a end line (\n). each bit before or after becomes a distince object (an 'element' in an array) and assigned a sequential value based on order encountered
    code:

    for (var i = 0; i<p.length; i++) {


    for as many times as there are elements in the new array, do this stuff:
    code:

    b = _level0.createEmptyMovieClip("button"+i, i);


    make some empties
    code:

    b._x = 100*i;


    space them 100 px apart
    code:

    b.createEmptyMovieClip("jpg", 0);
    b.jpg._visible=false;
    b.jpg.createEmptyMovieClip("holder",0);
    b.jpg.holder.loadMovie("images/WBfolio/folioWB"+i+".jpg");


    make an empty inside the one we just made, load a jpeg into it, then turn it invisible
    code:

    b.createTextField("tf"+i, 1, 0, 0, 0, 0);
    b["tf"+i].autoSize=true;
    b["tf"+i].text = i+": "+p[i];


    make some textfields inside the same movieclip, label it accorind the the data in the text file
    code:

    b.onPress=function(){
    this.jpg._visible=true;
    }
    }
    }


    when we push the movieclip it'll make the hidden jpg visible

  6. #6
    Member
    Join Date
    Apr 2001
    Posts
    49
    This is all getting a bit above me!
    i cant get my thoughts around the method you are using....
    my copy has no breaks and is seporated into text files according to the button numbers. named; fab1.txt, fab2.txt......etc.

    i wish to load jpegs into the mc on stage called "loadit_mc" and add to the following to do so.... if possible???


    code:


    function lv(id) {
    trace("calling text file #" + id);
    this["txtVars" + id] = new LoadVars();
    this["txtVars" + id].load("../flashtestupdater/fab" + id + ".txt");
    this["txtVars" + id].onLoad = function(success) {
    if(success) {
    trace("this.textfilevar=" + this.textfilevar);
    WBfabricstext.text = this.textfilevar;

    }else{
    trace("Load failed");
    }
    }
    }
    numButs = 24;
    for(var i=1; i<=numButs; i++) {
    this["foliobut" + i].id = i;
    this["foliobut" + i].onRelease = function() {
    trace("Button #" + this.id + " pressed");
    lv(this.id);
    }
    }




    but i'm not sure of how to do this.
    i need to add another command to the function so that it calls the images into "loadit_mc"

    would that work somthing along the lines of...



    code:


    this.loadMovie("images/WBfolio/folioWB"+ id +".jpg", loadit_mc);




    placed above the "else" line in the script?????

  7. #7
    Member
    Join Date
    Apr 2001
    Posts
    49
    the above seems to work, but images dont appear, they are loading, but not appearing on stage. why might this be?

  8. #8
    ___________________
    Join Date
    May 2004
    Posts
    3,174
    because youre mixing methods and functions.

    code:

    this.loadMovie("file.jpg");


    the preceding 'this' - or preceding instance name - means replace the referenced movieclip with the loaded file. this is an example of the loadMovie method.

    if you wish to specify a target, you use the loadMovie function
    code:

    loadMovie("filename.jpg", targetmc);



    but even if you did do that, it wouldnt work as is because you keep loading the movie into the same target clip
    code:

    this.loadMovie("images/WBfolio/folioWB"+ id +".jpg", loadit_mc);



    keeps replacing loadit_mc over and over. try changing it to this
    code:

    this.loadit_mc.createEmptyMovieClip("jpg"+id, id);
    this.loadit_mc["jpg"+id].loadMovie("images/WBfolio/folioWB"+ id +".jpg");



    gl

  9. #9
    Member
    Join Date
    Apr 2001
    Posts
    49
    mmmm still no joy.....
    What your saying seems to make sence, but unfortunately makes little difference that i can see.

    i have tried your script using the function as oppposed to method system, but still no results.

    not sure what next!
    Attached Files Attached Files

  10. #10
    ___________________
    Join Date
    May 2004
    Posts
    3,174
    change
    code:

    lv(["fabbut" + i].id);


    to
    code:

    lv(this.id);


  11. #11
    Member
    Join Date
    Apr 2001
    Posts
    49

    argh

    You mean at the end? Like so...

    ActionScript:--------------------------------------------------------------------------------
    function lv(id) {
    trace("calling text file #"+id);
    this["txtVars"+id] = new LoadVars();
    this["txtVars"+id].load("flashtestupdater/fab"+id+".txt");
    this["txtVars"+id].onLoad = function(success) {
    if (success) {
    trace("this.textfilevar="+this.textfilevar);
    WBfabricstext.text = this.textfilevar;
    //loadit_mc.loadMovie("Flash/images/WBfabrics/WBfab"+id+".jpg");
    //BEST YET loadMovie("images/WBfabrics/WBfab"+ id +".jpg", loadit_mc);
    this.loadit_mc.createEmptyMovieClip("jpg"+id, id);
    this.loadit_mc["jpg"+id].loadMovie("images/WBfabrics/WBfab"+id+".jpg");

    } else {
    trace("Load failed");
    }
    };
    }
    numButs = 16;
    for (var i = 1; i<=numButs; i++) {
    this["fabbut"+i].id = i;
    this["fabbut"+i].onRelease = function() {
    trace("Button #"+this.id+" pressed");
    lv(this.id);
    };
    }


    --------------------------------------------------------------------------------

    still no pics in loadit_mc

  12. #12
    Member
    Join Date
    Apr 2001
    Posts
    49

    argh

    You mean at the end? Like so...

    ActionScript:--------------------------------------------------------------------------------
    function lv(id) {
    trace("calling text file #"+id);
    this["txtVars"+id] = new LoadVars();
    this["txtVars"+id].load("flashtestupdater/fab"+id+".txt");
    this["txtVars"+id].onLoad = function(success) {
    if (success) {
    trace("this.textfilevar="+this.textfilevar);
    WBfabricstext.text = this.textfilevar;
    //loadit_mc.loadMovie("Flash/images/WBfabrics/WBfab"+id+".jpg");
    //BEST YET loadMovie("images/WBfabrics/WBfab"+ id +".jpg", loadit_mc);
    this.loadit_mc.createEmptyMovieClip("jpg"+id, id);
    this.loadit_mc["jpg"+id].loadMovie("images/WBfabrics/WBfab"+id+".jpg");

    } else {
    trace("Load failed");
    }
    };
    }
    numButs = 16;
    for (var i = 1; i<=numButs; i++) {
    this["fabbut"+i].id = i;
    this["fabbut"+i].onRelease = function() {
    trace("Button #"+this.id+" pressed");
    lv(this.id);
    };
    }


    --------------------------------------------------------------------------------

    still no pics in loadit_mc

  13. #13
    Member
    Join Date
    Apr 2001
    Posts
    49
    code:


    function lv(id) {
    trace("calling text file #"+id);
    this["txtVars"+id] = new LoadVars();
    this["txtVars"+id].load("flashtestupdater/fab"+id+".txt");
    this["txtVars"+id].onLoad = function(success) {
    if (success) {
    trace("this.textfilevar="+this.textfilevar);
    WBfabricstext.text = this.textfilevar;

    } else {
    trace("Load failed");
    }
    };
    }
    numButs = 16;
    for (var i = 1; i<=numButs; i++) {
    this["fabbut"+i].id = i;
    this["fabbut"+i].onRelease = function() {
    trace("Button #"+this.id+" pressed");
    lv(this.id);
    loadit_mc.loadMovie("images/WBfabrics/WBfab"+id+".jpg");
    };
    }




    I have made it look for image 17 of 16 ! using the above, or similar, but cant fathom the identity thing for finding the button number and passing it to the load movie call.....?

  14. #14
    ___________________
    Join Date
    May 2004
    Posts
    3,174
    code:
    loadit_mc.loadMovie("images/WBfabrics/WBfab"+this.id+".jpg");

    Last edited by moagrius; 11-25-2004 at 03:19 PM.

  15. #15
    Member
    Join Date
    Apr 2001
    Posts
    49
    I have tried using :
    code:

    loadMovie("images/WBfabrics/WBfab"+this.id+".jpg", loadit_mc);


    before - and as posted, but this seems not to pick up the "id" property from the function. Therefore it loads, or attempts to load, WBfab.jpg on every call, ie from all buttons. No number value is added to the call.
    odd!

  16. #16
    ___________________
    Join Date
    May 2004
    Posts
    3,174
    post the fla and at least a few of the text and image files in a zip and ill fix it for you

  17. #17
    Member
    Join Date
    Apr 2001
    Posts
    49
    WOW
    here it is, .fla, plus folder setup and 1st 3 images and pics.
    thanks for the help - really very appreciated.
    Attached Files Attached Files

  18. #18
    ___________________
    Join Date
    May 2004
    Posts
    3,174
    ok you owe me lots of money. i spent 30 minutes thinking i thought i had smoked too much methamphetamine this morning; those are progressive jpegs - flash won't load them. resave as non progressive, and use the code at the end of this post (just delete everything in the frame as it is now). here are some problems:

    your paths were wrong. they're corrected below.

    you're assigning each button a variable "id" - which is attached to itself and fixed; common and good practice when you want to give mcs produced via loop a unique fingerprint.
    code:

    for (var i = 1; i<=numButs; i++) {
    this["fabbut"+i].id = i;


    however, in your function you also named the argument "id"
    code:
    function lv(id) {


    - so you, me and the flash player are all wondering what id we're supposed to use. then, inside the LoadVars statement block (anything between curly braces), 'this' now means the LoadVars data. anyhow just paste this in and resave the jpegs and you'll be ok.
    code:

    function lv(id) {
    this["txtVars"+id] = new LoadVars();
    this["txtVars"+id].load("flashtestupdater/fab"+id+".txt");
    this["txtVars"+id].onLoad = function(success) {
    if (success) {
    WBfabricstext.text = this.textfilevar;
    loadit_mc.loadMovie("images/WBfabrics/WBfab"+id+".jpg");
    }
    };
    }
    numButs = 16;
    for (var i = 1; i<=numButs; i++) {
    this["fabbut"+i]._id = i;
    this["fabbut"+i].onRelease = function() {
    lv(this._id);
    };
    }


  19. #19
    Member
    Join Date
    Apr 2001
    Posts
    49

    wot a first class ..... nit

    F....udge
    I didn't spot that, obviously.
    wot a first class ..... nit

    I did forget about the paths, but i've a couple of copies on the go and they reside in different places for reasons i cant remember i've been bashing at this page for so long..... but they at least are something i can get my head around!

    Think bashing my head with a blunt object will be this evenings entertainment.

    U R A *

    thanks so much again.

    IOU

    going curl up into a corner and rock now.



    C

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center