A Flash Developer Resource Site

Results 1 to 15 of 15

Thread: [RESOLVED] Load variables

  1. #1
    Designer, Programmer, Musician angelhdz's Avatar
    Join Date
    Mar 2010
    Posts
    971

    resolved [RESOLVED] Load variables

    Hello. I have long time trying to achieve this. I have this riddle/enigmatic html game, with a flash button for entering the keys to suceed to the next level.
    All the buttons from all levels are working 100% because the password variable is written in the same flash button. But the thing comes when in the level 5, i want to have random keys, that only will be shown when playing a PONG game. That works too, when you beat the PONG game, a random key/password/code, appears. The problem is loading the vars for the button.

    I have a button with "btn" as instance name, and an input text field with "passW" as variable name. The keys are in a text file named Passwords2.txt
    The keys are written like this:
    PHP Code:
    &myPassword=atari
    &myPassword2=nintendo
    &myPassword3=sega
    &myPassword4=flash
    &myPassword5=shockwave
    &myPassword6=adobe
    &myPassword7=arcade
    &myPassword8=gaming
    &myPassword9=actionscript
    &myPassword10=coding 

    Then i have this in the first frame:

    Actionscript Code:
    loadVariables ("Password2.txt");

    and in the button:
    Actionscript Code:
    on(release){
       
     if (passW == myPassword || passW == myPassword2 || passW == myPassword3 || passW == myPassword4 || passW == myPassword5 || passW == myPassword6 || passW == myPassword7 || passW == myPassword8 || passW == myPassword9 || passW == myPassword10) {

    getURL("nivel6.htm", "_self");
    }
       
    else {

    getURL("error.htm", "_self");


    }
        }

    The only thing that works is, if you type anything wrong, it goes to error.htm. That's ok. But when you type the correct keys (adobe, atari, flash...) it goes to error.htm TOO! Also, when there is nothing in the input text box, and you press the button, it goes to nivel6.htm!!!! Without writing any key!

    Ideas? Help? Thanks

  2. #2
    Prid - Outing Nig 13's Avatar
    Join Date
    Jul 2006
    Location
    Norway
    Posts
    1,864
    Problem is that you gave your textfield a Variables Name, and in it will the formatting be included, so it will rather look something like this:

    Code:
    <TEXT type="something" size="2">myPassword</TEXT>
    instead, give it an Instance Name, 'cause then you can access all of the text field's properties, so just target its .text property to retrieve the content of your text field:

    Actionscript Code:
    if (passW.text == myPassword || passW.text == myPassword2 // etc...

    Hope this helps

    I know this, because this occured to me when I tried CS5 after Flash 8, and then comparing the Variable name didn't work, and it was driving me crazy
    I am back, guys ... and finally 18 :P

    BRING BACK THE OLD DESIGN!! OR AT LEAST FIX THE AS TAGS

  3. #3
    Designer, Programmer, Musician angelhdz's Avatar
    Join Date
    Mar 2010
    Posts
    971

    Load Variables

    Ok thanks. Let me try it!
    Last edited by angelhdz; 04-11-2012 at 03:09 PM.

  4. #4
    Designer, Programmer, Musician angelhdz's Avatar
    Join Date
    Mar 2010
    Posts
    971
    I achieved to Load the variables correctly, but I can only view them with "traces".
    Actionscript Code:
    varReceiver = new LoadVars();
    // load the variables from the text file
    varReceiver.load("Passwords2.txt");
    // trigger something - when the variables finish loading
    varReceiver.onLoad = function(){
      //the variables have finished loading
      //do something...
      trace(this.myPassword); // you use the this keyword to call the variables stored in the object varReceiver
      trace(this.myPassword2);

    };

    How do I import them now into the first frame or something, so I can call them as:

    Actionscript Code:
    btn.onPress=function(){
       
    if (passW.text == myPassword || passW.text == myPassword2 ){

    getURL("nivelseis.htm", "_self");
    }
    else  {

    getURL("error.htm", "_self");


    }
        }

  5. #5
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,780
    Hi,

    Try

    Actionscript Code:
    if (passW.text == this.myPassword || passW.text == this.myPassword2 ){

    my hobby

  6. #6
    Designer, Programmer, Musician angelhdz's Avatar
    Join Date
    Mar 2010
    Posts
    971
    Quote Originally Posted by fruitbeard View Post
    Hi,

    Try

    Actionscript Code:
    if (passW.text == this.myPassword || passW.text == this.myPassword2 ){

    my hobby
    Tried that already hehe. As the traces use "this.myPassword" I thought "well, I will use that too, to call the external variables.
    Actionscript Code:
    if (passW.text == this.myPassword || passW.text == this.myPassword2 ){
    ...And nothing happened. I keep being redirected to error.htm instead of nivelseis.htm.

    I tried
    Actionscript Code:
    if (passW.text == myPassword || passW.text == myPassword2 ){

    I tried
    Actionscript Code:
    if (passW.text == _root.myPassword || passW.text == _root.myPassword2 ){
    Nothing.

    Also i tried to create 2 new dynamic text boxes, to load the variables there, so the
    Actionscript Code:
    if (passW.text == newVar ){
    call that new variable, and the new variable load the external variables as this:

    Actionscript Code:
    //The dynamic text box is called "Caller" as variable name, and the code goes likes this:
    if (passW.text == Caller  || passW.text == Caller2 ){    

    //And the new Variable calls the external variables on the .txt file
    Caller = this.myPassword;
    Caller2 = this.myPassword2;

    Nothing either. I'm very fustrated with this, because I have another thing that i achieved, loading external "Random" text from a .txt file, and that works 100%. If I put the external variables, in the firstframe, the proyect works 100%. I succeed in loading the external variables, as you can see, i can view them with "TRACES" in the output panel, but I CAN NOT use them in the code. Thanks for the help, hope somebody lead me in the right direction.
    Last edited by angelhdz; 04-10-2012 at 03:50 PM.

  7. #7
    Prid - Outing Nig 13's Avatar
    Join Date
    Jul 2006
    Location
    Norway
    Posts
    1,864
    Send the values to _root, and then call the same values in the if statement:

    Actionscript Code:
    varReceiver = new LoadVars();
    // load the variables from the text file
    varReceiver.load("Passwords2.txt");
    // trigger something - when the variables finish loading
    varReceiver.onLoad = function(){
      //the variables have finished loading
      //do something...
      _root.myPassword = this.myPassword; // you use the this keyword to call the variables stored in the object varReceiver
      _root.myPassword2 = this.myPassword2;

    };

    Actionscript Code:
    if (passW.text == _root.myPassword || passW.text == _root.myPassword2 ){
    I am back, guys ... and finally 18 :P

    BRING BACK THE OLD DESIGN!! OR AT LEAST FIX THE AS TAGS

  8. #8
    Designer, Programmer, Musician angelhdz's Avatar
    Join Date
    Mar 2010
    Posts
    971
    Nothing. I'm going to cry! Hehe.

    If I put the variables in the first frame, instead of in the external .txt file, It works 100%!
    Example:
    Actionscript Code:
    var myPassword = "atari"
    var myPassword2="nintendo";

    btn.onPress=function(){
       
    if (passW.text == myPassword || passW.text == myPassword2 ){

    getURL("nivelseis.htm", "_self");
    }
    else  {

    getURL("error.htm", "_self");


    }
        }
    That works! But the issue is when loading the variables from the text file
    PHP Code:
    &myPassword=atari&myPassword2=nintendo
    Actually, FLASH LOADS THEM!!!!!! I can see them in the output panel!!!!! But i CAN'T call them with the
    Actionscript Code:
    if (passW.text == _root.myPassword || passW.text == _root.myPassword2 ){
    !!!!!!!!!!!!!!

    I've tried using
    Actionscript Code:
    _global.myPassword _global.myPassword2
    I tried _root.myPassword, i tried varReceiver.myPassword, i tried to load the variables within another variables, and either!!!! :'(

    The one that comes with a solution, will be GREAT before my eyes hehe! Thanks

  9. #9
    Designer, Programmer, Musician angelhdz's Avatar
    Join Date
    Mar 2010
    Posts
    971
    OKAY people. I did it. Just for the record, and for anyone who tries to load variable from a .txt file, do ALL THE PREVIOUS CODE ABOVE
    Actionscript Code:
    varReceiver = new LoadVars();
    // load the variables from the text file
    varReceiver.load("Passwords2.txt");
    // trigger something - when the variables finish loading
    varReceiver.onLoad = function(){
      //the variables have finished loading
      //do something...
      trace (this.myPassword); // you use the this keyword to call the variables stored in the object varReceiver
    trace (this.myPassword2);

    };

    And now comes the tricky part...
    Actionscript Code:
    if (this.passW.text == this.myPassword || this.passW.text == this.myPassword2 ){

    I never imagined that a 4 letters word would drive me crazy!

    Just add the "this" before the textbox's instance name too.

    Thanks Nig 13, thanks fruitbeard.

  10. #10
    Prid - Outing Nig 13's Avatar
    Join Date
    Jul 2006
    Location
    Norway
    Posts
    1,864
    Oh, good that you've sorted the problem, but adding "this" depends completely on the structure of one's flash file, you just gotta learn how path works in flash, and once you've grasped that, it's really easy to tell when to add the prefix "this", "_parent" or "_root"
    I am back, guys ... and finally 18 :P

    BRING BACK THE OLD DESIGN!! OR AT LEAST FIX THE AS TAGS

  11. #11
    Designer, Programmer, Musician angelhdz's Avatar
    Join Date
    Mar 2010
    Posts
    971
    No. It was 6am, and I didn't test the code enough because i was sleepy. It isn't solved yet :'( , how do I put this thread as "unsolved" again?

    I thought putting "this" before the input textbox instances name in the "if" script have done the thing, but not. Now, if you press the button "btn" without typing anything, it goes to nivelseis.htm. !!! That's nuts!!! I return to the "fustrated" mood again

  12. #12
    Designer, Programmer, Musician angelhdz's Avatar
    Join Date
    Mar 2010
    Posts
    971

    Here are the files

    Here is my .fla and my .txt file, so anyone can look for the problem. http://sofistica2.zzl.org/Passwords2.txt


    http://sofistica2.zzl.org/passwordsnivel5.fla
    Last edited by angelhdz; 04-11-2012 at 02:30 PM.

  13. #13
    Designer, Programmer, Musician angelhdz's Avatar
    Join Date
    Mar 2010
    Posts
    971
    I realized where the problem is. I put the variable trace in the onPress of the button, and I got the variable "undefined" , that means the problem is "location". I don't know where the variables are loaded, i tried _global, _level0, _root, _parent, this, and nothing. It's just that, once I achieved to identify the location where are the variables loaded, voilá! It will works.

    Note: I'm noticing that the variables are loaded in the varReceiver object, how can I call the variable there? If is not with the varReceiver object, then it has to be something like _level0, _root, etc.

  14. #14
    Prid - Outing Nig 13's Avatar
    Join Date
    Jul 2006
    Location
    Norway
    Posts
    1,864
    Notice in your trace that there is a linebreak (enter/blank space) between each traced variable from the text file, this is not a coincidence, there's actually a linebreak/enter included in each variable, which is preventing you from entering the correct password due to the enter/linebreak included at the end. I don't know why this is happening, but to remove the linebreaks, simply before every &, place your pointer before it, and press backspace once, and you'll see that nothing is removed, but in reality, a hidden linebreak/enter is removed, or just copy and paste this line to your text file:

    Code:
    myPassword=atari&myPassword2=nintendo&myPassword3=sega&myPassword4=flash&myPassword5=shockwave&myPassword6=entertainment&myPassword7=arcade&myPassword8=gaming&myPassword9=actionscript&myPassword10=coding
    Although, your code will still not work, because in your Button code, you are referring your password as, this.myPassword, which is wrong. When you type, this.myPassword in the LoadVars onLoad event, you are actually saying, varReceiver.myPassword, so this in this case, inside your LoadVars, merely refers to the LoadVars itself, because the received variables are saved inside that object. When you type this.myPassword in onPress event for your button, this then refers to your button, so it actually says, btn.myPassword, but you have no variables saved inside your button, which is why you're code is not working either!

    To solve it, as stated earlier, saved the passed variables from the txt file, to other variables in Flash which are accessible throughout the whole level/path. Just save each passed variable to its own variable in Flash:

    Actionscript Code:
    varReceiver.onLoad = function(){
        myPassword = this.myPassword;
        myPassword2 = this.myPassword2;
        myPassword3 = this.myPassword3;
        myPassword4 = this.myPassword4;
        myPassword5 = this.myPassword5;
        myPassword6 = this.myPassword6;
        myPassword7 = this.myPassword7;
        myPassword8 = this.myPassword8;
        myPassword9 = this.myPassword9;
        myPassword10 = this.myPassword10;
    };

    Then those 10 variables are accessible elsewhere as well. So, you're if statement would now look like this:

    Actionscript Code:
    if (passW.text == myPassword || passW.text == myPassword2){

    Alternatively, you could simply refer to the variables inside LoadVars, directly:

    Actionscript Code:
    if (passW.text == varReceiver.myPassword || passW.text == varReceiver.myPassword2){

    Hope this helps
    I am back, guys ... and finally 18 :P

    BRING BACK THE OLD DESIGN!! OR AT LEAST FIX THE AS TAGS

  15. #15
    Designer, Programmer, Musician angelhdz's Avatar
    Join Date
    Mar 2010
    Posts
    971
    Now solved FINALLY!!!!!!!!!!!!!!!!!!!!!!!!!!!! Thanks to Nig 13.

    Actionscript Code:
    if (passW.text == varReceiver.myPassword)
    didn't work, but
    Actionscript Code:
    if (passW.text == myPassword
    without the "varReceiver" object, that worked 100%, but the problem was in the .txt file. As you said, i delete the spaces between the variables on the text file, joining them together
    PHP Code:
    myPassword=atari&myPassword2=nintendo&myPassword3=sega&myPassword4=flash&.....etc 
    and violá. NOW I QUARANTEE THE PROBLEM IS SOLVED. Thank you very much.

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