Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

object reference/pointer?

New Here ,
Aug 05, 2008 Aug 05, 2008
Hello,

I have a 'main' which dynamically creates objects from a custom class at runtime. The user can then change the properties of these objects on the stage. e.g. text, color, alpha.

My problem is that when I try and change a property of one object, such as text, every object created from that class changes in the same manner.

What is the best way of referencing one particular object that I have created and added to the stage, either by passing a reference/pointer to 'main' or using some kind of flag to remember which object or another method etc.

I have just realised that the way I create each object is "var objectName:MyClass = new MyClass();" in a function which is called when an 'add' button is clicked. So I need to differentiate between these MyClass objects which have the same name (declaration).

Thank you in advance,

TOPICS
ActionScript
660
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Aug 06, 2008 Aug 06, 2008
or you could use a property of your class items like the displayobject uses the "name" property, so you could retrieve objects using their property.
Translate
Community Expert ,
Aug 05, 2008 Aug 05, 2008
are you using a static property? that's about the only thing that would cause them all to change. otherwise, using a setter would be a typical way to change the properties of individual instances.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Aug 05, 2008 Aug 05, 2008
Hi,,, I'm not using static properties within the class. Class variables are defined as below. I've set all the class variables public so i don't have to use setters (for the moment).

public var meters:int = new int(600);

I think i might be getting into trouble because I can't reference an individual object? If the user clicks on a movieClip on the stage, I need to know which object it belongs to so that i can then change its instance properties.

I am only using one line/name to create multiple objects ( as below ). Is this wrong/impossible for me to reference the way i need to? How else might i reference a screen item to its particular object?

var myObject:MyClass = new MyClass();


Thank you.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 05, 2008 Aug 05, 2008
i have no way to know the relationship between a MyClass instance and a movieclip on stage.

are all your MyClass instances names myObject? if so, and you want to address the various MyClass instances, you've got a problem
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Aug 06, 2008 Aug 06, 2008
yes... all the class instances have the same name eg. myObject, but when the user clicks on a movieclip which belongs to a particular object, i want to know which object it belongs to so i can change its other settings. Unfortunately all objects are created with...

var myObject:MyClass = new MyClass(); // ( all with same name myObject )

Is there any way of identifying the particular object that the movieclip belongs to, or will i have to change my implementation? If so how might I create dynamic names when the user chooses to create an object?

Thank you.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Aug 06, 2008 Aug 06, 2008
There are a few things you can do:

1. You can push each newly created object into an array, where you can target them individually later.

var myObject:MyClass = new MyClass();
var myObjectArray:Array = [];
myObject.addEventListener(MouseEvent.CLICK,changeMyObject);
myObjectArray.push(myObject);

function changeMyObject(e:MouseEvent){
for(var j=0;j<myObjectArray.length;j++){
if(myObjectArray == e.target){
//change properties of said object
}
}
}


2.You can create mimic creating dynamic instance names (although you lose strict typing capabilites)

//assuming this code is on the root timeline
for(var i=0;i<5;i++){
this["object"+i] = new MyClass();
}

for(var objs in this){
trace(objs)
}

3. If you're creating the class on an object of sorts create a reference to it:
var myContainerThing;
myContainerThing.customClass = new MyClass();

Hope that makes sense and/or helps.

Cheers,
FlashTastic
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 06, 2008 Aug 06, 2008
or you could use a property of your class items like the displayobject uses the "name" property, so you could retrieve objects using their property.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Aug 06, 2008 Aug 06, 2008
quote:

Originally posted by: kglad
or you could use a property of your class items like the displayobject uses the "name" property, so you could retrieve objects using their property.


Ok, Thank you very much for the posts so far.

I've stuck the objects in an array and given them 'name' variables (strings).

This may be a stupid question, but is there a good naming convention i can use? This is because i don't know how many objects will be created at runtime. Would it be best to just numerate names? i.e. name1, name2, name3 etc.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 06, 2008 Aug 06, 2008
LATEST
yes, use something like myClassInstance1, myClassInstance2, etc that makes it obvious what those instances are even if you re-check this application a few years after you finish it.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines