PDA

View Full Version : Array Question... i=0 or i=1



tomyknoker
03-15-2007, 03:27 PM
Hi all,

I have an array the code looks like this


i = 1;
frames = new Array(38, 40, 42, 44, 46, 48);
this.onEnterFrame = function() {
if (this._currentframe == frames[i]) {
this["btn" + i].index = i+1;
this["btn" + i].onRelease = function () {
variable = this.index;
this.play();
}
this["btn" + i].index = i;
this["btn" + i].onRelease = function() {
variable = this.index;
gotoAndPlay(51);
};
this["btn" + i].onRollOver = function() {
delete this.onEnterFrame;
this.play();
};
this["btn" + i].onRollOut = function() {
this.onEnterFrame = function() {
if (this._currentframe == 1) {
delete this.onEnterFrame;
} else {
this.gotoAndStop(this._currentframe - 1);
}
};
};
i++;
}
if (this._currentframe == frames[frames.length]) {
delete this.onEnterFrame;
}
};

And this is what it calls when it gets to the frame



if (variable == 1) {
gotoAndStop("btn1");
} else if (variable == 2) {
gotoAndStop("btn2");
} else if (variable == 3) {
gotoAndStop("btn3");
} else if (variable == 4) {
gotoAndStop("btn4");
} else if (variable == 5) {
gotoAndStop("btn5");
} else if (variable == 6) {
gotoAndStop("btn6");
}


The problem is if I make 'i=o' the btn1 doesn't work... If I make 'i=1' btn6 doesn't work... I'm racking my brain but can't work this one out!

BLiZZaRD
03-15-2007, 07:17 PM
remove your i =1 up top there.

Replace the code section to add the following:



this.onEnterFrame = function() {
for (var i:Number=0; i<6; ++i){
if (this._currentframe == frames[i]) {
this["btn" + i].index = i+1;
this["btn" + i].onRelease = function () {
variable = this.index;
this.play();


Then add your extra "}" at the end :)

Remember like most other languages counting starts at 0, not 1 so when you have 6 buttons, your highest number = 5.

Hope it helps :)