1 decade ago by Eddie
Okay, so we're building a game where the character's stats are based on an array of information we're calling from an external site (for the purposes of what I'm doing, the external site is unnecessary).
So we have this object in Javascript:
var stats = {"nike_runs":[{"start_time":"2011-03-11T19:14:44Z","calories":12.0,"distance_miles":"0.10","total_seconds":288.0,"average_pace":"50.47"},
{"start_time":"2011-03-11T19:41:25Z","calories":7.0,"distance_miles":"0.06","total_seconds":559.0,"average_pace":"165.19"},
{"start_time":"2011-03-11T20:27:45Z","calories":197.0,"distance_miles":"1.63","total_seconds":8434.0,"average_pace":"86.22"}]}
And this piece of code that should retrieve the calories from each array item and add them up:
function getExp (somestats) {
var exp = 0;
for (var i = 0, l = somestats.nike_runs.length; i < l; i++) {
exp += somestats.nike_runs[i].calories;
}
return exp;
}
My problems so far are the following:
1) Clearly Impact uses a different syntax so me adding this code into it isn't working due to syntax (I think).
2) Ultimately I'd like to have that function in a separate file altogether instead of inside my entity file, so is there a way my entity can call a function from an external JS file?
Thanks guys! :D
So we have this object in Javascript:
var stats = {"nike_runs":[{"start_time":"2011-03-11T19:14:44Z","calories":12.0,"distance_miles":"0.10","total_seconds":288.0,"average_pace":"50.47"},
{"start_time":"2011-03-11T19:41:25Z","calories":7.0,"distance_miles":"0.06","total_seconds":559.0,"average_pace":"165.19"},
{"start_time":"2011-03-11T20:27:45Z","calories":197.0,"distance_miles":"1.63","total_seconds":8434.0,"average_pace":"86.22"}]}
And this piece of code that should retrieve the calories from each array item and add them up:
function getExp (somestats) {
var exp = 0;
for (var i = 0, l = somestats.nike_runs.length; i < l; i++) {
exp += somestats.nike_runs[i].calories;
}
return exp;
}
My problems so far are the following:
1) Clearly Impact uses a different syntax so me adding this code into it isn't working due to syntax (I think).
2) Ultimately I'd like to have that function in a separate file altogether instead of inside my entity file, so is there a way my entity can call a function from an external JS file?
Thanks guys! :D