有时候多张图片展示需求中,需要左右循环移动展示,为了以后省时间思考,写个例子:
import com.tweener.transitions.Tweener; import flash.display.Sprite; var _num:int = 3;//始终三张显示图片 var _Number = 278;//图片的宽度 var _objArr:Array = []; var _container:Sprite = new Sprite(); this.addChild(_container); initContainer(); leftBtn.addEventListener(MouseEvent.CLICK,onLeftClick); rightBtn.addEventListener(MouseEvent.CLICK,onRightClick); function initContainer():void { for (var i:int = 0; i<_num*2; i++) { var img:TestImage = new TestImage();//测试的图片元件 img.x = _width * i; img.t.text = (i % _num).toString(); //此处可以加载图片,地址索引: [i % _num],比如arr[i % _num] _objArr.push(img); _container.addChild(img); } } function onLeftClick(e:MouseEvent):void { //向右移动之前,将最后一个元素放到第一个位置 if (isTweening()) { return; } _objArr.unshift(_objArr.pop()); _objArr[0].x = -_width; for (var i:int = 0; i<_objArr.length; i++) { Tweener.addTween(_objArr[i],{x:i*_width,time:1,transition:"easeOutCubic"}); } } function onRightClick(e:MouseEvent):void { if (isTweening()) { return; } for (var i:int = 0; i<_objArr.length; i++) { Tweener.addTween(_objArr[i],{x:i*_width-_width,time:1,transition:"easeOutCubic"}); } setTimeout(onPush,1000); function onPush():void { //向左移动之后,将第一个元素放到最后一个位置 _objArr.push(_objArr.shift()); _objArr[_objArr.length-1].x = _width*(_objArr.length-1); } } function isTweening():Boolean { for (var i:int = 0; i<_objArr.length; i++) { if (Tweener.isTweening(_objArr[i])) { return true; } } return false; }