我有一个方法,它将事件监听器注册到我拥有的数组中的每个MovieClip。该方法位于类中,侦听器函数也是如此。问题是,我收到的错误是“访问未定义属性handleNavToggle”,即使已声明。
有什么想法吗?
public class Navigation {
var navItems:Array;
public function Navigation(navitems:Array) { ... }
private function registerNavigationEvents():void {
for each(var item:MovieClip in this.navItems) {
item.addEventListener(MouseEvent.MOUSE_OVER, this.handleNavToggle);
item.addEventListener(MouseEvent.MOUSE_OUT, this.handleNavToggle);
}
}
public function handleNavToggle(e:MouseEvent):void {
var mc:MovieClip = e.currentTarget as MovieClip;
mc.gotoAndStop((mc.currentFrame == 1) ? 2 : 1);
}
...
}
答案 0 :(得分:0)
摆脱这个。在函数名称之前,只需传递函数名称,否则它认为它正在寻找属性,由于某种原因我觉得这样[" handleNavToggle"]也可能有用,但为什么会复杂化
答案 1 :(得分:0)
除非必要,否则不要使用this
,这实际上仅限于以下情况:
something.property = this;
method(this);
public function method(property:int):void
{
this.property = property;
}
也就是说,尝试修改您的代码以排除this
。
public class Navigation
{
private var _navItems:Array = [];
public function Navigation(navItems:Array)
{
_navItems = navItems.slice();
_registerNavigationEvents();
}
private function _registerNavigationEvents():void
{
for each(var i:MovieClip in _navItems)
{
i.addEventListener(MouseEvent.MOUSE_OVER, _handleNavToggle);
i.addEventListener(MouseEvent.MOUSE_OUT, _handleNavToggle);
}
}
private function _handleNavToggle(e:MouseEvent):void
{
var mc:MovieClip = MovieClip(e.target);
mc.gotoAndStop((mc.currentFrame == 1) ? 2 : 1);
}
}