A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: something i don't get re classes

  1. #1
    Senior Member Boombanguk's Avatar
    Join Date
    Jun 2000
    Posts
    1,194

    something i don't get re classes

    Hi,

    simple this for someone who knows!, I'm putting this as one of the properties of my class

    var Tiles:Array = new Array();

    but when I try to add to that array with a method, Tiles is nowhere to be found in my object, its only when I copy that line again into the constructor that Tiles suddenly appears, so my question is, why is that? and then does that mean I only need to declare my array in the constructor?

    thanks for any help
    boombanguk

  2. #2
    FK Slacker
    Join Date
    Jun 2000
    Location
    vancouver
    Posts
    3,208
    You should always initialize an array in the constructor...inline declaration like that will result in a single instance of the array which is shared by all instances of that class - if any one of them modifies the array, all instances will reflect the changes...
    code:

    class myClass{
    private var tiles:Array;

    public function myClass(){
    tiles=[];
    }
    }



    K.

  3. #3
    Senile member! :)
    Join Date
    Dec 2001
    Location
    Saunaswamp (transl)
    Posts
    2,296
    Don't instantiate the array (new Array) when you declare the varaiable in the class file. Instead that code should go into the class constructor. If you assign an instance to a var where you create it in the class declaration. This reference will become global for all instances of the class.


    e.g:
    Code:
    class myClass
    {
        // This declaration is left as-is
        var Tiles:Array;
    
        public function myClass()
        {
            // Instantiate the new array here.
            Tiles = new Array();
        }
    }

    If you declare the variable with var statement and all inside the constructor the variable vill be local to that method and is deleted as soon as the constructor is done.


    EDIT:Beaten by "deadbeat"...

    /Mirandir
    Last edited by Mirandir; 05-11-2006 at 03:02 PM.

  4. #4
    Senior Member Boombanguk's Avatar
    Join Date
    Jun 2000
    Posts
    1,194
    thanks for the info.

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