ActionScript 3 来自AS3的PHP图像上传器

//CODE
import com.adobe.images.JPGEncoder;
import flash.display.BitmapData;
import flash.net.URLRequest;
import flash.net.URLRequestHeader;
import flash.net.URLRequestMethod;
import flash.net.navigateToURL;

var myBitmapData:BitmapData = new BitmapData(100, 100, false, 0xFFFFFF);
var jpgEncoder:JPGEncoder = new JPGEncoder(85);
var jpgStream:ByteArray = jpgEncoder.encode(myBitmapData);
var fileName:String='Name';
jpgStream.writeMultiByte("_s_"+fileName,"us-ascii");
var header:URLRequestHeader = new URLRequestHeader("Content-type", "application/octet-stream");
var request:URLRequest = new URLRequest("myJpgSaver.php");
request.requestHeaders.push(header);
request.method = URLRequestMethod.POST;
request.data = jpgStream;
navigateToURL(request, "_blank");	
myBitmapData.dispose();

//myJpgSaver.php
<?PHP
if (isset($GLOBALS["HTTP_RAW_POST_DATA"]))
{
	$parts=explode("_s_", $GLOBALS["HTTP_RAW_POST_DATA"]);
	$name=$parts[1];	
	$fp = fopen( $name.".jpg", 'wb' );	
	fwrite( $fp, $parts[0] );   
	fclose( $fp );		
}
?>

ActionScript 3 如何加载最新的XML文件来解决缓存问题

Many browser have cache for load file.But some one need client to see latest data so that we must delete the cache.Then we need to solve the cache problem.

About load file,we often load xml files/swf files/images files etc.... we can use ?cache =new Date().getTime

For example:

var load:URLLoader = new URLLoader();
load.load(new URLRequest("data.xml" + "?cache=" + new Date().getTime()));

or

var load:Loader = new Loader();
load.load(new URLRequest("atf.jpg" + "?cache=" + new Date().getTime()));

ActionScript 3 获取嵌入SWF的页面的URL:附录

root.loaderInfo.url;

ActionScript 3 AS3 Draw Square,Blur和Gradiant Fill

var squareDrawing:MovieClip = new MovieClip();
this.addChild(squareDrawing);

squareDrawing.graphics.lineStyle(1, 0x000000, 1);
var fillType:String = GradientType.RADIAL;
var colors:Array = [0xFF0000, 0x0000FF];
var alphas:Array = [100, 100];
var ratios:Array = [0, 255];
var matr:Matrix = new Matrix();
matr.createGradientBox(200, 100, 0, -50, 100);
var spreadMethod:String = SpreadMethod.REFLECT; //REPEATE, PAD, OR REFLECT

squareDrawing.graphics.beginGradientFill(fillType, colors, alphas, ratios, matr, spreadMethod);


squareDrawing.graphics.drawRect (0, 0, 100, 300);
squareDrawing.graphics.endFill();
squareDrawing.graphics.drawRoundRect(200, 0, 100, 300, 50);

var blur:BlurFilter = new BlurFilter();

blur.blurX = 10;
blur.blurY = 10;
blur.quality = 1;

var filterArray:Array = new Array(blur);
squareDrawing.filters = filterArray;


squareDrawing.x = 500;
squareDrawing.y = 100;

ActionScript 3 AS3:使用URLLoader进行文本和XML

var assetLoader:URLLoader = new URLLoader();
    assetLoader.addEventListener(ProgressEvent.PROGRESS, progressHandler);
    assetLoader.addEventListener(Event.COMPLETE, completeHandler);

function progressHandler(e:Event):void
{
	trace(e.currentTarget.bytesLoaded + " / " + e.currentTarget.bytesTotal);
}
function completeHandler(e:Event):void
{
	trace("completeHandler:" + e.currentTarget + " :: " + e.currentTarget.dataFormat + " :: " + 	e.currentTarget.data);
}

ActionScript 3 AS3:使用SWFObject2和AS3传递变量

/*************************************
This is how the SWF Object would look
*************************************/
<script type="text/javascript" src="js/swfobject.js"></script>
<script type="text/javascript">
	var flashVars = { key: "AAA555XXXYYYZZZ", title: "This is my Title" };
	var flashParams = { menu: "false"};
        var flashID = { id : "swfContent" };
	swfobject.embedSWF("main.swf", "swfContent", "550", "400", "9.0.0", "expressInstall.swf",  flashVars, flashParams, flashID);	
</script>

/*************************************
Place this code in the Actions window of Flash CS3
*************************************/

var APP_ID:String = getFlashVars().key;
var title:String = getFlashVars().title;

function getFlashVars():Object
{
 return Object( LoaderInfo( this.loaderInfo ).parameters );
}

ActionScript 3 AS3 Singleton

package ###PACKAGE_PATH###
{
	private static var _instance:###CLASS_NAME###;
	
	public class ###CLASS_NAME###
	{
		public static function getInstance():###CLASS_NAME###
		{
			if ( !_instance )
				_instance = new ###CLASS_NAME###( new SingletonEnforcer() );
			return _instance;
		}
		public function ###CLASS_NAME###( se:SingletonEnforcer )
		{
			if ( !se )
				throw( new Error( "use ###CLASS_NAME###.getInstance() instead!!" ) );
		}
	}
	
}
class SingletonEnforcer{}

ActionScript 3 如果鼠标没有移动,请隐藏一些内容。

import flash.utils.Timer;

this.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);

var myTimer:Timer = new Timer(1000,1);
myTimer.addEventListener("timer", function() {	
	//Do what ever you want. Like tween away a movie control using tweenmax.
	TweenMax.to(control, 1, { alpha:0, ease:Linear.easeNone } );
});
myTimer.start();



private function onMouseMove(e:Event):void 
{
	control.alpha = 1;
	myTimer.reset();
	myTimer.start();
}

ActionScript 3 使用XML创建AS3幻灯片

// import tweener
import caurina.transitions.Tweener;

// delay between slides
const TIMER_DELAY:int = 5000;
// fade time between slides
const FADE_TIME:int =	1;

// reference to the current slider container
var currentContainer:Sprite;
// index of the current slide
var intCurrentSlide:int = -1;
// total slides
var intSlideCount:int;
// timer for switching slides
var slideTimer:Timer;
// slides holder
var sprContainer1:Sprite;
var sprContainer2:Sprite;
// slides loader
var slideLoader:Loader;
// url to slideshow xml
var strXMLPath:String = "slideshow-data.xml";
// slideshow xml loader
var xmlLoader:URLLoader;
// slideshow xml
var xmlSlideshow:XML;

function init():void {
	// create new urlloader for xml file
	xmlLoader = new URLLoader();
	// add listener for complete event
	xmlLoader.addEventListener(Event.COMPLETE, onXMLLoadComplete);
	// load xml file
	xmlLoader.load(new URLRequest(strXMLPath));

	// create new timer with delay from constant
	slideTimer = new Timer(TIMER_DELAY);
	// add event listener for timer event
	slideTimer.addEventListener(TimerEvent.TIMER, switchSlide);

	// create 2 container sprite which will hold the slides and
	// add them to the masked movieclip
	sprContainer1 = new Sprite();
	sprContainer2 = new Sprite();
	mcSlideHolder.addChild(sprContainer1);
	mcSlideHolder.addChild(sprContainer2);

	// keep a reference of the container which is currently
	// in the front
	currentContainer = sprContainer2;

}

function onXMLLoadComplete(e:Event):void {
	// create new xml with the received data
	xmlSlideshow = new XML(e.target.data);
	// get total slide count
	intSlideCount = xmlSlideshow..image.length();
	// switch the first slide without a delay
	switchSlide(null);
}

function fadeSlideIn(e:Event):void {
	// add loaded slide from slide loader to the
	// current container
	currentContainer.addChild(slideLoader.content);
	// clear preloader text
	mcInfo.lbl_loading.text = "";
	// fade the current container in and start the slide timer
	// when the tween is finished
	Tweener.addTween(currentContainer, {alpha:1, time:FADE_TIME, onComplete:function() { slideTimer.start(); }});
}

function switchSlide(e:Event):void {
	// check, if the timer is running (needed for the
	// very first switch of the slide)
	if(slideTimer.running)
		slideTimer.stop();

	// check if we have any slides left and increment
	// current slide index
	if(intCurrentSlide + 1 < intSlideCount)
		intCurrentSlide++;
	// if not, start slideshow from beginning
	else
		intCurrentSlide = 0;

	// check which container is currently in the front and
	// assign currentContainer to the one that's in the back with
	// the old slide
	if(currentContainer == sprContainer2)
		currentContainer = sprContainer1;
	else
		currentContainer = sprContainer2;

	// hide the old slide
	currentContainer.alpha = 0;
	// bring the old slide to the front
	mcSlideHolder.swapChildren(sprContainer2, sprContainer1);

	// create a new loader for the slide
	slideLoader = new Loader();
	// add event listener when slide is loaded
	slideLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, fadeSlideIn);
	// add event listener for the progress
	slideLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, showProgress);
	// load the next slide
	slideLoader.load(new URLRequest(xmlSlideshow..image[intCurrentSlide].@src));

	// show description of the next slide
	mcInfo.lbl_description.text = xmlSlideshow..image[intCurrentSlide].@desc;
	// show current slide and total slides
	mcInfo.lbl_count.text = (intCurrentSlide + 1) + ” / ” + intSlideCount + ” Slides”;
}

function showProgress(e:ProgressEvent):void {
	// show percentage of the bytes loaded from the current slide
	mcInfo.lbl_loading.text = “Loading…” + Math.ceil(e.bytesLoaded * 100 / e.bytesTotal) + “%”;
}

// init slideshow
init();

ActionScript 3 检测方向鼠标正在移动

function Start() {

	stage.addEventListener(MouseEvent.MOUSE_MOVE, CheckDirection);

}
Start();

var prevX=0;
var prevY=0;
var curX=0;
var curY=0;


var dirX:String="";
var dirY:String="";

function CheckDirection(e:MouseEvent) {


	trace("X movement: " + GetHorizontalDirection() + ", Y movement: " + GetVerticalDirection());

	e.updateAfterEvent();

}

function GetHorizontalDirection():String {

	prevX=curX;
	curX=stage.mouseX;

	if (prevX>curX) {

		dirX="left";

	} else if (prevX < curX) {

		dirX="right";

	} else {

		dirX="none";

	}

	return dirX;

}

function GetVerticalDirection():String {

	prevY=curY;
	curY=stage.mouseY;

	if (prevY>curY) {

		dirY="up";

	} else if (prevY < curY) {

		dirY="down";

	} else {

		dirY="none";

	}

	return dirY;

}