[FMX]Please some explanation on substr

For quite some time I use the following code to control, behaviors of some buttons and other content:

for (var i in this) {
if (this*._name.substr(0, 4) == "btn_") {
setKnop(this*._name);
}
}
 
function setKnop(clip) {
var clip = eval(clip);
clip.onRollOver = function() {
new Color(clip).setRGB(0x000000);
}; 

Etc, etc.

I used this code because somebody adviced me to do so. So in this case btn_ is always followed by some other name. Now I bought the book Foundation actionscript from FriendsOfEd and they advice to use btn as a suffix “_btn” as well as Colin Moock did in his book Actionscript the definite guide.

Is there somebody who can explain me a little bit more about the use of “_name, substr” etc

What i would like to find out is how the this code:

_name.substr (0, 4) 

would look like if I start working with btn as suffix, as the adviced in the mentioned books?

Thanks in advance :wink:

when you trace mc._name, you’ll get the instance name back, in this case is “mc”…

then, it checks if the name begins with "btn" (substr(0,4)), if so, its a button (if you named al your buttons like “btn_”+name)

then it will apply setKnop on every button that was returned…
(do i make any sence here?)

from the reference:

MovieClip._name

Availability

Flash Player 4.

Usage

myMovieClip_name

Description

Property; returns the instance name of the movie clip specified by MovieClip.

String.substr

Availability

Flash Player 5.

Usage

myString.substr(start, [length])

Parameters

start An integer that indicates the position of the first character in myString to be used to create the substring. If start is a negative number, the starting position is determined from the end of the string, where the -1 is the last character.

length The number of characters in the substring being created. If length is not specified, the substring includes all of the characters from the start to the end of the string.

Description

Method; returns the characters in a string from the index specified in the start parameter through the number of characters specified in the length parameter. The substr method does not change the string specified by myString, it returns a new string.

Yes that made very much sence :slight_smile: Thank you. :slight_smile: But now the question about when I use btn as suffix “_btn” I tried it the way Colin Moock is describing it in the book (negative starting index) so I tried it like this:

for (var i in this){
if (_name.substr(-4) == "_btn"){
setButton(this*._name);
}
}

But that wasn’t working. What am I doing wrong here?

[edit] I found my mistake already. Thanks again for your explanation :wink:

substr needs a start index and a length

here 2 examples on how to use it…

myString = “Hello”;
myString.substr(0,2);
trace(myString);
// returns “He”;

when reversing, use the length of the string

myString = “Hello”;
strLength = myString.length;
myString = myString.substr(strLength-2,2);
trace(myString);
// returns “lo”;