#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2011
    Posts
    2
    Rep Power
    0

    [ActionScript 2] Variable not changing properly from within onPress


    Hi there.

    I'm very new to Flash, but I have been learning loads and getting some good results so far. Browsing these forums has been a massive help. However today I have hit a problem in what should be almost the simplest line of code I know how to write (variable = x), and could do with some advice.

    I have a simple program containing a button which will change a variable when pressed. There is also a text-box that will output that variable. However when I press the button, the variable will change, but then immediately revert to it's previous value.


    Code:
    class Hero extends MovieClip
    {
    		
    	var mmoloaded = "A"
    		
    	function onLoad()
    	{
    		_root.loadButton.onPress = function()
    			{
    			mmoloaded = "B"
    			_root.mmoloadedText.text = mmoloaded
    			}
    	}
    	
    	function onEnterFrame()
    	{						
    		_root.mmoloadedText.text = mmoloaded			
    	}
    			
    }
    The effect is that when I press the button, it will change to "B" for a single frame, then back to "A".

    I am writing all my code from within my 'Hero', and if I make the hero itself a button with the instance name 'heroButton' and put the code in there, then the code seems to work as intended. So is it something with the button just not being able to access the variables defined in the Hero class, despite being written inside the Hero class? I'm very confused!

    Any and all advice is appreciated.

    PS. I am using FLash CS3 and ActionScript 2.0
  2. #2
  3. Editor-in-Beef
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2008
    Location
    South Florida
    Posts
    465
    Rep Power
    10
    Test
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2010
    Posts
    8
    Rep Power
    0
    test
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2011
    Posts
    2
    Rep Power
    0
    Is it something to do with the scope of the variable?

    Is it just changing a local variable while it's in the button function but not changing the real variable or something?

    If the variable doesn't exist it will error, so I figured it must be changing the same variable. But I don't get why it would only change it for the duration of the button function.

IMN logo majestic logo threadwatch logo seochat tools logo