0

我正在开发一个 Pure ActionScript 3.0 项目,其中允许用户将自己的 swfs 上传到应用程序中。

我的目标是加载 swf 并完全适合舞台。问题是,当用户上传被屏蔽的 swf 时,它并不完全适合其他 swf 工作正常的阶段。

考虑我的舞台分辨率为 800x600,当用户上传舞台宽度和高度为 600x550 但内容宽度和高度为 900 x 800 的蒙版 swf 时。

如何将这种 swf 融入舞台?

4

2 回答 2

1

根据确定蒙版 SWF 的边界,使用BitmapData.

例如,我创建了一个 400x400 的 swf,它从 100x100 开始屏蔽为 200x200(下载

蒙面瑞士法郎

加载此 SWF 将报告 400x400 作为宽度/高度。

要获得未屏蔽内容的可见边界,您可以实现:

package
{
    import flash.display.BitmapData;
    import flash.display.Loader;
    import flash.display.Sprite;
    import flash.display.StageAlign;
    import flash.display.StageScaleMode;
    import flash.events.Event;
    import flash.geom.Rectangle;
    import flash.net.URLRequest;

    [SWF(width = 400, height = 400, backgroundColor = 0xefefef, frameRate = 60)]
    public class X extends Sprite
    {

        private var loader:Loader;

        public function X()
        {
            stage.scaleMode = StageScaleMode.NO_SCALE;
            stage.align = StageAlign.TOP_LEFT;

            loader = new Loader();
            var url:URLRequest = new URLRequest("http://jasonsturges.com/labs/stack-overflow/examples/swf-mask/masked.swf");
            loader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler);
            loader.load(url);
        }

        protected function completeHandler(event:Event):void
        {
            var bitmapData:BitmapData = new BitmapData(loader.width, loader.height, true, 0);
            bitmapData.draw(loader);
            var bounds:Rectangle = bitmapData.getColorBoundsRect(0xff000000, 0xff000000, true);
            trace(bounds);
        }
    }
}

其中报告:

(x=100, y=100, w=200, h=200)

因此,可见内容是从 100x100 开始的 200x200。

根据舞台的大小,您需要确定您的约束是什么,以避免舞台上出现空白/空白区域。

这可以使用比率变量来实现:

var ratio:Number = 1.0;

比例缩放(按宽度):

var ratio:Number = stage.stageWidth / bounds.width;

loader.scaleX = ratio;
loader.scaleY = ratio;
loader.x = -(bounds.x * ratio);
loader.y = -(bounds.y * ratio);

拉伸显示对象以适应舞台:

ader.scaleX = stage.stageWidth / bounds.width;
loader.scaleY = stage.stageHeight / bounds.height;
loader.x = -(bounds.x * (stage.stageWidth / bounds.width));
loader.y = -(bounds.y * (stage.stageHeight / bounds.height));

所以,总而言之,为了适应加载的内容,拉伸以填充整个舞台,考虑蒙版偏移:

package
{
    import flash.display.BitmapData;
    import flash.display.Loader;
    import flash.display.Sprite;
    import flash.display.StageAlign;
    import flash.display.StageScaleMode;
    import flash.events.Event;
    import flash.geom.Rectangle;
    import flash.net.URLRequest;

    [SWF(percentWidth = 100, percentHeight = 100, backgroundColor = 0xefefef, frameRate = 60)]
    public class X extends Sprite
    {

        private var loader:Loader;

        public function X()
        {
            stage.scaleMode = StageScaleMode.NO_SCALE;
            stage.align = StageAlign.TOP_LEFT;

            loader = new Loader();
            var url:URLRequest = new URLRequest("http://jasonsturges.com/labs/stack-overflow/examples/swf-mask/masked.swf");
            loader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler);
            loader.load(url);
        }

        protected function completeHandler(event:Event):void
        {
            var bitmapData:BitmapData = new BitmapData(loader.width, loader.height, true, 0);
            bitmapData.draw(loader);
            var bounds:Rectangle = bitmapData.getColorBoundsRect(0xff000000, 0xff000000, true);
            trace(bounds);

            addChild(loader);

            loader.scaleX = stage.stageWidth / bounds.width;
            loader.scaleY = stage.stageHeight / bounds.height;
            loader.x = -(bounds.x * (stage.stageWidth / bounds.width));
            loader.y = -(bounds.y * (stage.stageHeight / bounds.height));
        }
    }
}
于 2012-10-30T05:41:01.090 回答
0

您应该比较内容和持有人比率以获得准确的拟合。如果您正在加载外部 swf,您的宽度/高度信息应该来自loaderInfoSWF 的元数据,而不是加载的 displayObject 内容的大小。这是一个指向 wonderfl 的链接,可以查看它的工作原理:http ://wonderfl.net/c/eRYv

这里是比较比率的简单函数:

function makeItFit(cont:Sprite, hold:Sprite):void{
        var holderRatio:Number = hold.width/hold.height;
        var contentRatio:Number = cont.width/cont.height;
        //compare both ratios to get the biggest side
        if(holderRatio < contentRatio){
           cont.width = hold.width; //width bigger, lets make it fit
           cont.scaleY = cont.scaleX; //adjust scale to avoid distortion
        }else{
           cont.height = hold.height;
           cont.scaleX = cont.scaleY;
        }
        //now the size is ok, lets center the thing
        cont.x = (hold.width - cont.width)/2;
        cont.y = (hold.height - cont.height)/2;
}

loaderInfo请记住,如果加载外部 swf ,请使用宽度/高度数据。这只是一个例子。

于 2012-10-30T22:09:49.397 回答