ActionScript 3.0 学习笔记(二)

使用HTTP请求进行URL导航

flash中最普通的http请求是使用URLRequest类和navigateToURL()方法进行URL导航。

创建HTTP请求

在创建HTTP请求时,需要URLRequest类参与处理所有的通信。

在创建HTTP请求之前,必须新建一个URLRequest对象:

var urlRequest:URLRequest = new URLRequest();

另外,URLRequest类在构造方法中有一个可选的参数用于指定HTTP请求的URL:

var urlRequest:URLRequest = new URLRequest("http://www.websote.com");

新建一个URLRequest对象之后,就乐意将其传递至任何需要HTTP请求对象的方法。

导航至URL

navigateToURL(urlRequest);

调用navigateToURL()后将会在同一个浏览器窗口中打开一个新的网页,若要在新的标签页或者窗口中浏览,需要指定方法的第二个参数。以下的代码通过指定第二个参数为_blank在新的窗口中打开网址:

navigateToURL(urlRequest,"_blank");

外部加载文本内容

创建一个URLLoader对象

var ulLoader:URLLoader = new URLLoader();

将外部文件加载进URLLoader对象之前,需要创建一个URLRequest对象处理HTTP请求,并且使用该类的load方法加载文件:

var urRequest:URLRequest = new URLRequest('http://www.website.com/');

var ulLoader:URLLoader = new URLLoader();

urLoader.load(urRequest);

监听URLLoader对象的COMPLETE事件

var urRequest:URLRequest = new URLRequest('http://www.focusonmedia.com/vps/as3/externaltext.txt');

var ulLoader:URLLoader = new URLLoader();

urlLoader.addEventListener(Event.COMPLETE,loaderComleteHandler);

function loaderComleteHandler(evt:Event):void{

trace(evt.target.data);

}

ulLoader.load(urRequest);

使用urlloader对象加载内容并跟踪其载入进度

var urRequest:URLRequest = new URLRequest('http://www.focusonmedia.com/vps/as3/externaltext.txt');
var ulLoader:URLLoader = new URLLoader();
ulLoader.addEventListener(IOErrorEvent.IO_ERROR,loaderErrorHandler);
ulLoader.addEventListener(ProgressEvent.PROGRESS,loaderProgressHandler);
ulLoader.addEventListener(Event.COMPLETE,loaderCompleteHandler);
ulLoader.load(urRequest);
function loaderErrorHandler(evt:IOErrorEvent):void
{
	trace(evt.text);
}

function loaderProgressHandler(evt:ProgressEvent):void{
	var bloaded:Number = evt.target.bytesLoaded;
	var bTotal:Number = evt.target.bytesTotal;
	trace(bloaded + '/' + bTotal + 'has loaded');
}
function loaderCompleteHandler(evt:Event):void{
	trace(evt.target.data);
}

给urlloader对象添加IOErrorEvent并显示URLRequest对象中错误URL引发的错误

var urRequest:URLRequest = new URLRequest('http://www.focusonmedia.com/vps/as3/externaltext.txt');
var ulLoader:URLLoader = new URLLoader();
var tfDisplay:TextField = new TextField();
tfDisplay.width = stage.stageWidth;
tfDisplay.height = stage.stageHeight;
tfDisplay.wordWrap = true;
tfDisplay.border = true;
ulLoader.addEventListener(IOErrorEvent.IO_ERROR,loaderErrorHandler);
ulLoader.addEventListener(ProgressEvent.PROGRESS,loaderProgressHandler);
ulLoader.addEventListener(Event.COMPLETE,loaderCompleteHandler);
ulLoader.load(urRequest);
addChild(tfDisplay);
function loaderErrorHandler(evt:IOErrorEvent):void
{
	trace(evt.text);
}

function loaderProgressHandler(evt:ProgressEvent):void{
	var bloaded:Number = evt.target.bytesLoaded;
	var bTotal:Number = evt.target.bytesTotal;
	trace(bloaded + '/' + bTotal + 'has loaded');
}
function loaderCompleteHandler(evt:Event):void{
	tfDisplay.htmlText = evt.target.data;
}

加载外部文本并使用HTML格式

加载外部文本到文本字段

var cssRequest:URLRequest = new URLRequest('http://www.focusonmedia.com/vps/as3/style.css');
var cssLoader:URLLoader = new URLLoader();
var cssSheet:StyleSheet = new StyleSheet();

var textSample:String = '<p><img src = "http://www.focusonmedia.com/vqs/as3/danda.jpg" alt="d and a" width="111" height="150" class="image"><span class="georgia">Me and Adrian</p>'

var tfDisplay:TextField = new TextField();
tfDisplay.width = stage.stageWidth();
tfDisplay.height = stage.stageHeight();
tfDisplay.wordWrap = true;
tfDisplay.border = true;

cssLoader.addEventListener(Event.COMPLETE,StyleTextHandler);
cssLoader.addEventListener(IOErrorEvent.IO_ERROR,loaderErrorHandler);
function StyleTextHandler(eve:Event):void{
	cssSheet.parseCSS(evt.target.data);
	tfDisplay.StyleSheet = cssSheet;
	tfDisplay.htmlText = textSample;
	addChild(tfDisplay);
}
function loaderErrorHandler(evt:IOErrorEvent):void
{
	tfDisplay.htmlText = 'There was a problem while loading the content styles.Please try again.';
}
cssLoader.load(cssRequest);

加载外部的CSS文件

新建一个文本字段,使用StyleSheet对象设置它的HTML样式

var cssRequest:URLRequest = new URLRequest('http://www.focusonmedia.com/vps/as3/style.css');
var cssLoader:URLLoader = new URLLoader();
var cssSheet:StyleSheet = new StyleSheet();

var textSample:String = '<p><img src = "http://www.focusonmedia.com/vqs/as3/danda.jpg" alt="d and a" width="111" height="150" class="image"><span class="georgia">Me and Adrian</p>'

var tfDisplay:TextField = new TextField();
tfDisplay.width = stage.stageWidth();
tfDisplay.height = stage.stageHeight();
tfDisplay.wordWrap = true;
tfDisplay.border = true;

cssLoader.addEventListener(Event.COMPLETE,StyleTextHandler);
cssLoader.addEventListener(IOErrorEvent.IO_ERROR,loaderErrorHandler);

function StyleTextHandler(eve:Event):void{
	cssSheet.parseCSS(evt.target.data);
	tfDisplay.StyleSheet = cssSheet;
	tfDisplay.htmlText = textSample;
	addChild(tfDisplay);
}
function loaderErrorHandler(evt:IOErrorEvent):void
{
	tfDisplay.htmlText = 'There was a problem while loading the content styles.Please try again.';
}
cssLoader.load(cssRequest);

外部加载素材

使用Loader类加载外部数据

监听assetLoader的属性contentLoaderInfo的COMPLETE事件类型,在图片载入完成后,将Loader对象添加至显示列表,监听并响应一个IOErrorEvent

var assetLoader:Loader = new Loader();
assetLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,loaderCompleteHandler);
assetLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,loaderErrorHandler);
function loaderCompleteHandler(evt:Event):void
{
	trace('Image has loaded.');
	addChild(assetLoader);
}
function loaderErrorHandler(evt:IOErrorEvent):void{
	trace('An error has occurred while loading the image');
}
assetLoader.load(new URLRequest('http://focusonmedia.com/vqs/as3/chees_and_crackers.jpg'));

单击stage从assetLoader中使用unload()方法卸载素材,并移除其事件监听器

var assetLoader:Loader = new Loader();
assetLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,loaderCompleteHandler);
assetLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,loaderErrorHandler);
function loaderCompleteHandler(evt:Event):void
{
	trace('Image has loaded.');
	addChild(assetLoader);
	stage.addEventListener(MouseEvent.MOUSE_UP,unloadAssetHandler);
}
function unloadAssetHandler(evt:MouseEvent):void
{
	assetLoader.unload();
	stage.removeEventListener(MouseEvent.MOUSE_UP,unloadAssetHandler);
}
function loaderErrorHandler(evt:IOErrorEvent):void{
	trace('An error has occurred while loading the image');
}
assetLoader.load(new URLRequest('http://focusonmedia.com/vqs/as3/chees_and_crackers.jpg'));

使用Loader对象加载一个外部图像

var imageLoader:Loader = new Loader();
imageLoader.load(new URLRequest('http://focusonmedia.com/vqs/as3/external_movie.swf'));
imageLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,loaderErrorHandler);
imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,loaderCompleteHandler);
function loaderCompleteHandler(evt:Event):void{
	addChild(imageLoader);
}
function loaderErrorHandler(evt:Event):void{
	trace('An error has occurred while loading the image');
}

加载一个外部SWF文件

监视加载进度

var imageLoader:Loader = new Loader();
imageLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS,loaderProgressHandler);
function loaderProgressHandler(evt:ProgressEvent):void{
	var percentage:Number = evt.bytesLoaded/evt.bytesTotal;
	trace('Percentage'+ percentage);
}
imageLoader.load(new URLRequest('http://www.focusonmedia.com/vqs/as3/chees_and_crackers.jpg'));
addChild(imageLoader);

加载完成后从显示列表移除文本字段

var imageLoader:Loader = new Loader();
imageLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS,loaderProgressHandler);
imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,loaderCompleteHandler);

function loaderProgressHandler(evt:ProgressEvent):void{
	var percentage:Number = evt.bytesLoaded/evt.bytesTotal;
	trace('Percentage:'+percentage);
}
function loaderCompleteHandler(evt:Event):void
{
	removeChild(loadDisplay);
}
imageLoader.load(new URLRequest('http://www.focusonmedia.com/vqs/as3/chees_and_crackers.jpg'));
addChild(imageLoader);

控制外部加载的SWF文件

加载一个外部SWF文件并将其转换为一个MovieClip对象

var swfContainer:MovieClip;
var swfLoader:Loader = new Loader();
swfLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,loaderCompleteHandler);
function loaderCompleteHandler(evt:Event):void
{
	swfContainer = swfLoader.content as MovieClip;
	addChild(swfContainer);
}
swfLoader.load(new URLRequest('http://www.focusonmedia.com/vqs/as3/external_movie.swf'));

调用已加载的SWF的方法

var swfContainer:MovieClip;
var swfLoader:Loader = new Loader();
swfLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,loaderCompleteHandler);
function loaderCompleteHandler(evt:Event):void
{
	swfContainer = swfLoader.content as MovieClip;
	addChild(swfContainer);
	swfContainer.setText('Loaded SWF');
}
swfLoader.load(new URLRequest('http://www.focusonmedia.com/vqs/as3/external_movie.swf'));

猜你喜欢

转载自blog.csdn.net/baidu_29474379/article/details/86238502