AS3>按钮按顺序显示数组图像?

时间:2011-11-10 23:44:03

标签: arrays actionscript-3 button

根据阵列内的位置,按钮按顺序显示图像有多难?

我需要 NEXT PREVIOUS 按钮。检查下面的数组。

阵列:

private var images:Array = ["Layer_1.jpg", "Layer_2.jpg", "Layer_3.jpg", "Layer_4.jpg", "Layer_5.jpg", "Layer_6.jpg", "Layer_7.jpg", "Layer_8.jpg"];

按钮:

        private var triangleButton:triangle = new triangle;
        //to call the function slideGames
        triangleButton.addEventListener(MouseEvent.CLICK, slideGames);

装载机:

    private var loader:Loader = new Loader();

完整代码:

package {

import flash.display.Loader;
import flash.net.URLRequest;
import flash.events.Event;
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.display.MovieClip;


    public class play extends MovieClip {

        private var loader:Loader = new Loader();

        private var images:Array = ["Layer_1.jpg", "Layer_2.jpg", "Layer_3.jpg", "Layer_4.jpg", "Layer_5.jpg", "Layer_6.jpg", "Layer_7.jpg", "Layer_8.jpg"];
        private var triangleButton:triangle = new triangle;

           public function play() {
               loader.load(new URLRequest("img/Layer_1.jpg"));
                addChild(loader);
                addChild(triangleButton);

                triangleButton.x = 600;
                triangleButton.y = 200;

                triangleButton.addEventListener(MouseEvent.CLICK, slideGames);



    function slideGames(event:MouseEvent):void
    {

    }

    }
}}

1 个答案:

答案 0 :(得分:1)

试试这个:

package {

  import flash.display.Loader;
  import flash.net.URLRequest;
  import flash.events.Event;
  import flash.display.Sprite;
  import flash.events.MouseEvent;
  import flash.display.MovieClip;


  public class play extends MovieClip {

    private var loader:Loader = new Loader();

    private var images:Array = ["Layer_1.jpg", "Layer_2.jpg", "Layer_3.jpg", "Layer_4.jpg", "Layer_5.jpg", "Layer_6.jpg", "Layer_7.jpg", "Layer_8.jpg"];
    private var triangleButton:triangle = new triangle;

    public function play() {
      loadNextImage();

      addChild(loader);
      addChild(triangleButton);

      triangleButton.x = 600;
      triangleButton.y = 200;

      triangleButton.addEventListener(MouseEvent.CLICK, slideGames);
    }



    public function slideGames(event:MouseEvent):void {
      loadNextImage();
    }

    public function loadNextImage() : void {
      // Increment the image
      _imageIndex++;

      // If we've reached the end of the array, start over
      if (_imageIndex >= images.length) {
        _imageIndex = 0;
      }

      // Now get the image source from the array and tell the loader to load it
      var imageSource : String = images[_imageIndex] as String;
      loader.load(new URLRequest(imageSource));
    }
    // Next image to display
    protected var _imageIndex : int = -1;
  }
}

基本思想是保持指向要显示的下一个图像的整数指针。显示下一个图像时,递增此指针。如果超过图像源阵列的长度,请将其重置为0.

此指针从-1开始,因此第一次递增时它的值为0.

你应该能够轻松处理上一个案例 - 只需减少指针而不是检查它是否大于数组长度,检查它是否小于零并将其设置为images.length-1。 / p>