我正在开发一个Pure ActionScript 3.0项目,允许用户将自己的swfs上传到应用程序中。
我的目标是加载swf并完全适合舞台。问题是,当用户上传被屏蔽的swf时,当其他swfs正常工作时,它不完全适合舞台。
考虑我的舞台分辨率是800x600,当用户上传带有舞台宽度且高度为600x550但内容宽度和高度为900 x 800的蒙版swf时。
如何将这种瑞士法郎融入舞台?
答案 0 :(得分:1)
在确定蒙版SWF的范围时,请使用BitmapData
。
例如,我创建了一个400x400 swf,从100x100开始屏蔽到200x200(download)
加载此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)
因此,可见内容为200x200,从100x100开始。
在调整舞台大小时,您需要确定您的约束条件,以避免舞台上出现空白/空白区域。
这可以使用比率变量来实现:
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));
}
}
}
答案 1 :(得分:0)
您应该比较内容和持有者比率以获得准确的拟合。如果要加载外部swf,则宽度/高度信息应来自SWF的loaderInfo
元数据,而不是加载的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;
}
请记住,如果加载外部swf,请使用loaderInfo
宽度/高度数据。这只是一个例子。