AS3 错误 1119:通过静态类型 flash.events:MouseEvent 的引用访问未定义的 属性 CHANGE
AS3 error 1119: Access of undefined property CHANGE through a reference with static type flash.events:MouseEvent
这可能是 AS3 中的一个错误,因为它在错误中列出的事件与 mouseEvent 无关,但我真的很想弄清楚这个问题。我一直致力于这个项目,并且有一个用于搜索功能的影片剪辑,其中包含 3 个组合和 2 个输入文本字段。当用户在文本字段中输入文本时,我想禁用组合,将所选索引设置为 -1。如果用户清除文本字段,我希望启用组合。 CS4 抛出上述错误。太奇怪了还是我?
我的代码:
fltr.btn.addEventListener(MouseEvent.CLICK, shwSrch);
function shwSrch(Event:MouseEvent):void{
popcmb1(); //function to populate combo 1
srch.canNow.button.addEventListener(MouseEvent.CLICK, cans);
srch.srchNow.button.addEventListener(MouseEvent.CLICK, gos);
srch.npt1.it.addEventListener(Event.CHANGE, txtchng);
srch.npt1.it.dispatchEvent(new Event(Event.CHANGE));
}
function txtchng(event:Event):void{
if (srch.npt1.it.length >0){
//DISABLE COMBO AND NPT2
srch.cmb1.cmb.enabled = false;
srch.cmb1.cmb.selectedIndex = -1;
srch.cmb1.cmb.prompt = "All";
cmb1si = gSrch.cmb1.cmb.selectedIndex;
}
else{
srch.cmb1.cmb.enabled = true;
srch.npt1.it.selectable = true;
srch.npt1.it.type = TextFieldType.INPUT;
srch.npt1.it.borderColor = 0x000000;
}
npt1 = srch.npt1.text;
}
所以,当我 运行 这个新添加的代码时,它抛出错误 1046:未找到类型或不是编译时常量:事件。
我已经有行 'import flash.events.Event;'
我已经做了很多实验,并注意到如果我为侦听器创建一个单独的函数,然后使用 shwSrch 函数调用它:
function lstnrs():void{
srch.npt1.it.addEventListener(Event.CHANGE, txtchng);
srch.npt1.it.dispatchEvent(new Event(Event.CHANGE));
}
有效...有没有人可以对此有所启发?
问题在于您如何命名变量。
function shwSrch(Event:MouseEvent):void
此处使用 Event
作为参数名称,这是一个错误的选择,因为它与 Event
class 具有相同的名称。稍后,您添加一个侦听器。
srch.npt1.it.addEventListener(Event.CHANGE, txtchng);
现在不清楚您指的是 class 还是参数变量名。对于后者,您会收到错误消息。
要解决此问题,请使用约定以小写字母开头变量名称,class 名称以大写字母开头。
function shwSrch(mouseEvent:MouseEvent):void
旁注:例如,您真的需要在添加监听器后显示事件吗?
srch.npt1.it.addEventListener(Event.CHANGE, txtchng);
srch.npt1.it.dispatchEvent(new Event(Event.CHANGE));
如果你不在处理函数中使用参数,你可以简单地做
srch.npt1.it.addEventListener(Event.CHANGE, txtchng);
txtchng(null);
这可能是 AS3 中的一个错误,因为它在错误中列出的事件与 mouseEvent 无关,但我真的很想弄清楚这个问题。我一直致力于这个项目,并且有一个用于搜索功能的影片剪辑,其中包含 3 个组合和 2 个输入文本字段。当用户在文本字段中输入文本时,我想禁用组合,将所选索引设置为 -1。如果用户清除文本字段,我希望启用组合。 CS4 抛出上述错误。太奇怪了还是我?
我的代码:
fltr.btn.addEventListener(MouseEvent.CLICK, shwSrch);
function shwSrch(Event:MouseEvent):void{
popcmb1(); //function to populate combo 1
srch.canNow.button.addEventListener(MouseEvent.CLICK, cans);
srch.srchNow.button.addEventListener(MouseEvent.CLICK, gos);
srch.npt1.it.addEventListener(Event.CHANGE, txtchng);
srch.npt1.it.dispatchEvent(new Event(Event.CHANGE));
}
function txtchng(event:Event):void{
if (srch.npt1.it.length >0){
//DISABLE COMBO AND NPT2
srch.cmb1.cmb.enabled = false;
srch.cmb1.cmb.selectedIndex = -1;
srch.cmb1.cmb.prompt = "All";
cmb1si = gSrch.cmb1.cmb.selectedIndex;
}
else{
srch.cmb1.cmb.enabled = true;
srch.npt1.it.selectable = true;
srch.npt1.it.type = TextFieldType.INPUT;
srch.npt1.it.borderColor = 0x000000;
}
npt1 = srch.npt1.text;
}
所以,当我 运行 这个新添加的代码时,它抛出错误 1046:未找到类型或不是编译时常量:事件。 我已经有行 'import flash.events.Event;' 我已经做了很多实验,并注意到如果我为侦听器创建一个单独的函数,然后使用 shwSrch 函数调用它:
function lstnrs():void{
srch.npt1.it.addEventListener(Event.CHANGE, txtchng);
srch.npt1.it.dispatchEvent(new Event(Event.CHANGE));
}
有效...有没有人可以对此有所启发?
问题在于您如何命名变量。
function shwSrch(Event:MouseEvent):void
此处使用 Event
作为参数名称,这是一个错误的选择,因为它与 Event
class 具有相同的名称。稍后,您添加一个侦听器。
srch.npt1.it.addEventListener(Event.CHANGE, txtchng);
现在不清楚您指的是 class 还是参数变量名。对于后者,您会收到错误消息。
要解决此问题,请使用约定以小写字母开头变量名称,class 名称以大写字母开头。
function shwSrch(mouseEvent:MouseEvent):void
旁注:例如,您真的需要在添加监听器后显示事件吗?
srch.npt1.it.addEventListener(Event.CHANGE, txtchng);
srch.npt1.it.dispatchEvent(new Event(Event.CHANGE));
如果你不在处理函数中使用参数,你可以简单地做
srch.npt1.it.addEventListener(Event.CHANGE, txtchng);
txtchng(null);