• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Looking for a way to make this slimmer - AS3

New Here ,
Jul 10, 2018 Jul 10, 2018

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!

TOPICS
ActionScript

Views

248

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Explorer , Jul 10, 2018 Jul 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 re

...

Votes

Translate

Translate
Explorer ,
Jul 10, 2018 Jul 10, 2018

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 11, 2018 Jul 11, 2018

Copy link to clipboard

Copied

LATEST

This worked like a charm!  Thanks!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines