Ask a Question related to Macromedia Flash, Design and Development.
-
Andreas #1
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
-
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... -
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... -
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... -
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... -
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... -
Jeckyl #2
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
to> class Picture {
> // data members
> var picName:String;
> var picPath:String;
> var picDestcription:String;
> var categories:Array=new Array();
> //class constructor
> public function Picture(){
>
> }
> ...
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
-
Andreas #3
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
>> to>> class Picture {
>> // data members
>> var picName:String;
>> var picPath:String;
>> var picDestcription:String;
>> var categories:Array=new Array();
>> //class constructor
>> public function Picture(){
>>
>> }
>> ...
>
> 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



Reply With Quote

