mardi 30 juin 2015

Is it possible compare DisplayObjects by content if flash?

We trace MovieClip, frame by frame, and want to find unique objects that preset on it(this is a rasterizer flash animations for opengl). We do this work, with code below:

    public function process():void
    {
        movieClip.addEventListener(Event.FRAME_CONSTRUCTED, onEnterFrame);
        onDone = false;

        currentFrame = 0;
        movieClip.gotoAndStop(1);

        while (!onDone)
        {
            movieClip.nextFrame();
        };
    }

    private function onEnterFrame(e:Event):void
    {
        if ( currentFrame >= movieClip.totalFrames )
        {
            convert(movieClip);
            movieClip.removeEventListener(Event.FRAME_CONSTRUCTED, onEnterFrame);

            onDone = true;
            onDoneCallback(movieClipItem);
        }
        else
        {
            frames[currentFrame] = new Vector.<Object>();
            labels.push(movieClip.currentFrameLabel);

            for (var j:int = 0; j < movieClip.numChildren; ++j)
            {
                var child:DisplayObject = movieClip.getChildAt(j);

                if ( child.visible )
                {
                    frames[currentFrame].push(getSnapshot(child));

                    if ( !(child in objects) )
                    {
                        objects[child] = [];
                    }

                    objects[child].push(currentFrame);
                }
            }

            currentFrame++;
        };
    }

As a result we have "frames" collections where on each frame we have objects that exists on that frame. Then we try to find same objects in frame collection, and use follow function for compare objects:

private function objectEqual(left:DisplayObject, right:DisplayObject):Boolean {
    return left === right && left.name == right.name;
}

but this compare give not good results. For example when the same movieclip added at frame 2 and at frame 10, objectEqual give us that this movie clip is different(this is different objects instance, but with the same content, so in our case thay must by equal). Also some times Shape objects may change they content, but instance and instance name will remain the same, so "objectEqual" also make mistake, and give us that "left" and "right" are same, but they different.

Aucun commentaire:

Enregistrer un commentaire