PDA

View Full Version : get instance name?


EnSanity
05-18-2003, 01:44 AM
Is there some type of method that on (press) you could find out what the instance name is of the variable clicked?

example: there is a button which has an instance name of myButton

- now when i click it do something like

instancename = getVariableClicked();
and instancename would get the value myButton
i dont know if there is a method like this, is there?

Thanks!

CyanBlue
05-18-2003, 02:01 AM
Howdy...

You can use _name to get the instance name, but you cannot reset/change the instance name via script...

EnSanity
05-18-2003, 03:36 AM
:-D dont need to!

EnSanity
05-18-2003, 03:59 AM
hrmmmm BLUE, how does this ._name function work? i've tried a couple different ways and havent gotten it to work. I have read in my flash book that
but quite honestly, using the _name property to determine the name of a movie clip instance would be rather silly. After all, to examine or set an object's properties, you would already need to know the name of the object. And if you already know the name of an object, the _name property will have the value that you already know.

Well that would defeat the purpose of the property now wouldnt it? If it cant tell the name of the object being selected, then it cant help me. Can it in someway that im missing?

Example: say the button's instance name is button1 and

on release i want the variable instname to get the instance name of this button. So if everything worked instname would equal "button1" can you help me get this?

CyanBlue
05-18-2003, 04:16 AM
Well... I am not quite sure what you are trying to do there...

Let's make it simple... :)

Create a button on the stage, and give it 'Btn_Test' as an instance name...
In frame 1 of the movie, add this script...Btn_Test.onPress = function ()
{
trace(this._name);
}Run the movie and click on it, and see the output panel...

Is it what you are looking for???

EnSanity
05-18-2003, 04:46 AM
This is what i am trying to do: I am letting the user dynamically create buttons, so the only thing i know about the instance name is that there will be a w to start it a number proceeding it (ex. w12) so at some point during my script i want the user to be able to add text associated to each button. So basically i need to be able to get the instance name to modify the text associated with it. Do you follow? Since i am duplicating these "buttons" from a main movie clip i could add a generic script to that button saying like

on(release){
instname = this._name;
}


would that work?

Regards,
EnSanity

EnSanity
05-18-2003, 04:57 AM
hrmm no that would be too easy. wow - that is the most pointless function EVER! to make that work i would have to go

trace(yay._name);

which wold give me the output "yay"....... how stupid. Do you have any idea how i could accomplish what i stated i needed to do in the previous post? Im stuck! - EnSanity

CyanBlue
05-18-2003, 08:38 AM
Hm... Obviously I am not getting what you are saying... :D

So, you have buttons on the stage via duplicateMovieClip() function... And, you want to know the instance name of the selected/pressed movieclip, and save it to the variable???

Your script should do what I've just said... ;)on (release){
instname = this._name;
}I don't understand what you meant by 'too easy'... Do me a favor and post your code or sample file... ;)

EnSanity
05-18-2003, 07:57 PM
well blue, i have found out how to make it work.

on the one being duplicated i added this code:

_root.instFunction(this);

and on the root timeline i added


function instFunction (obj){
_root.instname=obj._name;
trace (instname);
}


aparently my book The Complete Reference: Macromedia Flash MX was wrong. In it was that quote i posted a few posts ago about _name property being worthless. I have since then written them a letter and figured out my problem. But i do have a question i can ask of you. Right now i have a delete button and on it is the code

on (release)
{
removeMovieClip(_root.instname);
}

Now for some reason this does not work. instname gets the value once a copied button is clicked. How should i fix this?

CyanBlue
05-18-2003, 09:14 PM
Glad you got it working... ;)

removeMovieClip() function will only delte instances of the movieclip that has been laid on the stage via attachMovie() function or duplicateMovieclip() function... Make sure of that... and also make sure that _root.instname is within the scope... and try this code...on (release)
{
trace("_root.instname = " + _root.instname);
_root.instname.removeMovieClip();
// or
_root[instname].removeMovieClip();
}Let me know...

catbert303
05-18-2003, 09:25 PM
Hi,

_name doesn't provide a reference to the movie clip in question, it is just a string containing the name of the clip, so for a clip with the instance name

myClip

_name

is

"myClip"

CyanBlue's second script should do the job perfectly for removing the clip :) (assuming it is on the main timeline) since when the array the array access operators [] are used they use a string to look up the object

_root["myClip"].removeMovieClip();

is valid

_root."myClip".removeMovieClip();

is not.

A quick side note, if you need to remove a movie clip created by hand in flash dynamically you can do it by first using swapDepths on it and then removeMovieClip.

EnSanity
05-19-2003, 12:17 AM
Well the trace, traces it correctly. instname retains the correct instance name everytime you click a different button within an mc, and these duplicates are being created using the duplicateMovieClip method. I tried both of the removeMovieClip methods you posted but neither of them worked. I am posting my fla so you can indeed see what i am looking at. - EnSanity

CyanBlue
05-19-2003, 02:25 AM
Quck sample for you... See if you can use this technique...cols = 6;
rows = 5;
butHeight = 40;
butWidth = 40;
i = 0;
xOffset = 100;
yOffset = 100;
for (var c = 1 ; c <= cols ; c++)
{
for (var r = 1 ; r <= rows ; r++)
{
butMaxNumber = cols * rows;
if (i < butMaxNumber)
{
trace("i = " + i + "\tbutMaxNumber = " + butMaxNumber);
this.attachMovie("but", "but" + i, i + 100);
this["but" + i]._x = r * butWidth + xOffset;
this["but" + i]._y = c * butHeight + yOffset;
this["but" + i].onRollOver = function ()
{
trace("this button : " + this._name);
}
this["but" + i].onRelease = function ()
{
trace("\tDeleting button : " + this._name);
this.removeMovieClip();
}
i++;
}
}
}

EnSanity
05-19-2003, 03:29 AM
im using duplicateMovieClip and then trying to remove them, im on a different computer now so let me try to open my flash file, are you on flash 5? cause i believe flash mx settings may be on. I just downloaded my file and it worked fine, let me try uploading a different version of it. I like your techniques and will definetly take a much closer look after replying to this thread. I understand what your doing in your code which is a plus, but i dont know if removing buttons that are in movie clips is different when they are added with the attachMovie() versus the duplicateMovieClip. See if you can download this .fla and check it out. Thanks - Jeff

CyanBlue
05-19-2003, 04:04 AM
Sorry... No offece, but I ain't got a clue on what's going on in that file... :(

EnSanity
05-19-2003, 04:15 AM
Hrm, basically when u click duplicate it plays the second frame on in the actions movieclip which is located top left corner of the stage. The movie clip being duplicated is located above the scene next to text "being copied". I think that that is what may have confused you - its a flash 4 hangover type technique - let me know - Jeff

catbert303
05-19-2003, 10:51 AM
The removeMovieClip code isn't working because the duplicateMovieClip code doesn't work (hence no clip to remove ;))

Are you trying to get it so that each button press on the duplicate button adds a new red dot clip and each press on the delete button deletes one these? If so is it the most recently added clip that gets deleted or the oldest one?

Here's how I would rework the duplicate and remove movieclip code,

First in the main timeline add the frame action,


clipArray = [];


this is an array that will store references to each clip that gets duplicated

next (again on the main timeline) create the red dot clip, give this the instance name w, inside the clip convert the shape to a button and add the actions,


on (press) {
this.startDrag();
}
on (release) {
this.stopDrag();
}


this makes this clip (and more importantly any clip duplicated from it) draggable,

now add the duplicate button,


on (press) {
++depth; // add 1 to a variable depth
_root.w.duplicateMovieClip("w" + depth, depth); // duplicate the clip w, name the new instance based on the depth variable
_root.clipArray.push(_root["w" + depth]); // add this new movie clip into the array, this allows us to keep track ofthe created clips so they can be removed again
_root["w" + depth]._x = somevalue; // position the new clip somehwere
_root["w" + depth]._y = someothervalue;
}



finally the remove button, this will remove the oldest of the clips created, to remove the newest ones you would work from the other end of the array (clipArray[clipArray.length - 1].removeMovieClip() etc)


on (press) {
_root.clipArray[0].removeMovieClip();
_root.clipArray.splice(0, 1);
}

EnSanity
05-20-2003, 01:53 AM
Awesome, great section of code! What i want it to be able to do is let them be able to click on a little red button and then click delete and have that instance be deleted. I also dont know what you meant by The removeMovieClip code isn't working because the duplicateMovieClip code doesn't work (hence no clip to remove )
But let me examine your code and try implementing it! Thanks for your help - Jeff

CyanBlue
05-20-2003, 01:59 AM
Hm... That's what that was??? Great job, catbert303... ;)

EnSanity
05-20-2003, 05:03 AM
n/m got it guys, you guys rock. thanks for helping me! <3 blue/catbert - heres what i did to delete the last clicked one:

on the mc being copied i put

root.varname = this._name;


and on the delete button i tweaked catberts script a little bit



on (press) {
varlength = length(_root.varname);
varnumber = substring(_root.varname, 2, varlength);
varnum = parseInt(varnumber);
trace("varlength = " + varlength + "\n varnumber = " + varnumber + "\n varnum - " + varnum);
_root.clipArray[varnum-1].removeMovieClip();
// _root.clipArray.splice(0, 1);
}




<3 - EnSanity

catbert303
05-20-2003, 08:55 AM
glad its working :D

hanuman0503
12-13-2010, 01:13 AM
This was what I did
var obj: DisplayObject = this.getChildByName("_yourInstanceName");