Problem with ActionScript, Classes, and instances

Ask a Question related to Macromedia Flash, Design and Development.

  1. #1

    Default Problem with ActionScript, Classes, and instances

    Hi all,

    I am playing around with as and my own classes, to make my code les messi
    and more object oriented, now I run into a little bit of a problem I don't
    understand. nor know of from doing the same in c# or Jave, I use the
    following code to store, (or wanna to store) data related with a picture
    into a picture object, which in itself is stored within an array, so I can
    quickly access the pictures by index, the class itself is working fine, I
    can create as many instances of it, and store the data .. but if I put them
    in an array, something strange happends I don't understand, or probably I
    am so blessed with blindless that I will get kicked from the group .. hehe

    well, here it is:
    the code:

    var picArray = new Array();
    fillPicArray(30); //function just to have the filling loop out of the way
    picArray[0].setName("pic1"); //name picture one pic1
    picArray[0].categories[0]=("cat1"); //name category [0] of pic1 cat1
    picArray[1].setName("pic2"); //name picture two pic2
    picArray[1].categories[0]=("cat2"); //name category [0] of pic2 cat2

    trace(picArray[0].getName()); //to get the name of Picture one
    trace(picArray[0].categories[0]); //to get the first category stored
    in the category array of picture one
    trace(picArray[0].getCategoryAt(0)); // other way of getting same date as
    above line
    trace(picArray[1].getName()); //same for 2. picture
    trace(picArray[1].categories[0]);
    trace(picArray[1].getCategoryAt(0));

    the output of this code: ????!!!

    pic1 //ok, fine
    cat2 // why ?? I can set the name of the first instance in the array to
    be pic1, and of the second to be pic2 and they are there
    cat2 // but the category array within them seems to be shared wth all,
    since I get always the last entry I did
    pic2 //ok, fine
    cat2
    cat2

    why is it not pic1, cat1, cat1, pic2. cat2, cat2 ??? what am I missing??
    the function and classe I use are below, also, I may say, I use flash MX pro
    2004

    thanx for any help or hint, I don't get it

    Andreas




    the function
    function fillPicArray(picNumber:Number){
    for (var n = 0; n < picNumber; n++){
    picArray.push(new Picture());
    }
    }

    the class file (noting special, plain set/get data idea):

    class Picture{
    // data members
    var picName:String;
    var picPath:String;
    var picDestcription:String;
    var categories:Array=new Array();

    //class constructor
    public function Picture(){

    }

    // class method
    function addCategory(newCat:String):Void{
    this.categories.push(newCat);
    }
    function getCategories():Array{
    return this.categories;
    }
    function getCategoryAt(n:Number):Array{
    return this.categories[n];
    }

    function getCatLength():Number{
    return categories.length;
    }

    function setName(n:String):Void{
    picName = n;
    }
    function getName():String{
    return picName;
    }
    function setDescription(n:String):Void{
    picDestcription = n;
    }
    function getDescription():String{
    return picDestcription;
    }
    }


    Andreas Guest

  2. Similar Questions and Discussions

    1. charting memory problem - caching of instances
      Hi I am using an AreaChart and I have a refresh button that when clicked refreshes the AreaChart with new data from an XML file. on each...
    2. multiple instances problem with virtual sites
      We are running 4 cf instance on our dev server one per application version. Per version of the application powers multiple sites, in total 511...
    3. problem with actionscript 2.0 classes
      please don't be afraid to help me with this if you don't understand 3 points, vectors and matrixes. the problem is a actionscript 2. freak of...
    4. Instances from dynamic classes within packages
      I'm sure someone else has ran into this and was curious on the answer (if there is one) I would like to know how to create a dynamic instance of...
    5. Actionscript 2.0: Adding functions to predefined classes?
      I'm currently attempting to bring an extremely large project from Acitonscript 1.0 to Actionscript 2.0. One of the (many) stumbling blocks along the...
  3. #2

    Default Re: Problem with ActionScript, Classes, and instances

    because you created your array in the var statement in the class, it is
    common to ALL the instances (there is only one 'new' call). You need to set
    it up in the constructor so there is a separate 'new' called for each
    instance.

    ie change
    > class Picture {
    > // data members
    > var picName:String;
    > var picPath:String;
    > var picDestcription:String;
    > var categories:Array=new Array();
    > //class constructor
    > public function Picture(){
    >
    > }
    > ...
    to

    class Picture {
    // data members
    var picName:String;
    var picPath:String;
    var picDestcription:String;
    var categories : Array;
    //class constructor
    public function Picture(){
    categories = new Array();
    }
    ....
    --
    All the best,
    Jeckyl


    Jeckyl Guest

  4. #3

    Default Re: Problem with ActionScript, Classes, and instances

    aaaaahh, ... yes, the woods and the trees ,

    Thank you very very much Jeckyl .. ya saved my day :) now I can go to bed
    and sleep

    Andreas


    "Jeckyl" <Jeckyl@Hyde.com> wrote in message
    news:GLp2e.17293$C7.10030@news-server.bigpond.net.au...
    > because you created your array in the var statement in the class, it is
    > common to ALL the instances (there is only one 'new' call). You need to
    > set it up in the constructor so there is a separate 'new' called for each
    > instance.
    >
    > ie change
    >
    >> class Picture {
    >> // data members
    >> var picName:String;
    >> var picPath:String;
    >> var picDestcription:String;
    >> var categories:Array=new Array();
    >> //class constructor
    >> public function Picture(){
    >>
    >> }
    >> ...
    > to
    >
    > class Picture {
    > // data members
    > var picName:String;
    > var picPath:String;
    > var picDestcription:String;
    > var categories : Array;
    > //class constructor
    > public function Picture(){
    > categories = new Array();
    > }
    > ...
    > --
    > All the best,
    > Jeckyl
    >

    Andreas Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139