A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: Local Variables

  1. #1
    Senior Member
    Join Date
    Mar 2010
    Posts
    107

    Local Variables

    Hi there,

    The other day I was thinking how to access local variables in more than one function in the same class. As example, we take a custom class called "Fields". My point is that I don't want to create another instance of the Fields class, but I do want to access the field variable in another function. Let me create an example of it:


    Actionscript Code:
    package
        {
            import flash.display.Sprite;
           
           
            /**
             * ...
           
             */

            public class Main extends Sprite
            {

           

              public function Main()
              {
               
                         createFields();
              }

              private function createFields():void
              {
                         
                var field:Fields = new Fields();
                addChild(field);
                           
              }
             
              private function accesFieldHere():void
              {
                  var field:Fields = Fields();
                  field.someProperty = "bla";
              }

               
            }

    This doesn't work. But i'm trying to achieve it that way. Can someone explain me why this isn't working? Or should I forget this and make the variable global? I hope I'm clarified myself enough.

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    It doesn't work because Fields is not a method, so calling it without new as you do in accesFieldHere (sic) is meaningless. If you did have "new" there, it would be a new instance of Fields, and that's apparently not what you want.

    Bottom line, if you want to use the instance of fields you create in createFields, then you need a reference to it where you want to use it. The simplest way is to make field an instance variable of your class. I'm not sure why you want to avoid that.
    But any reference would do. You could give field a name and access it with getChildByName.
    I don't suggest that.
    You could attempt to access it with getChildAt.
    I don't suggest that either.

    You could store a reference to field in some other object which is accessible in both places. But I don't see the advantage here compared to just making field an instance variable.

  3. #3
    Senior Member
    Join Date
    Mar 2010
    Posts
    107
    Thanks for your answer.

    Ok to explain the reason why I wanted to know this is because of this part of code:

    Actionscript Code:
    private function itemRollOutHandler(e:MouseEvent):void
            {
               
                var item:menuItem = menuItem (e.currentTarget);
               
                TweenLite.to (item, 0.5, { tint:0xFFFFFF } );
               
               
            }

    This little part of code belongs to a menu application. Menu is the main class, having a sub class called MenuItem. MenuItem describes one item out of the menu. The menu class loops a few times through the MenuItem class.

    Here some code of the MenuItem class:

    Actionscript Code:
    public class menuItem extends Sprite
        {
            //declare needed vars
            private var _sprite:Sprite;
            private var _id:int;   
               
            public function menuItem (title:String)
            {
                _sprite = createOneBtn (title);
                addChild (_sprite);
               
            }
           
           
            private function createOneBtn(label:String):Sprite
            {
                // create a button and pass it into a sprite.
                // It's better to make a sprite holder to hold the textfields, because sprites are easier to tween
                var navItem:TextField = new TextField();
                var format:TextFormat = new TextFormat ();
                var sprite:Sprite = new Sprite ();
       
               
                sprite.addChild (navItem);
               
                format.size = 18;
                format.color = 0xFFFFFF;
                format.font = "Calibri";
               
                navItem.selectable = false;
                navItem.autoSize = TextFieldAutoSize.CENTER;  
                navItem.defaultTextFormat = format;
               
                sprite.mouseChildren = false;
               
                navItem.htmlText = label;
               
               
                return sprite;
               
               
            }

    This is working, but why does this work and my code doesn't ?

  4. #4
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Because it's not doing anything like what your code was attempting to do. What part of it do you think is similar?

    Code:
    var item:menuItem = menuItem (e.currentTarget);
    This line is not creating a menuItem, it is casting a reference to an existing menuItem (e.currentTarget) which is typed as DisplayObject to menuItem so that it may be assigned to a variable of type menuItem.

    That's about the only thing with superficial resemblance to the code you first posted.

  5. #5
    Senior Member
    Join Date
    Mar 2010
    Posts
    107
    That explains it! So yes thats what I meant. Thanks again.

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