What am I doing wrong?
Code:
sample_txt.text = "sat";
var myArray:Array = new Array ("the", "cat", "sat", "on", "the", "mat");
arrsLength = Number(myArray.length);
for (var i = 0; i<arrsLength; i++){
...some stuff...
btn.onPress=function(){
if(myArray[i]==sample_txt.text){
trace (text found!);
}else{
trace (text not found!);
}
}
}
Output:
text not found!
What I need to do is check if the text in input textfield is equal to one of the array values and run specific function depends on result.
Regards,
Artur.
The problem is in visibility and in ‘onPress’ handler declaration. You should declare ‘onPress’ handler outside of ‘for’ loop, because each loop iteration you doing redeclaration of your handler. And event if you will fix visibility issue your ‘onPress’ handler always will have ‘myArray[i]’ equals to last array element value.
So you should do like this:
But make sure that ‘sample_txt’ is visible inside ‘onPress’ handler.