1

我是 Action-script OOP 的新手,我需要知道如何像这个例子一样链接方法

I.$(button).bind('click',clickButton).bind('rollover',overButton).bind('rollout',outButton)

首先,我需要删除I.仅像 jQuery 一样使用美元符号 :) 来选择 MovieClip 并对其应用任何操作,因为这样我使用静态方法操作脚本限制我只使用静态属性保存最后一个在这里调用动作的是类代码,以了解我的意思:

package com.MAIN
{
    import flash.display.Sprite;
    import flash.events.MouseEvent;

    public class I extends Sprite
    {
        private static var cSelector:Sprite;

        public static function $(selector:Sprite)
        {
            cSelector = selector
            return I;
        }

        public static function alpha(val:Number)
        {
            cSelector.alpha = val;
            return I;
        }

        // bind mouse event to the element
        public static function bind(EventStr,func:Function)
        {
            var func1:Function = function(e:MouseEvent){
                func(cSelector);
            }
            // select the event from the list
            if(typeof(EventStr) == 'string'){
                // map the events in lowercase
                var events:Object = {click:'CLICK',rollover:'ROLL_OVER',rollout:'ROLL_OUT',dblclick:'DOUBLE_CLICK',mousedown:'MOUSE_DOWN',mousemove:'MOUSE_MOVE',mouseout:'MOUSE_OUT',mouseover:'MOUSE_OVER',mouseup:'MOUSE_UP',mousewheel:'MOUSE_WHEEL'};
                // check if the event exists in the list
                if(events[EventStr] && MouseEvent[events[EventStr]]){
                    cSelector.addEventListener(MouseEvent[events[EventStr]],func1);
                }
            }else if(typeof(EventStr) == 'object'){
                // add the event
                cSelector.addEventListener(EventStr,func1);
            }
            return I;
        }

        public static function remove()
        {
            cSelector.parent.removeChild(cSelector);
            return I;
        }

    }
}
4

2 回答 2

2

给你,朝着正确的方向迈出几步。然而,这是一个非常非常非常糟糕的想法。

//$.as
package
{
    import flash.display.DisplayObject;

    //NOTE: there's NO class definition

    public function $( selector : DisplayObject ) : IDisplayObject
    {
        //traverse displaylist to find <code>selector</code>
        //and return an instance of IDisplayObject that holds the reference        
    }
}

//IDisplayObject.as
package
{
    public interface IDisplayObject{
        function alpha( value : Number ) : IBinding;
    }
}

//IBinding.as
package jquery
{
    public interface IBinding{
        function bind( eventName : String, callback : Function, ...parameters ):void;
    }
}

一旦您创建了这些的具体实现,您可以执行以下操作:

$( someMC ).alpha( .5 ).bind( 'click', function(){ trace( 'what a miraculously crappy idea !!!!' ) } );
于 2013-03-06T15:18:01.450 回答
1

你可以这样尝试:

interface Test {
  function doBla(): Test
  function moreBla(): Test
}

public class StaticTest {
  private static const instance: Test = new InternalTest()

  public static doBla() : Test {
    return instance.doBla();
  }

  public static moreBla() : Test {
    return instance.moreBla();
  }
}

internal class InternalTest implements Test {
  function doBla(): Test {
    trace("bla");
    return this;
  }

  function moreBla(): Test {
    trace("more bla");
    return this;
  }
}
于 2013-03-05T17:15:30.217 回答