Copy link to clipboard
Copied
Hi all. First time poster and novice AS3 / Animate CC user.
I'm pulling data from a JSON file and passing it to text objects on my canvas.
I ran into a problem where some of the values on the JSON file were "null" and was getting a "TypeError: Error #2007: Parameter text must be non-null".
I'm working around this by creating and If statement for each text object and replacing any "null" with "0" string. I'm doing this for each text object and it feels clunky.
The questions: is there a better way to do this?
Here is the function:
//(remainingUserCount_# are the text objects on the canvas)
function processJson(e:Event):void
{
var jsonObject:Object = JSON.decode (e.target.data);
var NullString:String = "0"
//Remaining user count question 1
if (jsonObject.game['questions'][0]['remainingUsersCount'] == null)
{
remainingUsersCount_1.text = NullString
}
else
{
remainingUsersCount_1.text = String(jsonObject.game['questions'][0]['remainingUsersCount'])
}
//Remaining user count question 2
if (jsonObject.game['questions'][1]['remainingUsersCount'] == null)
{
remainingUsersCount_2.text = NullString
}
else
{
remainingUsersCount_2.text = String(jsonObject.game['questions'][1]['remainingUsersCount'])
}
//Remaining user count question 3
if (jsonObject.game['questions'][2]['remainingUsersCount'] == null)
{
remainingUsersCount_3.text = NullString
}
else
{
remainingUsersCount_3.text = String(jsonObject.game['questions'][2]['remainingUsersCount'])
}
}
Thanks!
You can use a for loop to shorten your script a bit. A for loop will allow you to execute a block of code a number of times. In this example, you want to check all your 'remainingUsersCount' objects for its value. Therefore, you will also need to create an array that stores references to these objects. Below is one way that you can achieve this:
...function processJson(e:Event):void
{
var jsonObject:Object = JSON.decode (e.target.data);var NullString:String = "0";
//Create an array that stores a re
Copy link to clipboard
Copied
You can use a for loop to shorten your script a bit. A for loop will allow you to execute a block of code a number of times. In this example, you want to check all your 'remainingUsersCount' objects for its value. Therefore, you will also need to create an array that stores references to these objects. Below is one way that you can achieve this:
function processJson(e:Event):void
{
var jsonObject:Object = JSON.decode (e.target.data);var NullString:String = "0";
//Create an array that stores a reference to the "remainingUsersCount" objects
var remainingUsersArr = [remainingUsersCount_1, remainingUsersCount_2,remainingUsersCount_3];
//Iterate through the loop; the loop is from 0-3
for(var i = 0; i < remainingUsersArr.length; i++){if (jsonObject.game['questions']['remainingUsersCount'] == null)
{
remainingUsersArr.text = NullString;
}else
{
remainingUsersArr.text = String(jsonObject.game['questions']['remainingUsersCount']);
}
}
}
Copy link to clipboard
Copied
This worked like a charm! Thanks!