开发者

array loop with instances, as3

开发者 https://www.devze.com 2023-01-04 14:09 出处:网络
There\'s 3 boxes I\'m indexing through with a timer. They disappear in sequence. How do I make them reappear?Thanks

There's 3 boxes I'm indexing through with a timer. They disappear in sequence. How do I make them reappear?

Thanks

boxes disappear in sequence 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;
}

I tried this 'it didn't work'

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

I'm not to particular about the sequence they disappear and appear, but it need to keep going in a loop.


I'm a little bit confused by what you've posted. first, why is counting a string? second, if counting is set to a number mod 6, when will it be greater than 6? furthermore you've only got four things in your array, so you don't want to be using mod 6, right? also, if you try to access this[""], won't that give you some kind of error?

to get these to loop I would just switch the visibility each time something came around, so rather than

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

I would use:

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

or, written out:

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

which, I think, is a pretty standard way of toggling anything that needs to be toggled.


You're setting counting to be between 0 and 5 (timer.currentCount %6)

then you're only doing your visible = true if counting > 6.

Counting is never going to be > 6 :)

Try checking that it's >= 3 :)

Something like this should work (NB Not tested, written from memory)

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


You are targeting the seventh + greater items in the array. As the array only has 4 item (including the blank first one), there are no items past pink[3]. Also, if counting = timer.currentCount % 6, it will be between 0 and 5, so it will never go past 6.

Also, counting should be an int, not a string. If you are using it to determine greater than and less than and to index an array, it should be an 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;
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消