【问题标题】:call function from another function actionscript从另一个函数动作脚本调用函数
【发布时间】:2015-02-21 14:05:50
【问题描述】:

我生成文本字段数组和随机整数数组。每个文本字段都包含数组中的一个随机数。接下来我有按钮来触发函数来搜索数组中的用户定义的数字。此函数调用 Timer 实例来减慢搜索过程。当我单击按钮功能时 是搜索和停止搜索过程只有一次。当我再次点击搜索按钮时,闪现给我这个错误:

TypeError: Error #1010: A term is undefined and has no properties.
at bakalarka_fla::MainTimeline/timerListener()
at flash.utils::Timer/_timerDispatch()
at flash.utils::Timer/tick()

这是代码

input_hladaj.restrict ="0-9"
btn_hladaj.addEventListener(MouseEvent.CLICK, hladaj_fn);

var cislohladaj:int = new int;
var myTimer:Timer = new Timer(1000,10);
myTimer.addEventListener(TimerEvent.TIMER, timerListener);
var t:int= 0;

function timerListener (e:TimerEvent):void
{
	trace("Times Fired: " + e.currentTarget.currentCount);
		if(numberArray[t]==cislohladaj )
		{
			trace(t);
			TextFields[t].backgroundColor = 0x3AEE20;
			txt_vysledok.text = "Najdene na pozícii číslo " +t.toString();
			t=sizeOfArray;
			myTimer.stop();
			myTimer.reset();
			
		}
		else
		{
			txt_vysledok.text = "Nenajdene"
			TextFields[t].backgroundColor = 0xFF0000; //nenajde 
		}
	t++;
}



function hladaj_fn(ev:Event):void
{
		for (var g:int =0; g<sizeOfArray; g++)   
		{
			TextFields[g].backgroundColor = 0xFFFFFF;
		}
		txt_vysledok.text = "";
		cislohladaj = parseInt(input_hladaj.text);
		myTimer.reset();
		myTimer.start();
		
}

【问题讨论】:

  • 尝试验证您的numberArray[t] 不为空,因为您忘记验证if(t &lt; numberArray.length){} 并在每次搜索时将t 重置为0。

标签: actionscript-3


【解决方案1】:

计时器计时 10 次。数组的长度可能小于 10。当t 大于数组长度时,numberArray[t] 返回 null 并出现错误。你应该在t++;之后添加条件:

if (t >= sizeOfArray)
{
    myTimer.stop();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-13
    • 2017-11-09
    • 2015-02-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多