im having a problem where whatever text or even blank space i put i still go in frame 170, as you see i put 171 frame there, and if i type "therefore" it go in 171 , seems like its working fine its just even i put wrong text it go to frame 170 , i cant find the problem tho,also i dont know if i should make an else statement so if the word is not in the list it will go to other frame, thanks mate
var i:int = 0;
var names:Array = new Array("therefore","disciples","nations","baptizing","father","son","holy spirit");
var frames:Array = new Array("171","170","170","170","170","170","170","170");
button_140.addEventListener(MouseEvent.CLICK, fl_MouseClickHandler_4);
function fl_MouseClickHandler_4(event:MouseEvent):void
{
var searchtext:String = searchtext.text.toLowerCase();
findInArray(searchtext);
gotoAndStop(frames[i]);
}
function findInArray(str:String):int
{
for(i=0; i < names.length; i++)
{
if(names[i] == str)
{
return i;
}
}
return 0;
}
why you always go to frame 170:
let's see your function fl_MouseClickHandler_4
:
findInArray(searchtext);//string won't be found so "i" would be 7 (the last index in array)
gotoAndStop(frames[i]);//so it goes to frame 170
a fix for your code:
the function fl_MouseClickHandler_4
:
function fl_MouseClickHandler_4(event:MouseEvent):void
{
var searchtext:String = searchtext.text.toLowerCase();
var index:int=findInArray(searchtext);
if(index==-1){
//do something when frame not found
}
else{
gotoAndStop(frames[index]);
}
the function findInArray
:
function findInArray(str:String):int
{
for(i=0; i < names.length; i++)
{
if(names[i] == str)
{
return i;//return the found index
}
}
return -1;//return -1 if nothing found
}
I Hope this helps...
Edit:
You don't need to make a function to find a value in your array. You can use Array
class built-in method indexOf()
to find the index of an item in an array: see the AS3 manual for more info.
theArray.indexOf(theValue);
returns the index of theValue. if theValue is not in theArray, returns -1.
Test this example below :
//# declare variables outside of function (make once & re-use)
var searchtext:String = "";
var index:int = 0;
//# after updating searchtext string with user-text then run function below
function fl_MouseClickHandler_4(event:MouseEvent):void
{
searchtext = searchtext.text.toLowerCase();
index = names.indexOf(searchtext); //test with "holy spirit"
if(index == -1)
{
trace("index is : -1 : No match found");
//do something when frame not found
}
else
{
trace("index is : " + index + " ::: result is : " + (frames[index]) );
gotoAndStop( frames[index] );
}
}