【问题标题】:searchbox as3 not working properly搜索框 as3 无法正常工作
【发布时间】:2017-01-28 12:01:40
【问题描述】:

我有一个问题,无论我放什么文本甚至空白,我仍然在第 170 帧中,如您所见,我在那里放了 171 帧,如果我输入“因此”它会在 171 中,看起来它工作正常只是即使我输入了错误的文本,它也会转到第 170 帧,但我找不到问题,我也不知道我是否应该做一个 else 语句,所以如果这个词不在列表中,它将转到其他帧,谢谢队友

 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;
}

【问题讨论】:

  • for 循环增加“i”的值,不管它是否能找到值,并且你在这一行使用 i 在 gotoAndStop(frames[i]);,你应该像这样使用它 gotoAndStop (frames[findInArray(searchtext)]);
  • 嘿,非常感谢,它工作得很好,但是现在如果在数组列表中没有找到单词,它就会出错,有没有办法让 else 语句来确保他输入错误的词我会去不同的框架,告诉词没有找到?谢谢你的朋友
  • 如果找不到,只需在 findInArray 函数中返回 -1 并检查结果,如果函数返回 -1 警报给用户作为未找到单词
  • if(findInArray(searchtext) == -1) { /* alert code */ } else{ gotoandstop } ,这里的语法你也可以google一下。
  • @VC.One 答案太基础了,我只是想帮助 jarvis,我确信社区不会从中受益。所以不需要信用感谢您的关注

标签: arrays actionscript-3 flash searchbar search-box


【解决方案1】:

为什么你总是转到第 170 帧:

让我们看看你的函数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

修复您的代码:
函数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]);
}

函数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
}

我希望这会有所帮助...

编辑:

您无需创建函数即可在数组中查找值。您可以使用Array 类内置方法indexOf() 来查找数组中某个项目的索引:有关详细信息,请参阅AS3 manual

theArray.indexOf(theValue);

返回 theValue 的索引。如果 theValue 不在 theArray 中,则返回 -1。

在下面测试这个例子:

//# 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] );
    }
}

【讨论】:

  • 先生,你是个英雄,好的,我回家后试试这个。非常感谢伙计!干杯!
  • 嘿先生,我应该替换这个 findInArray(searchtext);到这个 theArray.indexOf(theValue); ?
  • @JarvisLorenzD.PAlad 看看用代码编辑是否对你有帮助。
  • 好的先生,我回家后也试试看,非常感谢兄弟!
  • @JarvisLorenzD.PAlad 不客气。很高兴能再次为您提供帮助。
猜你喜欢
  • 2019-08-04
  • 1970-01-01
  • 1970-01-01
  • 2022-01-08
  • 2014-03-31
  • 2021-11-10
  • 2013-12-23
  • 2015-07-27
  • 2014-07-17
相关资源
最近更新 更多