Skip to main content
kas8
Known Participant
March 28, 2011
Answered

Get data from array in Number format

  • March 28, 2011
  • 1 reply
  • 611 views

Ive got this array with numbers in it, which i want to use for some simple calculations, but i keep getting the message that the array data isnt a number:

The whole is in a for loop, with teller1 as increasing value.

the code:

var xco:Array = [180,233,271,292,292,270,231,179,124,74,34,11,9,30,70,121];
var yco:Array = [11,33,73,124,181,232,270,290,291,271,234,183,125,72,33,11];

for(var teller1=1; teller1<33; teller1++) {
    var ringsegment_mc:Ring_1=new Ring_1();
    ringsegment_mc.x=xco[teller1]+kernplaats;
    ringsegment_mc.y=yco[teller1]+kernplaats;
    ringsegment_mc.addEventListener(Event.ENTER_FRAME,vernietig);
    addChild(ringsegment_mc);
    switch(teller1) {
     case 16:
     //kernplaats=Math.random()*600+300;
     kernplaats=Number(400);
     trace (yco[teller1]);
     trace (yco[teller1]+kernplaats);
    }
}

so the trace functions give:

undefined

NaN

so i tried using this:

trace (yco[3]);
trace (yco[3]+kernplaats);

which gave this:

124

524

so something is going wrong when trying to read the array, but what? and how can it be fixed/

This topic has been closed for replies.
Correct answer Ned Murphy

I don't know what you are after, but if you want to use the same loop twice, then put it inside another loop... (note that arrays start at index 0, not 1)

for(var i=0; i<2; i++){

  for(var teller1=0; teller1<16; teller1++) {
    var ringsegment_mc:Ring_1=new Ring_1();
    ringsegment_mc.x=xco[teller1]+kernplaats;
    ringsegment_mc.y=yco[teller1]+kernplaats;
    ringsegment_mc.addEventListener(Event.ENTER_FRAME,vernietig);
    addChild(ringsegment_mc);
    switch(teller1) {
     case 16:
     //kernplaats=Math.random()*600+300;
     kernplaats=Number(400);
     trace (yco[teller1]);
     trace (yco[teller1]+kernplaats);
    }
  }

}

1 reply

Ned Murphy
Legend
March 28, 2011

Due to the size of your arrays (only 16 elements), any value of teller1 > 15 is going to result in an undefined result.  And since you go up to 32 in your loop, you should expect quite a few.

kas8
kas8Author
Known Participant
March 28, 2011

Wow, that really is a stupid mistake!

How would you suggest I still use this loop, but use the same array, twice?

Ned Murphy
Ned MurphyCorrect answer
Legend
March 28, 2011

I don't know what you are after, but if you want to use the same loop twice, then put it inside another loop... (note that arrays start at index 0, not 1)

for(var i=0; i<2; i++){

  for(var teller1=0; teller1<16; teller1++) {
    var ringsegment_mc:Ring_1=new Ring_1();
    ringsegment_mc.x=xco[teller1]+kernplaats;
    ringsegment_mc.y=yco[teller1]+kernplaats;
    ringsegment_mc.addEventListener(Event.ENTER_FRAME,vernietig);
    addChild(ringsegment_mc);
    switch(teller1) {
     case 16:
     //kernplaats=Math.random()*600+300;
     kernplaats=Number(400);
     trace (yco[teller1]);
     trace (yco[teller1]+kernplaats);
    }
  }

}