-3

Just wondering if anyone can help? I can't seem to figure out how to get the value of

textArray[i].text

from inside this function

function tock(event) {
createjs.Ticker.setInterval(83.33333333333333);
var tickerInt = createjs.Ticker.getInterval();
var tickerTime = createjs.Ticker.getTime(true);
var realTime = Math.round(tickerTime/tickerInt);

if(counter > 0){
    realTime = realTime - counter * labelArray[labelArray.length -1].position;
}   

for (i = 0; i < labelArray.length -2; i++){
    if(labelArray[i].position){
    realTime >= labelArray[i].position;
    newStage.addChild(textArray[i].text);
    }
}

The array contains the correct data, as per my log dump:

            var textArray = new Array();

    for (i = 0; i < labelArray.length -2; i++){
    this.tField = new cjs.Text("", "18px 'Arial'");
    this.tField.textAlign = "center";
    this.tField.lineHeight = 20;
    this.tField.lineWidth = 556;
    this.tField.setTransform(313,402);
    textArray.push(this.tField);
    textArray[i].text = mydata[keysbyindex[i]];
    }
    for (i = 0; i < labelArray.length -2; i++){
    console.log(textArray[i].text)
    }

I just can't seem to access it, but I don't work too much with JS, so it's probably something really obvious. Any help very much appreciated!

Thanks!

Matt

Alpinist1974
  • 21
  • 1
  • 5
  • Where are you invoking tock? your array may contain the correct data eventually, but at the time of invocation it may still be empty. You should provide more of your code. – adrichman Apr 15 '14 at 20:28
  • `var textArray = new Array()` - ew. Use `var textArray = []`. – John Dvorak Apr 15 '14 at 20:33
  • 2
    `.setInterval(83.33333333333333)` - even hoping the browser will satisfy the request to call your function to milisecond precision is kinda optimistic. `83.333333` is nanosecond precision - you would be selecting individual clock ticks here. You've got eight extra digits of precision. There's no way the browser - or any piece of equipment - can satisfy that. – John Dvorak Apr 15 '14 at 20:37

1 Answers1

0

The short answer is that textArray is not a global variable because it has been declared with a var. If its just declared like this

textArray = [];

you would have access to it in the tock() function.

Ofcourse, I am not recommending that you use global variables. There are perhaps deeper problems with the code as it is right now.

vivekian2
  • 3,795
  • 9
  • 35
  • 41
  • @Jan Dvorak and Vivek Aseeja-- thanks I tried with textArray = [], but still no luck. I'm still getting an undefined error from easeljs. – Alpinist1974 Apr 15 '14 at 20:46
  • @adrichman -- Thanks. I tried invoking tock after the function definition, but that didn't work. I don't think I can post a fiddle because this is a Flash/CreateJS output. I'll try pastebin. – Alpinist1974 Apr 15 '14 at 20:50