AS3 Compare two functions

╄→гoц情女王★ 提交于 2019-12-11 06:54:33

问题


I have a static Array and a static method in my class and via this method I push some functions in the array. The method is called on timeline so when I play the frame twice it will push the function twice and so on.

Question : How can I check if the function exists and prevent duplication? ( it is like addeventlistener )


回答1:


short version: don't code on the timeline.

long version: if you're lucky the functions would be the same, so it's possible to compare them using the == operator. but as you're coding on the timeline, there may be several instances of the same function, so this won't work. You could save a static Boolean indicating if you've already added it.

BUT! Start coding in classes, it will save you a lot of trouble in the long run, and give you much better answers here on Stack Overflow -- it's kinda like asking a 5 star chef why your only-add-water-and-shake-togehter-cake didn't turn out very great.




回答2:


You can compare objects using ByteArray's. For your condition I would probably push function calls in an array with function, arguments and the "thisObject". Here's a rough idea. But I would probably create a new class that extends Array for that awaiting action container.

package  {

    import flash.display.MovieClip;
    import flash.utils.ByteArray;
    import flash.events.MouseEvent;

    public class Main extends MovieClip {

        private static var awaiting:Array = [];

        private function pushAction(func:Function,args:Array=null,thisArg:*=null):uint {
            var newObj:Object = {
                func    :func,
                args    :args,
                thisArg :thisArg
            }
            for (var i:int = 0; i < Main.awaiting.length; i++) {
                if (duplicates(Main.awaiting[i],newObj)) return i;
            }
            return Main.awaiting.push(newObj);
        }

        private function nextAction(extraArgs:Array=null):* {
            if (Main.awaiting.length == 0) return null;
            var o:Object = Main.awaiting.shift();
            var args:Array = o.args;
            if (extraArgs) args = args == null ? extraArgs : args.concat(extraArgs);
            var thisArg:* = o.thisArg;
            var func:Function = o.func;
            return func.apply(thisArg,args);
        }

        private static function duplicates(item1:Object,item2:Object):Boolean {
            var bArr1:ByteArray = new ByteArray();
            var bArr2:ByteArray = new ByteArray();
            bArr1.writeObject(item1);
            bArr2.writeObject(item2);
            bArr1.position = 0;
            bArr2.position = 0;
            var str1:String = bArr1.readUTFBytes(bArr1.length);
            var str2:String = bArr2.readUTFBytes(bArr2.length);
            return str1 == str2;
        }

        public function Main() {
            pushAction(function(){trace('the first item')});
            pushAction(traceMe,['the second item']);
            pushAction(traceMe,['the third item']);
            pushAction(sayHi,null,Main);
            pushAction(function(){trace('the first item')});
            pushAction(traceMe,['the second item']);
            pushAction(traceMe,['the third item']);
            pushAction(sayHi,null,Main);
            stage.addEventListener(MouseEvent.CLICK,clicked);
        }

        private function clicked(e:MouseEvent):void {
            this.nextAction();
        }

        public function traceMe(str:String):void {
            trace(str);
        }

        private static function sayHi():void {
            trace('hi');
        }

    }

}



回答3:


Short version:

var i = arr.indexOf(myFunc);
if(i != -1) arr.push(myFunc);

Long version:

I am not an expert on the timeline. I do know that there is only one instance of a static function in pure AS3 and I imagine it is the same thing when you use the timeline.

So the quickest thing you can do is just use

arrayName.indexOf(funcName);

which will return -1 if the function does not exist in the array and it's correct index otherwise.

If this does not work, let us know, I'm very curious.




回答4:


Function is a dynamic type, but the compiler usually doesn't allow creating random properties on functions, however, if you "trick" the type checking, you can have a property on a function that you could later use to identify it - this way it won't be necessary for the functions to be the exact same function.

Note, that this is a disastrous thing to do :) But just as a trivia knowledge this may help later.

function foo():void { /* some code */ }
function bar():void { /* some code */ }
foo["baz"] = bar["baz"] = 42;

if (foo["baz"] && foo["baz"] == bar["baz"]) trace("equal");
else trace("distinct");


来源:https://stackoverflow.com/questions/12543910/as3-compare-two-functions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!