本来是想项目弄个东西,后来改变了,不需要,于是出来了个半成品,
里面有二帧加载进度的实现方式.还有mytrace实现.与自适应.xml配置加载,背景音乐加载
main.as
package {
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.text.TextFieldAutoSize;
import flash.text.TextField;
import flash.events.*;
import flash.display.Loader;
import flash.display.StageScaleMode;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.net.URLVariables;
import flash.utils.setInterval;
import flash.utils.clearInterval;
import flash.system.Security;
import flash.media.Sound;
import flash.net.navigateToURL;
import flash.display.StageAlign;
import flash.text.StyleSheet;
public class main extends MovieClip {
private var pBar:TextField;
private var errorObj:TextField;
private var itemObj:Object;
private var _itemI:int;
private var _bgSd:Sound;
private var _stageMc:Sprite;
private var _SW:int = 0;
private var _SH:int = 0;
private var _DW:int = 0;
private var _DH:int = 0;
private var _CO:Object;
public function main() {
this.stage.scaleMode = StageScaleMode.EXACT_FIT;
stage.align = StageAlign.TOP_LEFT;
_stageResize(null);
this.stage.addEventListener(Event.RESIZE, _stageResize);
stop();
pBar = loadTip('');
loaderInfo.addEventListener(ProgressEvent.PROGRESS, enterPRH);
loaderInfo.addEventListener(Event.COMPLETE, enterOKH);
}
private function _stageResize(e) {
_SW = stage.stageWidth;
_SH = stage.stageHeight;
}
private function enterPRH(event:ProgressEvent) {
var tKB = Number(event.bytesTotal / 1024).toFixed(1);
var lKB = Number(event.bytesLoaded / 1024).toFixed(1);
var pr = Math.round(event.bytesLoaded / event.bytesTotal * 100);
loadTipTop(pBar, "主程序已经加载 " + lKB + " KB / 共 " + tKB + " KB; 已经完成 " + pr + " %");
}
private function enterOKH(event:Event) {
_DW = this.loaderInfo.width;
_DH = this.loaderInfo.height;
pBar.visible = false;
errorObj = loadTip('点击关闭出错提示\n');
errorObj.width = _DW;
errorObj.wordWrap = true;
errorObj.autoSize = TextFieldAutoSize.LEFT;
errorObj.addEventListener(MouseEvent.CLICK, errorCLS);
errorObj.textColor = 0xff0000;
errorObj.backgroundColor = 0x000000;
errorObj.x = errorObj.y = 0;
errorObj.visible = false;
_stageMc = new Sprite();
_stageMc.cacheAsBitmap = true;
_stageMc.opaqueBackground = 0xFF0000;
var prevBt = new TextField();
var nextBt = new TextField();
prevBt.addEventListener(MouseEvent.CLICK, prevBtClk);
nextBt.addEventListener(MouseEvent.CLICK, nextBtClk);
nextBt.background = prevBt.background = true;
nextBt.backgroundColor = prevBt.backgroundColor = 0xffffff;
nextBt.border = prevBt.border = true;
nextBt.borderColor = prevBt.borderColor = 0x000000;
nextBt.textColor = prevBt.textColor = 0x000000;
nextBt.autoSize = prevBt.autoSize = TextFieldAutoSize.LEFT;
nextBt.visible = prevBt.visible = false;
nextBt.multiline = prevBt.multiline = true;
nextBt.selectable = prevBt.selectable = false;
nextBt.htmlText = '<font size="30">向<br/>右<br/>翻<br/>动</font>';
prevBt.htmlText = '<font size="30">向<br/>左<br/>翻<br/>动</font>';
prevBt.name = 'prevBt';
nextBt.name = 'nextBt';
var desc = new TextField();
desc.name = 'desc';
desc.textColor = 0x000000;
desc.width = _DW * 0.2;
desc.height = _DH;
desc.x = _DW * 0.8;
desc.y = 0;
addChildAt(_stageMc, 0);
_stageMc.addChild(desc);
nextBt.x = _DW - nextBt.width;
_stageMc.addChild(prevBt);
_stageMc.addChild(nextBt);
_stageMc.addEventListener(MouseEvent.MOUSE_OVER, mcOverH);
_stageMc.addEventListener(MouseEvent.MOUSE_OUT, mcOutH);
_stageMc.width = _DW;
_stageMc.height = _DH;
prevBt.y = nextBt.y = _DH / 2 - nextBt.height /2;
prevBt.x = 0;
loadPic();
}
private function prevBtClk(e) {
tween(prevImg ());
}
private function nextImg () {
var oi = itemObj[_CO.name] + 1;
return itemObj[oi >= itemObj.length ? 0 : oi].img;
}
private function prevImg () {
var oi = itemObj[_CO.name] - 1;
return itemObj[oi < 0 ? itemObj.length - 1 : oi].img;
}
private function nextBtClk(e) {
tween(nextImg());
}
private function mcOverH(e) {
_stageMc.getChildByName('nextBt').visible = _stageMc.getChildByName('prevBt').visible = true;
}
private function mcOutH(e) {
_stageMc.getChildByName('nextBt').visible = _stageMc.getChildByName('prevBt').visible = false;
}
private function loadTipTop(obj, txt) {
pBar.visible = true;
obj.text = txt;
setChildIndex(obj, obj.parent.numChildren-1);
}
private function loadTip(txt:String) {
var tip = new TextField();
tip.background = true;
tip.wordWrap = true;
tip.backgroundColor = 0x0000ff;
tip.autoSize = TextFieldAutoSize.CENTER;
tip.border = true;
tip.borderColor = 0x000000;
tip.textColor = 0xffffff;
if ('' == txt) txt = '\t';
tip.text = txt;
addChild(tip);
tip.y = Math.round(_DH /2 - tip.height /2 );
tip.x = Math.round(_DW /2 - tip.width /2 );
return tip;
}
private function errorTip(txt) {
errorObj.visible = true;
errorObj.appendText(txt + '\n');
var timer = setInterval(function() {if (true != errorObj.visible) return clearInterval(timer); setChildIndex(errorObj, errorObj.parent.numChildren-1);}, 500);
}
private function errorCLS(event:MouseEvent) {
event.currentTarget.text = '点击关闭出错提示\n';
event.currentTarget.visible = false;
}
private function loadPic() {
var src = this.loaderInfo.parameters['xmlSrc'];
/*if (!src) return errorTip('请添加参数.\n'
+ '添加方式一: xxx.swf?xmlSrc=xml的绝对路径;\n'
+ '添加方式二:flashVars="&xmlSrc=xml的绝对路径"; \n'
+ 'xml内容格式\n'
+ '<?xml version="1.0" encoding="utf-8"?>\n'
+ '<root>\n'
+ '<bgsound src="http://xxx.com/背景音乐名字(可不使用背景音乐)"/>\n'
+ '<item>\n'
+ '<img><![CDATA[ 图片绝对路径 ]]></img>\n'
+ '<desc><![CDATA[ 图片摘要 ]]></desc>\n'
+ '</item>\n'
+ '<item>\n'
+ '<img><![CDATA[ 图片2绝对路径 ]]></img>\n'
+ '<desc><![CDATA[ 图片2摘要 ]]></desc>\n'
+ '</item>\n'
+ '</root>\n'
);*/
src = 'http://local.q/x.xml?t=' + Math.random();//测试
var urler = new URLLoader();
configureurler(urler);
var request:URLRequest = new URLRequest(src);
try {
urler.load(request);
} catch (error:Error) {
errorTip(error + ':' + src);
}
}
private function configureurler(dispatcher:IEventDispatcher):void {
dispatcher.addEventListener(Event.COMPLETE, okHandler);
dispatcher.addEventListener(ProgressEvent.PROGRESS, prHandler);
dispatcher.addEventListener(SecurityErrorEvent.SECURITY_ERROR, errorHandler);
dispatcher.addEventListener(IOErrorEvent.IO_ERROR, errorHandler);
}
private function okHandler(event:Event) {
var loader:URLLoader = URLLoader(event.target);
pBar.visible = false;
var xml = new XML(loader.data);
var items = xml.elements('item');
if (items.length() < 1) return errorTip('配置文件的<item></item>对象必须给出.');
itemObj = {length:0};
for (var item in items) {
if (items[item].nodeKind() != 'element') {
errorTip( '第' + ++item + '个<item></item>格式错误');
continue;
}
var desc = items[item].elements('desc');
desc = desc.length() == 1 ? desc[0].children()[0] : null;
if ( !desc || ('text' != desc.nodeKind()) || /^\s*$/.test(desc) ) {
errorTip( '第' + ++item + '个<item></item>对象中<desc></desc>无效:不是字符串[' + desc + ']');
continue;
}
var img = items[item].elements('img');
img = img.length() == 1 ? img[0].children()[0] : null;
if ( !img || ('text' != img.nodeKind()) || !/^\s*http\:\/\/.+/i.test(img) ) {
errorTip( '第' + ++item + '个<item></item>对象中<img></img>无效:不存在/不是img绝对地址字符串[' + img + ']');
continue;
}
img = img.toString().replace(/[\n\r\t]/mg, '');
itemObj[itemObj.length++] = {img:img, desc:desc.toString()};
}
if (itemObj.length < 1) {
errorTip('配置全部无效');
return;
}
var bgsound = xml.elements('bgsound');
if (1 == bgsound.length()) {
bgsound = bgsound[0].attribute('src');
if ( !bgsound.length() || !/^http\:\/\/.+/i.test(bgsound[0].toString()) ) bgsound = null
else {
bgsound = bgsound[0].toString();
}
} else bgsound = null;
loadSound(bgsound);
}
private function loadSound(src) {
if (src) {
try{
_bgSd = new Sound();
configureSder(_bgSd);
_bgSd.load(new URLRequest(src));
} catch (e) {errorTip('加载背景音乐[' + src + ']出错:' + e);}
} else loadItem(0);
}
private function configureSder(obj:IEventDispatcher):void {
obj.addEventListener(Event.COMPLETE, sdOk2next);
obj.addEventListener(ProgressEvent.PROGRESS, sdprHandler);
obj.addEventListener(IOErrorEvent.IO_ERROR, sderrorHandler);
}
private function sdOk2next(event:Event) {
var loop = 9999999;
_bgSd.play(0, loop);
loadItem(0);
pBar.visible = false;
}
private function sdprHandler(event:ProgressEvent):void {
var tKB = Number(event.bytesTotal / 1024).toFixed(1);
var lKB = Number(event.bytesLoaded / 1024).toFixed(1);
var pr = Math.round(event.bytesLoaded / event.bytesTotal * 100);
loadTipTop(pBar, "背景音乐[" + _bgSd.url + "]已经加载 " + lKB + " KB / 共 " + tKB + " KB; 已经完成 " + pr + " %");
}
private function sderrorHandler(event:Event) {
errorTip('加载背景音乐出错:' + event);
loadItem(0);
pBar.visible = false;
}
private function prHandler(event:ProgressEvent):void {
var tKB = Number(event.bytesTotal / 1024).toFixed(1);
var lKB = Number(event.bytesLoaded / 1024).toFixed(1);
var pr = Math.round(event.bytesLoaded / event.bytesTotal * 100);
loadTipTop(pBar, "配置文件已经加载 " + lKB + " KB / 共 " + tKB + " KB; 已经完成 " + pr + " %");
}
private function errorHandler(event:Event):void {
errorTip('加载配置文件:' + event);
}
private function configureimger(obj:IEventDispatcher):void {
obj.addEventListener(Event.COMPLETE, imgOk2next);
obj.addEventListener(ProgressEvent.PROGRESS, imgprHandler);
obj.addEventListener(IOErrorEvent.IO_ERROR, imgerrorHandler);
}
private function imgOk2next(event:Event) {
var img = itemObj[_itemI].img;
try{
var pic = _stageMc.addChildAt(event.target.content, 0);
itemObj[pic.name] = _itemI;
itemObj[_itemI].img = pic;
pic.x = pic.y = 0;
pic.width = _DW * 0.75;
pic.height = _DH;
if (_itemI < 1) {
_CO = pic;
(_stageMc.getChildByName('desc') as TextField).htmlText = itemObj[_itemI].desc;
}
else pic.visible = false;
}catch(e:Error){errorTip(e.toString() + (e.errorID == 2122 ? ',不同域名下的资源安全原因不允许加载' : '') + ':显示图片' + img)}
loadItem(_itemI + 1);
pBar.visible = false;
}
private function imgprHandler(event:ProgressEvent):void {
var tKB = Number(event.bytesTotal / 1024).toFixed(1);
var lKB = Number(event.bytesLoaded / 1024).toFixed(1);
var pr = Math.round(event.bytesLoaded / event.bytesTotal * 100);
loadTipTop(pBar, "图片[" + itemObj[_itemI].img + "]已经加载 " + lKB + " KB / 共 " + tKB + " KB; 已经完成 " + pr + " %");
}
private function imgerrorHandler(event:Event) {
errorTip('加载图片[' + itemObj[_itemI].img + ']出错:' + event);
pBar.visible = false;
itemObj[_itemI] = itemObj[itemObj.length - 1];
itemObj[itemObj.length - 1] = null;
itemObj.length--;
loadItem(_itemI);
}
private function loadItem(i) {
if ( (itemObj.length < 1) || (i >= itemObj.length) ) {
createStage();
return;
}
_itemI = i;
var img = itemObj[i].img + '?r=' + Math.random();
loadTipTop(pBar, '准备加载图片:' + img);
try{
var urler = new Loader();
configureimger(urler.contentLoaderInfo);
var request:URLRequest = new URLRequest(img);
urler.load(request);
} catch (error:Error) {
errorTip(error + ': 加载' + img);
}
}
private function createStage() {
_stageMc.visible = true;
}
private function tween(no) {
_CO.visible = false;
no.visible = true;
_CO = no;
(_stageMc.getChildByName('desc') as TextField).htmlText = itemObj[itemObj[no.name]].desc;
}
}
}