View Full Version : indexOf() syntax - help please !!!

02-06-2003, 09:18 AM
I have a bunch of mc's, instance name is 1st, 2nd 3rd and so on.
I want to display the names in a textBox ( called label) as 1, 2, 3, and so on.

So my code is/should be

label = _name.indexOf( what should I put in here to remove the last 2 letters )

if I put label = _name.indexOf("st");
this works well with 1st, but (obviously) not with 2nd

I have done search here and looked at the AS dic, but cannot find what I am looking for

Any help, as ever, greatly appreciated



02-06-2003, 10:16 AM
isn't it possible to give your mc's all names like



it'll save you a lot of crap adressing them...


02-06-2003, 10:40 AM
Hi linckx,
Nah, - the way it works is that all my mc's are buttons, inside each one is a label, when a user clicks a button I want another textBox populated like this:

this.label= _name
onRelease = function(){
myOtherTextBox = "_name";

Now, my mc's are called ( at the moment) mc.1st, mc.2nd mc.3rd and so on, this populates myOtherTextBox exactly how I want it but not the mc's.
What I want is for the label ( in the mc ) to show "1";
and myOtherTextBox to show 1st - this means either taking off or adding the string "st" or "nd" or "th" or "rd"

I have an idea it means writing an if/else argument or perhaps a while loop.

Thanks for the input - any other ideas very welcome



02-06-2003, 10:44 AM
well, I'm not the scripting hero

--fiddles with his fluffy pink rabbit slippers while whispering to himself 'you know you will be in 5 years, giving himself a little comfort--

But I wish you good luck with it :)
I'll keep an eye on the other replies, since it interests me how this is gonna be solved :)


02-06-2003, 11:26 AM
Howdy, vosgien... Long time no see... :)

I am not sure if I am getting it righ... Correct me if I am wrong...

You have series of movieclips which have instance name of 'mc.1st, mc.2nd mc.3rd, ...', right???

If that is true, you are in a big trouble... (I am not the script hero you are looking for either... :)) All those instance names contain '.' which is used for the dot syntax, so there is a chance when Flash will burst out and say no way sooner or later...
So, I think it will be a good idea to name them to be something like this... 'mc_1st, mc_2nd mc_3rd, ...'

Having said that, what is it you want to get from those instance names??? Is it 'mc_1' from 'mc_1st' and 'mc_2' from 'mc_2nd'??? Or, Is it '1' from 'mc_1st' and '2' from 'mc_2nd'??? At any rate, you can use substr() function to do the job...

theLabel = _name.substr(0, 4); ---> mc_1
theLabel = _name.substr(3, 1); ---> 1

Know what I mean??? I hope this is what you are looking for... and let me know if this is what you are looking for... :)

(I am not sure if 'label' is a keyword or not, but I know that I use 'label' for the combobox component, so I just changed it to 'theLabel' so that it won't confuse Flash...)

02-06-2003, 11:50 AM
Hi Jason
Yeah, I've still been around, just don't post as much as I need to - whiich could mean I am getting better at this coding thing or I have learnt how to use the searh facility better (lol!!!)
Well, you are right, however in an effort to explain myself clearly I have in fact confused the issue.
Forst of my mc's are called ( in the instance box)
1st 2nd 3rd 4th and so on ( it is for a Flash diary)
the code on the mc is
this.label = _name

therefore the textBox in my mc shows exactly that, but I want it to show 1 2 3 4 and so on
However in myOtherText box, when the button (mmc) is clicked I want it to show 1st 2nd 3rd 4th and so on.
So, I can leave my mc instance names the same and find away of removing st nd rd th etc, OR I can renames my mc's 1 2 3 4 and find away of adding st nd rd th etc etc

Your code does work, but I cannot find the right number (to go in the brackets) to remove or add the offending text.
The whole idea of doing it this way was that I could write on line of code as above - somethings are never as easy as they look !!!



p.s. when I say I do not post as often - I do try and answer Q's as often as pos - like many people on this forum, I would never have got ads far as I have without the help of others

02-06-2003, 12:26 PM
Um... Okay... :)

If your MC's intance names are '1st 2nd 3rd 4th', you can say
this.label = _name.substr(0, 1);
to show '1, 2, 3, 4' to the text field...

Yes... You can go backward, but if you go backward, you will be needing lots of if statements to add different postfixes, so I'll stick with what you have if I were you...

Let me know if it works for you...

Glad that you are trying to help other people... Yes... We gotta do what we gotta do to help othere... No doubt about it... :)

02-06-2003, 01:12 PM
Yeah, I tried that, and played around with different numbers, but it throws up its own problem in that there 31 mc's( for the month)
called 1st to 31st - so, obviously once we get over 9th it doesn't work.
I think I will have to write an if/else argument something like

if (label==3 characters){
} else {
label = _name.substr(0,2);

Not sure of syntax here, but, believe it or not, the point of the exercise is to minimise my code as much as possible, so if I have to write just the one argument, that'll be OK - won't it ? (lol!)


02-06-2003, 01:23 PM
AS a rule of thumb, object names should never start with a number, Flash is probably one of the only ones that will let you get away with it. Can't you set a variable in each clip for the labels you need. I mean are you dynamically creating these or are they already created and on the stage? Name them something different like mc1, mc2, mc3

In the mc

this.label1 = "1st";
this.label2 = 1;

Now you can access those variables onRelease, unless I am missing something it should be more efficient this way.

02-06-2003, 01:25 PM
OK, I got it, this little if/else jobbie does eactly what I want - hope your are still folloing this thread linckx........

Thanks for all your help Jason, much appreciated.

this.label = _name.substr(0, 1);
} else {
this.label = _name.substr(0, 2);



02-06-2003, 10:29 PM
No problemo... I'm glad you got it working... :)