Actionscript AS2:从onRollOver访问类函数

Actionscript AS2:从onRollOver访问类函数,actionscript,actionscript-2,Actionscript,Actionscript 2,我正在开发一个动态构建下拉按钮的类。下面是我的代码摘录(位于类构造函数中): 我试图调用函数drawOption(),该函数位于同一个类中,如下所示: private function drawOption(option:MovieClip, state:String) { trace("yo"); switch (state) { case "hover" : var backgro

我正在开发一个动态构建下拉按钮的类。下面是我的代码摘录(位于类构造函数中):

我试图调用函数
drawOption()
,该函数位于同一个类中,如下所示:

private function drawOption(option:MovieClip, state:String)
    {
        trace("yo");
        switch (state)
        {
            case "hover" :
                var backgroundColour:Number = _shadow;
                var textColour:Number = 0xffffff;
                break;
            default :
                var backgroundColour:Number = _background;
                var textColour:Number = _shadow;
                break;
        }
        option._x = edgePadding;
        option._y = 1 + edgePadding + (optionPadding * (option.index)) + (optionHeight * option.index);
        option.beginFill(backgroundColour,100);
        option.lineStyle(1,_border,100,true);
        option.moveTo(0,0);
        option.lineTo(_optionWidth,0);
        option.lineTo(_optionWidth,optionHeight);
        option.lineTo(0,optionHeight);
        option.endFill();
        var textfield:TextField = option.createTextField("string", option.getNextHighestDepth(), 20, 2, _optionWidth, optionHeight);
        var format:TextFormat = new TextFormat();
        format.bold = true;
        format.size = fontSize;
        format.font = "Arial";
        format.color = textColour;
        textfield.text = option.string;
        textfield.setTextFormat(format);
    }

但由于我试图从
onRollOver
内部调用,因此它似乎无法识别类方法。如何在不复制功能的情况下访问该功能(非常混乱,我不想要!)。

onrollover中的所有内容都与滚动的按钮有关,以访问外部功能,在调用函数之前,您必须以与访问外部变量完全相同的方式导航到外部类,例如:

如果按钮的父级包含该功能:

此._parent.drawOption(..)

ContainerMC类:

class ContainerMC extends MovieClip{

    function ContainerMC() {
        // constructor code
        trace("Container => Constructor Called");
    }

    function Init(){
        trace("Container => Init Called");
        this["button_mc"].onRollOver = function(){
            trace(this._parent.SayHello());
        }
    }

    function SayHello():String{
        trace("Container => SayHello Called");
        return "Hellooooo World";
    }
}
然后,我在库中有一个movieclip,它带有ContainerMC类和identifier Container_mc,通过主时间线中的这行添加到stage中:

var Container = attachMovie("Container_mc","Container_mc",_root.getNextHighestDepth());
Container.Init();

编辑:在AS2中添加了工作示例

,我更喜欢使用
委托
类向事件处理程序添加函数,同时保持对范围的控制

您可以这样实现它:

import mx.utils.Delegate;

//create method allows you to set the active scope, and a handler function
_button.onRollOver = Delegate.create(this,rollOverHandler);

function rollOverHander() {
    // since the scope has shifted you need to use 
    // the instance name of the button
    _button.gotoAndStop("over");
    TweenLite.to(_button.options,0.2 * optionCount,{_y:mask._y, ease:Strong.easeOut, onComplete:detectMouse, onCompleteParams:[button]});
}

this.\u parent
查找
displayObject
,该
this
不是它所引用的类的子对象。类不是舞台上的对象,无法通过这种方式访问。在这种情况下,我通常会让我的类扩展一个movieclip作为按钮+其他元素的容器,以便我可以从Events访问类方法我的类正在扩展
movieclip
,我不确定您所说的扩展只是从另一个类继承的一种形式,而不是定义您的类的范围。你能写一个例子来说明你的意思吗?为Quickly创建一个示例我看到你正在将一个movieclip导出到actionscript并在此基础上进行扩展。现在有道理了。这会起作用,但我更喜欢委派选项+1感谢您的帮助:)太棒了,这正是我最初想要的语法,在
onRollOver
和局部变量中包含函数太可怕了。谢谢:)
import mx.utils.Delegate;

//create method allows you to set the active scope, and a handler function
_button.onRollOver = Delegate.create(this,rollOverHandler);

function rollOverHander() {
    // since the scope has shifted you need to use 
    // the instance name of the button
    _button.gotoAndStop("over");
    TweenLite.to(_button.options,0.2 * optionCount,{_y:mask._y, ease:Strong.easeOut, onComplete:detectMouse, onCompleteParams:[button]});
}