Skip to main content
Participant
July 10, 2018
Answered

Looking for a way to make this slimmer - AS3

  • July 10, 2018
  • 1 reply
  • 339 views

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!

This topic has been closed for replies.
Correct answer DOM

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']);
  }
}
}

1 reply

DOMCorrect answer
Participating Frequently
July 10, 2018

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']);
  }
}
}

Participant
July 11, 2018

This worked like a charm!  Thanks!