View Full Version : getting instance name
new2oop
09-18-2008, 06:26 AM
Even if I define an instance in actionscript:
var circle2:Sprite = new Sprite();
I still can't refer to it by name unless I also do:
circle2.name = "circle2";
Is there a way to read the valiable name directly. eg. this only works if I have also 'named' the movieclip:
function clicked(event:MouseEvent):void {
switch(event.currentTarget.name) {
How do I recognise the clip by it's variable name?
fx.barrett
09-18-2008, 06:39 AM
Flash automatically names your variable, to something like "instance34" or "instance3" you can find out the default name by tracing the name of the variable without setting it manually: trace(circle2.name); // it will display the default name
Naming variables manually is good because that way you can reference then easier, especially if you name more then 1 variable using a well defined pattern ( with a for loop ). Anyway, no one is forcing you to use a name, you can simply refer to the instance "circle2" as you'd do normally but in situations like the one you posted, it's better to name the variable.
new2oop
09-18-2008, 06:56 AM
That's the bit I don't understand. I'm already 'naming' it with
var circle2
but can't seem to refer to that in relation to the
event.target.????
new2oop
09-18-2008, 07:00 AM
I'm working with an externally loaded swf and although I have the source to that swf I would rather not alter it. Since they haven't 'named' their movieclips I'd like to reference them in the mouse click function. Can't I read the original var name somehow?
fx.barrett
09-18-2008, 07:06 AM
No, "var circle2" isn't naming anything... by writing "var circle2:Sprite = new Sprite();" you are simply creating a reference to a Sprite object. You are doing this in order to have access to the sprite instance, you could have easily just said "new Sprite();" but without assigning a reference you wouldn't have had a mean to control it.
In order to control your object you are creating a reference called "circle2". But that doesn't mean that the variable's name is the same as the reference. Remember how you give a movie clip a linkage name... When you create the movie clip you can name it ( for example: myMovieClip ) and that's how it will show in the library, under the name of "myMovieClip". But don't forget that you can give it an instance name too, which can be anything, for example: "Dog". So you have 1 object with 2 different names, each one serving it's purpose. Kinda the same thing applies to variables, you are creating a reference but you can name it whatever you want.
Try this:
var circle2:Sprite = new Sprite();
trace(circle2.name); // it will display something like: instance1
circle2.name = "myCircle";
trace(circle2.name); // now that you have named it, it will display: myCircle
// from this point on you can call your variable by using it's custom name
trace(typeof getChildByName("myCircle")); // displays: object
I hope you understand now.
Can't I read the original var name somehow?
As I said before, if they didn't name it then Flash named it for them but you have no idea of knowing the name of the variable unless you first trace it's name trace(yourVar.name); that will display the default name of the variable and you'll be able to use it from that point on. But as I already said, this is a bad practice, mostly because you have no idea of the variables' name without tracing it first so you can't set up any conditions without knowing the name of the variable. You must first name it manually and you can use it's name after.
Or, if you don't want to name it, then you can use getChildAt(); to get a specific child with a specific index, example: getChildAt(2); will return the display object situated on the 2nd index in the display list. If there's no child on that index, it will throw and error.
Prasad Sivanandan
09-18-2008, 08:05 AM
Hi..,
import flash.events.Event;
var s:Sprite=new Sprite();
s.name="S";
this.addChild(s);
function handleActivate(e:Event)
{
trace(e.target.name);
}
s.addEventListener(Event.ACTIVATE,handleActivate);
this code is working fine...
//============================================
Sprite is a Static class. If it is not fine u can inherit the Sprite class by creating a new Dynamic class so that u can add a public property to the object of the Extended Sprite(SpriteXT) from outside the class..
You can achieve the required result by creating a new property something like "id", ie id="Circle2";
hope it s helpful to u..!!
//=============================================
here is the code
// Xtended Sprite Class
package
{
import flash.display.Sprite
public dynamic class SpriteXT extends Sprite
{
public function SpriteXT ()
{
super ();
}
}
}
//===================
main fla
import flash.events.Event;
var s:SpriteXT=new SpriteXT();
s.name="S";
s.id="Iname";
this.addChild(s);
function handleActivate(e:Event)
{
trace(e.target.name);
trace(e.target.id);
}
s.addEventListener(Event.ACTIVATE,handleActivate);
Thanx
Prasad
Prasad Sivanandan
09-18-2008, 09:16 AM
can u place the code that handles the loading of external swf..
new2oop
09-18-2008, 10:43 PM
Thanks, that explains the issue for me. Perhaps I am approaching it from the wrong direction. I will create a demo of the issue and upload it.
This is an awesome forum. Amazingly helpful. Hopefully as I gain experience I can give back with answering some posts.
|
vBulletin® v3.8.5, Copyright ©2000-2012, Jelsoft Enterprises Ltd.