ActionScript 3 AS3:在图像上放置叠加层

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ActionScript 3 AS3:在图像上放置叠加层相关的知识,希望对你有一定的参考价值。

var errorMc:MovieClip;

var assetLoader:Loader = new Loader();
	assetLoader.name = "assetLoader";
	assetLoader.contentLoaderInfo.addEventListener( Event.ACTIVATE, httpStatusHandler, false, 0, true );
	assetLoader.contentLoaderInfo.addEventListener( Event.COMPLETE, loaderCompleteHandler, false, 0, true );
	assetLoader.load( new URLRequest("http://farm3.static.flickr.com/2068/2226178289_3f9556c08f_b.jpg") );

var appStage:Stage = stage;
	appStage.addEventListener( Event.ADDED, addedHandler, false, 0, true );
	appStage.addEventListener( Event.REMOVED, removedHandler, false, 0, true );

function displayMessage( notice:* ):void
{
	errorMc = new MovieClip();
	errorMc.mouseChildren = false;
	errorMc.name = "messageMc";
	errorMc.graphics.beginFill(0x000000, 0.5);
	errorMc.graphics.drawRect( 0, 0, appStage.stageWidth, appStage.stageHeight );
	errorMc.graphics.endFill();

	var tf:TextFormat = new TextFormat();
		tf.font = "Verdana";
		tf.size = 16;
		tf.color = 0xffffff;
		tf.rightMargin = 10;
		tf.leftMargin = 10;

	var displayTxt:TextField = new TextField();
		displayTxt.width = errorMc.width;
		displayTxt.height = errorMc.height;
		displayTxt.wordWrap = true;
		displayTxt.selectable = false;
		displayTxt.text = notice.toString();
		displayTxt.setTextFormat( tf );

	errorMc.addChild( displayTxt );
	appStage.addChild( errorMc );
}

function httpStatusHandler( e:* ):void
{
	trace( e );
	displayMessage("httpStatusHandler:" + e );
}

function centerBitmap( bmp:Bitmap ):void
{
	var centerPoint:Point = new Point( appStage.stageWidth / 2, appStage.stageHeight / 2 );
	
	bmp.x = centerPoint.x - ( bmp.width / 2 );
	bmp.y = centerPoint.y - ( bmp.height / 2 );
}

function getAspectRatio( width:Number, height:Number ):Number
{
	if (width > height) 
		return ( width / height );
	else 
		return ( height / width );
}

function adjustImageSize( bmp:Bitmap ):void
{
	var aspectRatio:Number = getAspectRatio( bmp.width, bmp.height );

	if( bmp.height > bmp.width && bmp.height > appStage.stageHeight ){
		bmp.height = appStage.stageHeight;
		bmp.width = bmp.height / aspectRatio;
	} else if ( bmp.width > appStage.stageWidth ) {
		bmp.width = appStage.stageWidth;
		bmp.height = bmp.width / aspectRatio;
	}
	centerBitmap( bmp );
}

function loaderCompleteHandler( e:Event ):void
{
	var bmp:Bitmap = e.currentTarget.content
	adjustImageSize( bmp );
	appStage.addChild( e.currentTarget.loader );
}

function addedHandler( e:Event ):void
{
	if ( e.target.name == "assetLoader") {
		if ( appStage.getChildByName("messageMc") != null) {
			var error:DisplayObject = appStage.getChildByName("messageMc");
			if ( appStage.contains( error ) ) {
				appStage.swapChildren( appStage.getChildByName( e.target.name ) , error );
			}
		}
	}
}

function removedHandler( e:Event ):void {
	trace( "removedHandler: " + e.target.name );
}

以上是关于ActionScript 3 AS3:在图像上放置叠加层的主要内容,如果未能解决你的问题,请参考以下文章

AS3:在图像上放置覆盖图

ActionScript 3 AS3:图像加载

ActionScript 3 as3全背景图像类

ActionScript 3 在as3中加载图像时浏览crossdomain.xml文件

ActionScript 3 AS3获取图像的平均颜色

ActionScript 3 AS3 - 来自URL的缓存图像