AS2:来自onRollOver的访问类功能

时间:2012-01-31 10:12:33

标签: actionscript actionscript-2

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

_button.onRollOver = function()
            {
                this.gotoAndStop("over");
                TweenLite.to(this.options,0.2 * optionCount,{_y:mask._y, ease:Strong.easeOut, onComplete:detectMouse, onCompleteParams:[button]});
                function detectMouse(button:MovieClip)
                {
                    button.options.onMouseMove = function()
                    {
                        for (var option:String in this._parent.children)
                        {
                            if (this._parent.children[option].hitTest(_root._xmouse, _root._ymouse, true))
                            {
                                if (!this._parent.children[option].active) {
                                    this._parent.children[option].clear();
                                    drawOption(this._parent.children[option], "hover");
                                    this._parent.children[option].active = true;
                                }                               
                            }
                        }
                    };
                }
            };

我试图调用同一类中的函数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内部调用它似乎无法识别Class方法。如何在不复制它的情况下访问该函数(非常混乱,不要!)。

2 个答案:

答案 0 :(得分:1)

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

如果按钮的父级包含函数:

this._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";
    }
}

然后我在库中有一个带有Class ContainerMC和identitfier Container_mc的movieclip,它在主时间轴中被这一行添加到舞台上:

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

编辑:添加工作样本

答案 1 :(得分:1)

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

你这样实现:

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