图像阵列 - 不在所有图像中循环

时间:2010-11-19 23:57:32

标签: flash actionscript-3

问题 - 它不是循环遍历数组中的图像,而是直接跳到最后一个。

import fl.transitions.Tween;
import fl.transitions.easing.*;

play_btn.addEventListener(MouseEvent.CLICK, goPlay)

var images = new Array();

images[0] = "1.jpg";
images[1] = "2.jpg";
images[2] = "3.jpg";
images[3] = "4.jpg";
images[4] = "5.jpg";
images[5] = "6.jpg";
images[6] = "7.jpg";
images[7] = "8.jpg";
images[8] = "9.jpg";
images[9] = "10.jpg";

var currentImage:int = 0;

function goPlay(e:MouseEvent):void {
 while (currentImage < 10) {

   loadWindow.source = images[currentImage];
   currentImage++;
   var myTween:Tween = new Tween(loadWindow, "alpha", None.easeOut, 1, 0, 3, true)

}
}

1 个答案:

答案 0 :(得分:2)

这是你的问题

在goPlay上,你循环遍历所有这些,并在最后一个结束

基本上你这样做

function goPlay(e:MouseEvent) {
   loadWindow.source = images[0];
   var myTween:Tween = new Tween(loadWindow, "alpha", None.easeOut, 1, 0, 5, true);
   loadWindow.source = images[1];
   var myTween:Tween = new Tween(loadWindow, "alpha", None.easeOut, 1, 0, 5, true);
   loadWindow.source = images[2];
   var myTween:Tween = new Tween(loadWindow, "alpha", None.easeOut, 1, 0, 5, true);
   etc.. 
   loadWindow.source = images[9];
   var myTween:Tween = new Tween(loadWindow, "alpha", None.easeOut, 1, 0, 5, true);
}

成为

function goPlay(e:MouseEvent) {
   loadWindow.source = images[9];
   var myTween:Tween = new Tween(loadWindow, "alpha", None.easeOut, 1, 0, 5, true);
}

所以每次点击时,只会添加最后一张图片

你需要在函数

之外定义变量i

类似这样的事情

var _currentImage:int = 0;
function goPlay(e:MouseEvent) {
   currentImage = (currentImage+1)%images.length;
   loadWindow.source = images[currentImage];
   var myTween:Tween = new Tween(loadWindow, "alpha", None.easeOut, 1, 0, 5, true);
}

希望它很清楚


如果你想让动画自动发生(按下按钮后)......

var _currentImage:int = 0;
var myTimer:Timer = new Timer(5000, 0); // 5 seconds
myTimer.addEventListener(TimerEvent.TIMER, switchPics);

function goPlay(e:MouseEvent) {
   myTimer.start();
}

function goStop(e:MouseEvent) {
   myTimer.stop();
}

function switchPics(event:TimerEvent):void {
   currentImage = (currentImage+1)%images.length;
   loadWindow.source = images[currentImage];
   var myTween:Tween = new Tween(loadWindow, "alpha", None.easeOut, 1, 0, 5, true);
}