tags:

views:

27

answers:

1

Hi,

I created a movieclip and extended it with my user defined class. Now if I want to use that movie clip and use the properties from the class how do i go around doing that?

+2  A: 
//declare a variable of appropriate type and initialize it
var myClip:YourClipName = new YourClipName();
//now you can use myClip as if it were a movie clip variable
addChild(myClip);
myClip.x = 10;
//you can also access your custom properties/method with myClip
myClip.myAweSomeMethod(42, "Tadaa");
myClip.prop1 = "Some Value";

update for arrays:

//initialize
//n : Total_number_required
public var clips:Array = [];
for(var i:Number = 0; i < n; i++)
{
  var clip:YourType = new YourType();
  clip.x = 10 * i;
  clip.y = 20 * i;
  clips.push(clip);
}
//somewhere else in the code
//to get the clip at the kth index
var clip:YourType = YourType(clips[k]);
clip.x = 100;
clip.rotation = 90;
Amarghosh
myClip._x = 10; ?
Richard Inglis
@Richard `_x` in AS2, `x` in AS3. OP didn't specify language version, so I used AS3 (as I am more comfortable with that).
Amarghosh
Thanks for the reply @Amarghosh the problem I am facing now is for example if I need to create 10 instances of this movie clip. How do I reference them individually?
Fahim Akhter
If the number of instances required is small and each does a different action, you can declare separate variables for each of them and assign values. If you're gonna create lot of clips and all of them are gonna perform similar kind of actions (they are gonna be members of a list like structure, for example) you can declare an array and store instances to that array.
Amarghosh
You can also give them a name (eg myClip.name = "something"). Then if you have added them to a DisplayObject container as long as you refer to the correct parent container you can go. someParentContainer.getChildByName("something"); and it will return it.
Allan
@all be careful with `getChildByName` especially when you have lot of clips on stage. If you have accidentally assigned same name to multiple siblings, that method will return the first one in the display list (instead of throwing an error when duplicate names are assigned)
Amarghosh
I am using .name=String(1) etc. which works fine and works fine in a trace. But causes errors when I access its native properties like .x
Fahim Akhter
I would recommend using arrays over accessing by name property. Make sure that you have specified MovieClip as the super class.
Amarghosh
Movie clip is the superclass and my class extends it. And the container contains all the children. Can you give a link to how to make a array container?
Fahim Akhter
@Fahim see my update
Amarghosh
@Amarghosh, thanks so much for today you have been a good sport :) Saved the day.
Fahim Akhter
You are welcome :)
Amarghosh