A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: [AS2] Edit variable through a function?

  1. #1
    cake! Skribble_Style's Avatar
    Join Date
    Jun 2002
    Location
    Australia
    Posts
    550

    [Solved][AS2] Edit variable through a function?

    Solution is in ImprisonedPride's post - Thanks again!

    Hey guys. I've hit a little snag with my project and Im not sure how to proceed.

    I have a function that is used to customize the users controls, and I pass a variable to it for updating the screen with which keys are assigned to what controls.

    For examples sake, this is the code:

    code:
    upKey = 87;

    upKeyBTN.onRelease = function(){
    setCustomKeys(upKey);
    }

    function setCustomKeys(keyToSet){
    newKey = *key pressed by user*;
    keyToSet = newKey;
    }



    What I can't seem to do is get upKey updated with the new key from inside the function. As my example shows, I tried using keyToSet = newKey, but that doesn't seem to work. I need to use upKey in a few other places in the function too, so how can I make a direct reference to it from inside the function?

    The whole point of doing it this way is so that I can customize each key with just one function.

    Any ideas on how I can achieve this? Thanks!=)


    Brendan
    Last edited by Skribble_Style; 09-05-2010 at 05:17 AM. Reason: Missing info
    Skribble

    Its not that im lazy, I just dont care.

  2. #2
    Senior Member bluemagica's Avatar
    Join Date
    Jun 2008
    Posts
    766
    I have a few theories, but I can't test it now as I am in a bit of hurry, but I would suggest you to use setter methods (as in getter/setter) to update variables.

    Code:
    _global.upkey = 87;
    
    function set upkey(val)
    {
      //whatever code
    }
    or you can use "switch case" inside the function
    If you like me, add me to your friends list .

    PS: looking for spriters and graphics artists for a RPG and an Arcade fighting project. If you can help out, please pm me!

    My Arcade My Blog

    Add me on twitter:

  3. #3
    cake! Skribble_Style's Avatar
    Join Date
    Jun 2002
    Location
    Australia
    Posts
    550
    Hmmm.

    If I have to use the set method the way you have it above for every key, then that would require each key getting it's own function, which I could have done already. The whole point of doing things the way I have it in my original post is so that I can use 1 function for all keys.

    Thanks for the reply though!
    Last edited by Skribble_Style; 09-05-2010 at 03:48 AM.
    Skribble

    Its not that im lazy, I just dont care.

  4. #4
    Pumpkin Carving 2008 ImprisonedPride's Avatar
    Join Date
    Apr 2006
    Location
    Grand Rapids MI
    Posts
    2,378
    I think I see what you're getting at here. You can target a variable on the root timeline with the following code because what you have is an issue with scope.

    Code:
    upKey = 87;
    
    
    
    upKeyBTN.onRelease = function(){
    
         setCustomKeys("upKey");
    
    }
    
    
    
    function setCustomKeys(keyToSet){
    
         newKey = *key pressed by user*;
    
         _root[keyToSet] = newKey;
    
    }
    The 'Boose':
    ASUS Sabertooth P67 TUF
    Intel Core i7-2600K Quad-Core Sandy Bridge 3.4GHz Overclocked to 4.2GHz
    8GB G.Skill Ripjaws 1600 DDR3
    ASUS ENGTX550 TI DC/DI/1GD5 GeForce GTX 550 Ti (Fermi) 1GB 1GDDR5 (Overclocked to 1.1GHz)
    New addition: OCZ Vertex 240GB SATA III SSD
    WEI Score: 7.6

  5. #5
    cake! Skribble_Style's Avatar
    Join Date
    Jun 2002
    Location
    Australia
    Posts
    550
    Ahhh! That worked a treat. Thanks a lot!!
    Skribble

    Its not that im lazy, I just dont care.

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