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

发布于 2024-12-15 04:21:56 字数 1611 浏览 0 评论 0原文

制作一个按钮来根据图像在数组中的位置按顺序显示图像有多难?

我需要这个用于下一个上一个按钮。检查下面的数组。

数组:

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
    {

    }

    }
}}

How hard it is to make a button to display images in order, acording to their position inside an array?

I need this for the NEXT and PREVIOUS buttons. Check the array below.

Array:

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"];

Button:

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

Loader:

    private var loader:Loader = new Loader();

Full Code:

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
    {

    }

    }
}}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

↙厌世 2024-12-22 04:21:56

试试这个:

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。

Try this:

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;
  }
}

The basic idea is to keep an integer pointer to the next image you want to display. When you display the next image, increment this pointer. If it exceeds the length of the image source array, reset it to 0.

This pointer starts at -1 so that the first time it is incremented it has the value 0.

You should be able to handle the previous case easily -- just decrease the pointer and instead of checking if it is bigger than the array length, check if it is less than zero and set it to images.length-1.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文