A Flash Developer Resource Site

Results 1 to 8 of 8

Thread: scope question

  1. #1
    Flashmatics silentweed's Avatar
    Join Date
    Mar 2005
    Location
    London, UK
    Posts
    4,876

    scope question

    I have the following code:

    Code:
    start();
    function start():Void{
    	var someNumber:Number = 15;
    	}
    	
    _root.onEnterFrame = function():Void{
    	trace(someNumber); //this gives undefined which is fine and i understand
    }
    However what i dont understand is when i modify the code to :

    Code:
    start();
    function start():Void{
    	someNumber = 15;
    	}
    	
    _root.onEnterFrame = function():Void{
    	trace(someNumber); // This actually gives the value 15 instead of undefined like i expected
    }
    Does anybody know why this is so? cheers in advance
    Flashmatics | Flashmatics Library (free downloads)
    -----------------------------------------------------
    Latest Projects:
    The Dead Room | Are You Smarter | Prison Break (Frame A Friend) | Beck's Fusions | The 24 Experience

    -----------------------------------------------------
    Flash Certified Developer

  2. #2
    Senior Member cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,449
    var someNumber:Number = 15;

    Here you are declaring a local variable only for that function. Will stay within that function only.

    someNumber = 15;

    here it is assumed you have declared the variable somewhere else before already. It is not local to the function any more.
    - The right of the People to create Flash movies shall not be infringed. -

  3. #3
    Flashmatics silentweed's Avatar
    Join Date
    Mar 2005
    Location
    London, UK
    Posts
    4,876
    oh thanks for that..so even though i have not declared it before, it just ASSUMES I have declared it?
    Flashmatics | Flashmatics Library (free downloads)
    -----------------------------------------------------
    Latest Projects:
    The Dead Room | Are You Smarter | Prison Break (Frame A Friend) | Beck's Fusions | The 24 Experience

    -----------------------------------------------------
    Flash Certified Developer

  4. #4
    Senior Member cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,449
    In AS1 you can do that without the var declaration. In AS2 class file you would get an error, if the var had not been declared before.
    - The right of the People to create Flash movies shall not be infringed. -

  5. #5
    Flashmatics silentweed's Avatar
    Join Date
    Mar 2005
    Location
    London, UK
    Posts
    4,876
    Cheers for that but if you look at the code (which is in it's entirety) below:
    Code:
    start();
    function start():Void{
    	someNumber = 15;
    	}
    	
    _root.onEnterFrame = function():Void{
    	trace(someNumber); // This actually gives the value 15 instead of undefined like i expected
    }
    I have NOT actually declared the variable someNumber before and yet i get no error and it traces out the correct value. (Also the publish settings are set to AS2 and player 7)

    If you simply copy and paste the above code in a new flash movie you will see what i mean
    Last edited by silentweed; 01-29-2006 at 12:34 PM.
    Flashmatics | Flashmatics Library (free downloads)
    -----------------------------------------------------
    Latest Projects:
    The Dead Room | Are You Smarter | Prison Break (Frame A Friend) | Beck's Fusions | The 24 Experience

    -----------------------------------------------------
    Flash Certified Developer

  6. #6
    Flashmatics silentweed's Avatar
    Join Date
    Mar 2005
    Location
    London, UK
    Posts
    4,876
    bump
    Flashmatics | Flashmatics Library (free downloads)
    -----------------------------------------------------
    Latest Projects:
    The Dead Room | Are You Smarter | Prison Break (Frame A Friend) | Beck's Fusions | The 24 Experience

    -----------------------------------------------------
    Flash Certified Developer

  7. #7
    Qwaizang:syntax_entity_ Q__Hybrid's Avatar
    Join Date
    Aug 2005
    Posts
    270
    Silent,

    The Interpreter in ActionScript automatically scopes all variables within a function that are not declared with "var" to the timeline that contains the function, not the function itself.

    It's a quick and dirty shorthand used to access variables that are both local and external from within a function body, whether the variables already exist or not.

    That's why it's always good to qualify your references. It's easy for things like this to ruin a project.
    +Q__

  8. #8
    Flashmatics silentweed's Avatar
    Join Date
    Mar 2005
    Location
    London, UK
    Posts
    4,876
    Thanks for that Q was'nt aware of that!! Cheers 4 the help
    Flashmatics | Flashmatics Library (free downloads)
    -----------------------------------------------------
    Latest Projects:
    The Dead Room | Are You Smarter | Prison Break (Frame A Friend) | Beck's Fusions | The 24 Experience

    -----------------------------------------------------
    Flash Certified Developer

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