A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: Accessing variables

  1. #1
    Member
    Join Date
    Oct 2006
    Posts
    36

    Accessing variables

    how do I access a variable that was created in a function? For example


    Code:
    function create()
    
    {
    
      var myvariable:string='hello world';
    
    }
    
    trace myvariable

    does anyone have a solution for this?

  2. #2
    half as fun, double the price senocular's Avatar
    Join Date
    Feb 2002
    Location
    San Francisco, CA (USA)
    Posts
    4,361
    You don't. Variables created in function are local to that function scope and cannot be accessed outside of that.

    Your options are: use a class variable (where your function is a method of that class and that variable is accessible from an instance of that class), pass an accessible object into the function where the variable is a property of that object, or have the function return the value you wish to obtain.

  3. #3
    Member
    Join Date
    Oct 2006
    Posts
    36
    I am sorry. I just do not understand that. Could someone give me a simple example.

  4. #4
    Senior Member ozmic66's Avatar
    Join Date
    Oct 2005
    Posts
    472
    Code:
    public var myvariable:string='hello world';
    
    function create(){
      myvariable="modified by create"; //modifies the variable
    }
    
    trace(myvariable); //read it outside the function too
    Pixelwave Flash-based iPhone framework
    iPhone Games: Flyloop | Freedom Run

    Twitter: Oztune

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