完整的学习了ActionScritp 3.0一下,有些心得,也有些凌乱。主要是临时需要才充的电,实在谈不上系统。在工作完成之余,自己也涂鸦了一下,练习着写了一个简单的图片加载浏览效果,具体的效果见下面链接:
[url=/Demo/ImageAlpha/ImageAlpha.html]/Demo/ImageAlpha/ImageAlpha.html[/url]
功能上很多都没有实现,如图片的链接,图片的手动切换,图片加载等,要等以后来慢慢完善了!
简略的代码如下:
/*
*练习Actionscript 3.0之作
*功能:加载外部图片,并让图片之间轮换显示
*
*Copyright (c) IAwen </>
*2011-04-07
*/
package
{
import flash.display.Sprite;
import flash.display.Loader;
import flash.events.Event;
import flash.events.TimerEvent;
import flash.net.*;
import flash.utils.*;
[SWF(width = "300",height = "400")]
public class ImageAlpha extends Sprite
{
private var timer:Timer;
private var sprite:Sprite;
private var imgHandles:Array;
private var img:Array;
private var imgUrl:Array;
private var config:XML;
private var imgIndex:uint = 0; //图片索引,用来控制图片载入的顺序
private var currentIndex:uint = 0; //当前图片索引
private var delayTime:uint; //显隐之间的间隔时间
public function ImageAlpha()
{
var loader:URLLoader = new URLLoader ;
loader.dataFormat = URLLoaderDataFormat.TEXT;
loader.addEventListener(Event.COMPLETE, loadConfigComplete);
loader.load(new URLRequest("data.xml"));
}
private function loadConfigComplete(evt:Event):void
{
config = new XML(evt.target.data);
init();
var loader:Loader=new Loader();
loader.load(new URLRequest(img[0]));
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeLoad);
}
//初始化Flash配置
private function init():void
{
delayTime = uint(config.delayTime);
sprite=new Sprite();
addChild(sprite);
var fps:uint = uint(config.fps);
var mspf:int = Math.round(1 / fps * 1000);
timer = new Timer(mspf);
imgHandles=new Array();
img=new Array();
imgUrl = new Array();
trace(config.item.length);
for (var i:uint=0; i<config.item.length(); i++) {
img.push(config.item[i].img);
imgUrl.push(config.item[i].url);
}
}
private function completeLoad(event:Event):void
{
imgHandles[imgIndex] = event.target.content;
imgHandles[imgIndex].alpha = 0;
imgHandles[imgIndex].scaleX = stage.stageWidth / event.target.width;
imgHandles[imgIndex].scaleY = stage.stageHeight / event.target.height;
imgHandles[imgIndex].x=imgHandles[imgIndex].y=0;
sprite.addChild(imgHandles[imgIndex]);
if (imgIndex == 0) {//在载入第一幅图片时,预先显示
timer.addEventListener(TimerEvent.TIMER, onFirstTimer);
timer.start();
}
if (imgIndex==(img.length-1)) {//在载入最后一幅图片时,启动定时器
setInterval(myRepeatingFunction, delayTime);
}
imgIndex++;
//继续加载下一张,如果没有则退出
if (imgIndex > img.length - 1) {
return;
}
var loader:Loader=new Loader();
loader.load( new URLRequest(img[imgIndex]) );
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeLoad);
}
private function onFirstTimer(event:Event):void
{
imgHandles[0].alpha += 0.02;
if (imgHandles[0].alpha >= 1) {
imgHandles[0].alpha = 1;
timer.removeEventListener(TimerEvent.TIMER, onFirstTimer);
}
}
private function myRepeatingFunction():void
{
currentIndex++;
if (currentIndex>(img.length-1)) {
currentIndex = 0;
}
timer.addEventListener(TimerEvent.TIMER, onRepeaterTimer);
timer.start();
}
private function onRepeaterTimer(event:TimerEvent):void
{
var prevImg:uint = currentIndex - 1;
var nextImg:uint = currentIndex;
if (currentIndex == 0) {//轮播到最后一幅图片时,
prevImg=(img.length-1);
}
imgHandles[prevImg].alpha -= 0.02;
imgHandles[nextImg].alpha += 0.02;
if (imgHandles[nextImg].alpha >= 1) {
imgHandles[prevImg].alpha = 0;
imgHandles[nextImg].alpha = 1;
timer.removeEventListener(TimerEvent.TIMER, onRepeaterTimer);
}
event.updateAfterEvent();
}
}
}
对应的配置文件data.xml格式如下:
[codes=xml]
images/demo1.jpg
images/demo2.jpg
images/demo3.jpg
images/demo4.jpg
看了一半赶紧洗把脸去了、
这个支持了。
[emot]zan[/emot]博主厉害啊,这么牛逼,看不太懂。
看见代码,眼睛都花了
这么长的代码,博主真厉害。我很佩服你。
[emot]zan[/emot]