Skip to main content
M123456
Participant
February 6, 2019
Answered

How to get values from JSON on every single frame

  • February 6, 2019
  • 1 reply
  • 285 views

Hi,

I am a neewby with Animate.

What I want:

A button that changes position based on y numbers pulled from a JSON file.

So for example: on frame 1 the y = 200, on frame 2 the y = 202, on frame 3 the y = 206, etc.

I know how to do this on just one frame:

// load JSON file  

    var req = new XMLHttpRequest();  

    req.open("GET", "test.js", true);  

    req.addEventListener("load", transferComplete.bind(this));   

    req.send();

//outputJSONfile

    function transferComplete(e){

    var JSONObject = JSON.parse(req.responseText);

    console.log(JSONObject["y"]);

   

    var verticaal = JSONObject["y"];

    this.btn.y = verticaal;

    }

And my JSON file:

{

"y" : 200

}

But how do I do the same for a JSON file with lets say a 100 different numbers and a 100 frames.

Hopefully someone can help me out!

This topic has been closed for replies.
Correct answer JoãoCésar17023019

Hi.

When you store a value in Animate CC using the var keyword, that variable is available to that frame only. If you want to keep your loaded and parsed JSON data available for the entire timeline, set this JSON data as a property of the main timeline.

Something like this:

function transferComplete(e)

{

    exportRoot.json = JSON.parse(req.responseText);  // exportRoot is a global reference to the main timeline created by Animate CC

}

Then, from anywhere, you can just say:

this.mc.y = exportRoot.json["y"];

I hope this helps.

Regards,

JC

1 reply

JoãoCésar17023019
Community Expert
JoãoCésar17023019Community ExpertCorrect answer
Community Expert
February 6, 2019

Hi.

When you store a value in Animate CC using the var keyword, that variable is available to that frame only. If you want to keep your loaded and parsed JSON data available for the entire timeline, set this JSON data as a property of the main timeline.

Something like this:

function transferComplete(e)

{

    exportRoot.json = JSON.parse(req.responseText);  // exportRoot is a global reference to the main timeline created by Animate CC

}

Then, from anywhere, you can just say:

this.mc.y = exportRoot.json["y"];

I hope this helps.

Regards,

JC