代码之家  ›  专栏  ›  技术社区  ›  anon255058

带实例的数组循环,as3

  •  0
  • anon255058  · 技术社区  · 16 年前

    有三个盒子我正在用计时器进行索引。它们依次消失。我如何使它们重现?
    谢谢

    方框按顺序消失1-3

    var pink:Array = ["","boxInstance1","boxInstance2","boxInstance3"];
    var timer:Timer = new Timer(555);
    timer.addEventListener(TimerEvent.TIMER, onTimer);
    timer.start();
    function onTimer(evt:TimerEvent):void {
    var counting:*;
    counting = String(timer.currentCount %10);
    trace(counting);
    //TIMER LOOPS THROUGH MY ARRAY
    this[pink[counting]].visible = false;
    }
    

    我试过这个 “它不起作用”

    //THIS IS OK
    if(counting> 0){
    this[pink[counting]].visible = false;
    }
    //'null object ref #1010'
    if(counting> 6){
    this[pink[counting]].visible = true;
    }
    

    我并不是特别关注它们消失和出现的顺序,但它需要保持一个循环。

    3 回复  |  直到 16 年前
        1
  •  0
  •   deanWombourne    16 年前

    您将计数设置为介于0和5之间(timer.currentcount%6)

    那么你只需要做你的visible=true,如果数的话>6。

    计数永远不会是6:)

    尝试检查它是否>=3:)

    类似这样的东西应该可以工作(nb没有测试,是从内存写的)

    var pink:Array = ["boxInstance1","boxInstance2","boxInstance3"];
    var timer:Timer = new Timer(555);
    timer.addEventListener(TimerEvent.TIMER, onTimer);
    timer.start();
    
    function onTimer(evt:TimerEvent):void {
      var counting:uint = timer.currentCount % this[pink].length;
      this[pink[counting]].visible = !this[pink[counting]].visible;
    }
    
        2
  •  1
  •   Dogmatixed    16 年前

    我对你的帖子有点困惑。首先,为什么要计算一个字符串?其次,如果计数设置为数字模式6,它什么时候会大于6?此外,你的阵列中只有四件东西,所以你不想使用6型,对吗?另外,如果您尝试访问此[“”],这是否会给您带来某种错误?

    为了让这些循环,我只需要在每次有东西出现时切换可见性,所以,而不是

    this[pink[counting]].visible = false;
    

    我会用:

    this[pink[counting]].visible = this[pink[counting]].visible ? false : true;
    

    或者,写出:

    if(this[pink[counting]].visible)
        this[pink[counting]].visible = false;
    else
        this[pink[counting]].visible = true;
    

    我认为,这是一个非常标准的方法来切换任何需要切换的东西。

        3
  •  0
  •   timrwood    16 年前

    您的目标是数组中第七个以上的项。由于数组只有4个项(包括第一个空白项),所以没有项目过去的粉红色[ 3 ]。另外,如果counting=timer.currentcount%6,它将介于0和5之间,因此永远不会超过6。

    另外,计数应该是int,而不是字符串。如果使用它来确定大于和小于,并为数组编制索引,那么它应该是一个int。

    // these variables should have been created somewhere else in your code, 
    // or on the stage if you are coding in the timeline
    var boxInstance1:Sprite = new Sprite();
    var boxInstance2:Sprite = new Sprite(); 
    var boxInstance3:Sprite = new Sprite(); 
    // create an array of the boxInstance display objects, not an array of strings.
    // note: remove the first array item, 
    // unless you need it to be blank for some other reason.
    var pink:Array = [boxInstance1, boxInstance2, boxInstance3];
    var timer:Timer = new Timer(555);
    timer.addEventListener(TimerEvent.TIMER, onTimer);
    timer.start();
    //TIMER FUNCTION
    function onTimer(evt:TimerEvent):void {
        var counting:int = timer.currentCount % 6;
        trace(counting); // should output 0 through 5
        if (counting > 2) {
            // if greater than 2, hide an item (3,4,5)
            pink[counting - 3].visible = false;
        } else {
            // else counting is equal or less than 2, show an item (0,1,2)
            pink[counting].visible = true;
        }
    }
    
    推荐文章