天天看点

as之图片滚动

package

{

 import com.RectangleButton;

 import com.adobe.serialization.json.JSON;

 import com.greensock.TweenLite;

 import flash.display.Loader;

 import flash.display.LoaderInfo;

 import flash.display.Sprite;

 import flash.display.Stage;

 import flash.display.StageScaleMode;

 import flash.events.Event;

 import flash.events.IOErrorEvent;

 import flash.events.MouseEvent;

 import flash.net.URLLoader;

 import flash.net.URLRequest;

 import util.JsonUtil;

 [SWF(width="1000", height="560", frameRate="24", backgroundColor="#000000")]

 public class test extends Sprite

 {

  private var jsonObj:Object = new Object();

  public  var urlLoader:URLLoader = new URLLoader();

//  private var loader : Loader = new Loader();

  private var topSprite : Sprite;

  private var imageSprite : Sprite;

  private var imgArray:Array = new Array();

  private var spriteArray:Array = new Array();

  private var imgXYScaleJson:Array = [{xValue:350, yValue:280, scaleValue:1, index:2, widthValue:300, heightValue:200},

   {xValue:180, yValue:160, scaleValue:0.9, index:1, widthValue:270, heightValue:180},

   {xValue:380, yValue:80, scaleValue:0.8, index:0, widthValue:240, heightValue:160},

   {xValue:550, yValue:160, scaleValue:0.9, index:1, widthValue:270, heightValue:180}

  ];

  public function test(){

   this.addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);

  }

  private function onAddedToStage( e:Event ):void{

   this.init();

  }

  private function init():void{

   this.graphics.lineStyle(3,0xff0000,1)

   this.graphics.drawRect(0,0,1000,560);

   getJson();

  }

  private function getJson():void{

   urlLoader.addEventListener(Event.COMPLETE, handleComplete);

   urlLoader.load( new URLRequest("text/1.txt") );

  }

  private function handleComplete( e:Event ):void{

   try{

    this.jsonObj = JSON.decode(String(urlLoader.data).replace(/[/r/n]/g, ""));

    imgArray = jsonObj.data.images;

    this.addImages(imgArray);

   }catch(e:Error){

    trace("JSON格式错误");

    return;

   }

  }

  private var i:int = 0;

  private function addImages(imgArray : Array):void{

   if ( i<imgArray.length ) {

    var loader:Loader = new Loader();

    loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);

    loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onError);

    loader.load(new URLRequest(imgArray[i++].pic));

   } else {

    this.afterAddImages();

   }

  }

  private function onComplete(e : Event):void{

   var loader:Loader = LoaderInfo(e.target).loader;

   var s : Sprite = new Sprite();

   s.addChild(loader);

   s.height = 200;

   s.width = 300;

   this.addChild(s);

   spriteArray.push(s);

   this.addImages(imgArray);

  }

  private function onError(e : IOErrorEvent):void{

   trace("error");

  }

  private function afterAddImages():void{

   this.locateImages();

   var first:Sprite = spriteArray[0];

//   TweenLite.to(first, 0.5, {x:25, y:25});

  }

  private function locateImages():void{

   var i:int=0;

   var s:Sprite;

   for (i=0;i<spriteArray.length;i++) {

    s = spriteArray[i];

    s.x = imgXYScaleJson[i].xValue;

    s.y = imgXYScaleJson[i].yValue;

    s.width = imgXYScaleJson[i].widthValue;

    s.height = imgXYScaleJson[i].heightValue;

    this.setChildIndex(s, imgXYScaleJson[i].index);

    s.addEventListener(MouseEvent.CLICK, clikeImg);

   }

  }

  private function clikeImg( e:Event ):void{

   var s:Sprite = e.currentTarget as Sprite;

   var index:int = spriteArray.indexOf(s,0);

   var i:int = 0,j:int = 0, k:int = 0;

   for (i=0;i<(spriteArray.length-index)%4;i++) {

    this.moveClocwiseOnetime()

   }

//   if (mouseX >= 500) {

//    for (i=0;i<(spriteArray.length-index)%4;i++) {

//     this.moveClocwise(index);

//     var temp:Sprite = spriteArray[0];

//     for (k=0;k<spriteArray.length;i++) {

//      spriteArray[(spriteArray.length-1-k)%3] = spriteArray[spriteArray.length-1-k-1];

//     }

//     spriteArray[1] = temp;

//     for (j=0;j<spriteArray.length;j++) {

//      TweenLite.to(spriteArray[i], 0.7, {x:imgXYScaleJson[(i+1)%3].xValue, y:imgXYScaleJson[(i+1)%3].yValue});

//      spriteArray[i].x = imgXYScaleJson[(i+1)%3].xValue;

//      spriteArray[i].y = imgXYScaleJson[(i+1)%3].yValue;

//      spriteArray[i].scaleX = s.scaleX*imgXYScaleJson[(i+1)%3].scaleValue;

//      spriteArray[i].scaleY = s.scaleY*imgXYScaleJson[(i+1)%3].scaleValue;

//      

//      this.setChildIndex(s, imgXYScaleJson[(i+1)%3].index);

//      var temp:Sprite = spriteArray[0];

//      for (k=0;k<spriteArray.length;i++) {

//       spriteArray[(spriteArray.length-1-k)%3] = spriteArray[(spriteArray.length-1-k-1)%3];

//      }

//      spriteArray[1] = temp;

//      spriteArray[(i+1)%3] = spriteArray[i];

//     }

//     var temp:Sprite = spriteArray[(index+1)%spriteArray.length];

//     spriteArray[(index+1)%spriteArray.length] = spriteArray[index]

//     TweenLite.to(s, 0.7, {x:imgXYScaleJson[index+i].xValue, y:imgXYScaleJson[index+i].yValue});

//     TweenLite.to(s, 0.7, {x:imgXYScaleJson[i].xValue, y:imgXYScaleJson[i].yValue});

//    }

//   }

  }

  var tweenTimes:int = 0;

  private function moveClocwise(index:int):void{

   for (i=0;i<(spriteArray.length-index)%4;i++) {

    trace(1);

   }

   tweenTimes = 0;

   TweenLite.to(spriteArray[index], 0.7,

    {x:imgXYScaleJson[(index+1)%4].xValue, y:imgXYScaleJson[(index+1)%4].yValue,

     onComplete: onFinishTween, onCompleteParams:[index]

    }

   );

  }

  private function onFinishTween(index:int):void{

//   spriteArray重新排序,所有元素index加1

//   var temp:Sprite = spriteArray[0];

//   var k:int = 0;

//   for (k=0;k<spriteArray.length;i++) {

//    spriteArray[(spriteArray.length-1-k)%3] = spriteArray[spriteArray.length-1-k-1];

//   }

//   spriteArray[1] = temp;

//   trace(tweenTimes);

   tweenTimes++;

   var newIndex:int = (index+1)%4;

   trace(newIndex);

   if (tweenTimes<spriteArray.length) {

    TweenLite.to(spriteArray[newIndex], 0.7,

     {x:imgXYScaleJson[(newIndex+1)%4].xValue, y:imgXYScaleJson[(newIndex+1)%4].yValue,

      onComplete: onFinishTween, onCompleteParams:[newIndex]

     }

    );

   } else {

    //spriteArray重新排序,所有元素index加1

    var temp:Sprite = spriteArray[0];

    var k:int = 0;

    for (k=0;k<spriteArray.length;i++) {

     spriteArray[(spriteArray.length-1-k)%3] = spriteArray[spriteArray.length-1-k-1];

    }

    spriteArray[1] = temp;

   }

  }

  private function moveClocwiseOnetime():void{

   var i:int = 0;

   for (i=0;i<spriteArray.length;i++) {

    TweenLite.to(spriteArray[i], 0.7, {x:imgXYScaleJson[(i+1)%4].xValue, y:imgXYScaleJson[(i+1)%4].yValue});

   }

   //spriteArray重新排序,所有元素index加1

   var temp:Sprite = spriteArray[0];

   for (i=0;i<spriteArray.length;i++) {

    spriteArray[(spriteArray.length-i)%4] = spriteArray[spriteArray.length-i-1];

   }

   spriteArray[1] = temp;

   //重新指定图片大小和叠放顺序

   var s:Sprite;

   for (i=0;i<spriteArray.length;i++) {

    s = spriteArray[i];

    s.width = imgXYScaleJson[i].widthValue;

    s.height = imgXYScaleJson[i].heightValue;

    this.setChildIndex(s, imgXYScaleJson[i].index);

   }

  }

 }

}