Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Instance names in action scripting... Help please!!! 1

Status
Not open for further replies.

brutteforcce

Technical User
Jun 23, 2006
105
RO
How can I use a part of an instance name in action script... I have button1, button2, button3 and button4. This are the instance names for some buttons. I want to fin with the action script the number after button and to use it in the name of other instances...
How can I do that?
Please help!!!
Thanks!!!
 
Ok, but I want it to take the number of the button on a event and then to use that number for the instance names of other movie clips...
For example: on release button3 it should make these:

txt_3.gotoAndPlay("txt_init");
disp_3.gotoAndStop("disp_init");

When I release another button it should do the same thing.
Thanks...
 
You'd do something like:

[tt]// AS2 main timeline
button3.onRelease = function():Void {
var n:String = this._name.substring(6);
this._parent["txt_"+n].gotoAndPlay("txt_init");
this._parent["disp_"+n].gotoAndStop("disp_init");
};
//[/tt]

Kenneth Kawamoto
 
K... But I have button1, button2, button3, button4 and button5. If I click one of them it should take the number and use it for the instance names of other movieclips.
Can't I make a function that makes that thing for all of the buttons?
How you told me I should write that action for all of the buttons...
It would be:
button1.onRelease = function():Void {
var n:String = this._name.substring(6);
this._parent["txt_"+n].gotoAndPlay("txt_init");
this._parent["disp_"+n].gotoAndStop("disp_init");
};

button2.onRelease = function():Void {
var n:String = this._name.substring(6);
this._parent["txt_"+n].gotoAndPlay("txt_init");
this._parent["disp_"+n].gotoAndStop("disp_init");
};

button3.onRelease = function():Void {
var n:String = this._name.substring(6);
this._parent["txt_"+n].gotoAndPlay("txt_init");
this._parent["disp_"+n].gotoAndStop("disp_init");
};


 
I guess this is what you wanted to do.
[tt]//
for (i=1; i<5; i++) {
this["buton"+i].onRollOver = function() {
var n = this._name.substring(6);
this._parent["txt_"+n].gotoAndPlay("txt_init");
this._parent["disp_"+n].gotoAndStop("disp_init");
};
this["buton"+i].onRollOut = function() {
var n = this._name.substring(6);
this._parent["txt_"+n].gotoAndPlay("trans_txt");
this._parent["disp_"+n].gotoAndStop("trans_disp");
};
}
//[/tt]

Kenneth Kawamoto
 
Both of them are not finished... I worked, until I realized that the structure isn't good and the actions are to long...
So I modified the structure and I tryed to modify the actions but I don't know why it isn't working... So please help me if you can...
 
So what should I do? Can you fix the problem please if you still have the .fla files and send them on that mail?
If you can't, just tell me please what should I do...
Thanks...
 
When you do "for" loop you have to have "buton1" et al on Stage.

So what should I do, so it works?
Please help me!
 
First make sure you have "buton1" - "buton5" on Stage. Then put this script on the main timeline:

[tt]//
for (i=1; i<5; i++) {
this["buton"+i].onRollOver = function() {
trace(this)
}
}
//[/tt]

What do you get in the Output window when you roll over the buttons?

Kenneth Kawamoto
 
If I put it in the time line of the movie clip where are the buttons I get:
_level0.instance1.deschidere.buton2
_level0.instance1.deschidere.buton1
_level0.instance1.deschidere.buton2
_level0.instance1.deschidere.buton3
_level0.instance1.deschidere.buton4
If I put it in the timeline where the movie clip is I don't get nothing and if I put it in the main timeline I don't get nothing...
In the main timeline I have a movie clip... In that movie clip I have other movie clips with the buttons...
Thanks for helping...
 
I tryed there in the first place I put the code:

for (i=1; i<5; i++) {
this["buton"+i].onRollOver = function() {
var n = this._name.substring(5);
this._parent["txt_"+n].gotoAndPlay("txt_init");
this._parent["disp_"+n].gotoAndStop("disp_init");
};
this["buton"+i].onRollOut = function() {
var n = this._name.substring(5);
this._parent["txt_"+n].gotoAndPlay("trans_txt");
this._parent["disp_"+n].gotoAndStop("trans_disp");
};
}

stop();

You wrote substring(6) but it is five, but it still doesn't work... The movie clips are trying to do something but it isn't correcty... That's why I said I don't know what to do...
 
Sorry!!! Instead of a gotoAndStop it was gotoAndPlay...
Now, the first 2 buttons work correctly but the last 3 don't... What should I do?
Thanks for the support...
 
> You wrote substring(6) but it is five

That's because your button is spelt as "buton", which is perfectly fine.

> but it still doesn't work...

Where are MovieClips "txt_1" and "disp_1" etc? The code assumes they are in the same timeline as your buttons (i.e. in "deschidere").

[tt]//
for (i=1; i<=5; i++) {
this["buton"+i].onRollOver = function() {
var n = this._name.substring(5);
trace(this._parent["txt_"+n]);
}
}
//[/tt]

What do you get with this code?

Kenneth Kawamoto
 
Thank you very very much... It is working... I found all my mistakes... There are 5 buttons so i must be <6... That's why the last button didn't work and I inverted the instance name of the text for button 3 and 4... So evrything is fine now thanks to you...
Thanks a lot again!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top