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

ActionScript 3.0 errors

New Here ,
Jul 14, 2017 Jul 14, 2017

Hello, I am new to ActionScript 3.0 and I do not understand the problem with my code. Can someone please correct it for me? Here are the errors: Expecting identifier before assign, Expecting rightparen before leftbrace, and Expecting rightparen before BMI.

// This line makes the button, btnDetermine wait for a mouse click

// When the button is clicked, the determineBMI function is called

btnDetermine.addEventListener(MouseEvent.CLICK, determineBMI);

// These lines make the textinput and the radioButtons wait for a mouse click

// When these components are clicked, the clearLabels function is called

RadioMetric.addEventListener(MouseEvent.CLICK, clearLabels);

RadioImperial.addEventListener(MouseEvent.CLICK, clearLabels);

// This line makes the textinput, txtinWeight and txtinHeight

(txtinWeight.addEventListener);

(txtinHeight.addEventListener);

// This is the determineBMI function

// e:MouseEvent is the click event experienced by the button

// void indicates that the function does not return a value

function determineBMI(e:MouseEvent):void

{

// declare the variables

var Weight:Number;

var Height:Number;

var BMI:Number;

var GMetric:String;

var GImperial:String;

// get the number from the user

Weight = Number(txtinWeight.text);

Height = Number(txtinHeight.text);

BMI = ( Weight / Height * Height || Weight * 703 / Height * Height )

// MetricGroup.group determines the group that GMetric is in

// ImperialGroup.group determines the group that GImperial is in

// .selectedData gets the value of the RadioButton that is selected from the group

GMetric = String(MetricGroup.group.selectedData);

GImperial = String(ImperialGroup.group.selectedData);

// determine the BMI

if (RadioMetric.Checked == true && BMI < 15 || BMI < 15 && RadioImperial.Checked == true)

{

lblStatement.text = "STARVATION: Consider gaining weight by eating healthy foods and building your muscles by strength training.";

}

else if (RadioMetric.Checked == true && BMI == 15 && =< 18.5 || RadioImperial.Checked == true && BMI == 15 && =< 18.5)

{

lblStatement.text = "UNDERWEIGHT: Consider eating nutrient-rich foods more frequently.";

}

else if (RadioMetric.Checked == true && BMI == 18.5 && =< 25 || RadioImperial.Checked == true && BMI == 18.5 && =< 25)

{

lblStatement.text = "IDEAL: Consider doing your habits on a regular basis to maintain your ideal body weight.";

}

else if (RadioMetric.Checked == true && BMI == 25 && =< 30 || RadioImperial.Checked == true && BMI == 25 && =< 30)

{

lblStatement.text = "OVERWEIGHT: Consider removing calories from your diet.";

}

else if (RadioMetric.Checked == true && BMI == 30 && =< 4 || RadioImperial.Checked == true BMI == 30 && =< 4)

{

lblStatement.text = "OBESE: Consider setting a goal of sow and steady weight loss and participate in safe weight-loss programs.";

}

else

{

lblStatement.text = "MORBIDLY OBESE: Consider talking to a physician or medical weight loss team for them to provide you with advice pertaining to your medical issues.";

}

}

// This is the clearLabels function

// e:MouseEvent is the click event experienced by the textinput

// void indicates that the function does not return a value

function clearLabels(e:MouseEvent):void

{

lblStatement.text = "";

lblBMI.text = "";

txtinWeight.text = "";

txtinHeight.text = "";

}

TOPICS
ActionScript
1.5K
Translate
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

LEGEND , Jul 16, 2017 Jul 16, 2017

1. Here's the reference for setting a fixed decimal number: Number - Adobe ActionScript® 3 (AS3 ) API Reference

What it's telling you is to set the BMI calculation to a variable and then apply the toFixed(2)  argument to that.

2. FIrst, make the dynamic textfield large enough to contain more than one line and, second, set the Paragraph option for the textfield to be Multiline. Now your text should flow into the textfield in multiple lines.

3. You can set the color of your text by selecting the te

...
Translate
LEGEND ,
Jul 14, 2017 Jul 14, 2017

You aren't showing us where in your code these errors are occuring. From what I see, I'll guess that the section "get the number from the user" is showing you the expecting the identifier before the assignment error. If that is true then the problem is most likely that you are defining these variable's values based on the text entered into the textfields but the textfields are not on the stage at the time that the code occurs. For instance the code above is in frame 1 and the textfield txtinHeight is on frame 4, and so the code can't find the textfield where the value is entered.

The second error about the parens is because of this line: BMI = ( Weight / Height * Height || Weight * 703 / Height * Height ) . You're asking the code to evaluate those two values, but you aren't telling code what to do with the result. You probably want something like this:

if(Weight / Height * Height ) > ( Weight * 703 / Height * Height ) {

     BMI = Weight / Height * Height;

  } else {

     BMI = Weight * 703 / Height * Height;

}

I would set variables for the result of each of these calculations so that they don't need to be done more than once.

You also have a bunch of these evaluations: else if (RadioMetric.Checked == true && BMI == 15 && =< 18.5

That part at the end: && =< 18.5 is going to cause an error, because there is nothing on the left side to compare to 18.5. Actionscript really really likes statements like this to be contained in parens: else if ((RadioMetric.Checked == true) && (BMI == 15) && ( =< 18.5 ));

Translate
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 15, 2017 Jul 15, 2017

Hi, sorry for the late reply and not mentioning where my errors were. I revised my code and it seems to work; however, when I try to press the determine button after putting numbers in each text input, the statement and BMI labels won't make the text appear? Do you know how to fix this?

//import controls

import fl.controls.RadioButtonGroup;

import flash.events.MouseEvent;

import fl.controls.RadioButton;

// This line makes the button, btnDetermine wait for a mouse click

// When the button is clicked, the determineBMI function is called

btnDetermine.addEventListener(MouseEvent.CLICK, determineBMI);

// This line makes the textinput, txtinWeight and txtinHeight

(txtinWeight.addEventListener);

(txtinHeight.addEventListener);

// This is the determineBMI function

// e:MouseEvent is the click event experienced by the button

// void indicates that the function does not return a value

function determineBMI(e:MouseEvent):void

{

// declare the variables

var Weight:Number;

var Height:Number;

var MI:RadioButtonGroup = new RadioButtonGroup("MI");

var BMI:Number;

// put the groups together

RadioMetric.group = MI;

RadioImperial.group = MI;

// get the number from the user

Weight = Number(txtinWeight.text);

Height = Number(txtinHeight.text);

// determine the BMI

if (MI.selection.label == "Metric" && (BMI == Weight / (Height * Height)) && BMI < 15)

{

lblStatement.text = "STARVATION: Consider gaining weight by eating healthy foods and building your muscles by strength training.";

}

else if (MI.selection.label == "Imperial" && (BMI == (Weight * 703) / (Height * Height)) && BMI < 15)

{

lblStatement.text = "STARVATION: Consider gaining weight by eating healthy foods and building your muscles by strength training.";

}

else if (MI.selection.label == "Metric" && (BMI == Weight / (Height * Height)) && BMI <= 18.5)

{

lblStatement.text = "UNDERWEIGHT: Consider eating nutrient-rich foods more frequently.";

}

else if (MI.selection.label == "Imperial" && (BMI == (Weight * 703) / (Height * Height)) && BMI <= 18.5)

{

lblStatement.text = "UNDERWEIGHT: Consider eating nutrient-rich foods more frequently.";

}

else if (MI.selection.label == "Metric" && (BMI == Weight / (Height * Height)) && BMI == 18.5 && BMI <= 25)

{

lblStatement.text = "IDEAL: Consider doing your habits on a regular basis to maintain your ideal body weight.";

}

else if (MI.selection.label == "Imperial" && (BMI == (Weight * 703) / (Height * Height)) && BMI == 18.5 && BMI <= 25)

{

lblStatement.text = "IDEAL: Consider doing your habits on a regular basis to maintain your ideal body weight.";

}

else if (MI.selection.label == "Metric" && (BMI == Weight / (Height * Height)) && BMI == 25 && BMI <= 30)

{

lblStatement.text = "OVERWEIGHT: Consider removing calories from your diet.";

}

else if (MI.selection.label == "Imperial" && (BMI == (Weight * 703) / (Height * Height)) && BMI == 25 && BMI <= 30)

{

lblStatement.text = "OVERWEIGHT: Consider removing calories from your diet.";

}

else if (MI.selection.label == "Metric" && (BMI == Weight / (Height * Height)) && BMI == 30 && BMI <= 40)

{

lblStatement.text = "OBESE: Consider setting a goal of sow and steady weight loss and participate in safe weight-loss programs.";

}

else if (MI.selection.label == "Imperial" && (BMI == (Weight * 703) / (Height * Height)) && BMI == 30 && BMI <= 40)

{

lblStatement.text = "OBESE: Consider setting a goal of sow and steady weight loss and participate in safe weight-loss programs.";

}

else if (MI.selection.label == "Metric" && (BMI == Weight / (Height * Height)) && BMI >= 40)

{

lblStatement.text = "MORBIDLY OBESE: Consider talking to a physician or medical weight loss team for them to provide you with advice pertaining to your medical issues.";

}

else if (MI.selection.label == "Imperial" && (BMI == (Weight * 703) / (Height * Height)) && BMI >= 40)

{

lblStatement.text = "MORBIDLY OBESE: Consider talking to a physician or medical weight loss team for them to provide you with advice pertaining to your medical issues.";

}

}

// This is the clearLabels function

// e:MouseEvent is the click event experienced by the textinput

// void indicates that the function does not return a value

function clearLabels(e:MouseEvent):void

{

lblStatement.text = "";

lblBMI.text = "";

txtinWeight.text = "";

txtinHeight.text = "";

}

Translate
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
LEGEND ,
Jul 16, 2017 Jul 16, 2017

These two lines should cause an error:

(txtinWeight.addEventListener);

(txtinHeight.addEventListener);

It looks like you want to attach event listeners to two textfields. I don't see where you are trying to use these events, so I'm guessing that you don't really need this part. You can comment those lines out or just delete them.

I don't understand the problem that you state. Are you not seeing a result when determineBMI() is run? Or are you not seeing the text in the user input fields? Are you getting any errors in the output or compiler errors windows when you run Test Movie?

You have, at least, two input textfields and one dynamic textfield. Have you formatted these fields and embedded the font or fonts that you want to use?

Translate
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 16, 2017 Jul 16, 2017

OK, the problem is that after I press the button, the statement and the BMI number wont appear. Here is a picture to clarify: http://prntscr.com/fwhdso

Translate
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
LEGEND ,
Jul 16, 2017 Jul 16, 2017

Check that your textfield has the font embedded.

Translate
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 16, 2017 Jul 16, 2017

I have done that but I still somehow can't make the Statement to appear

Translate
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
LEGEND ,
Jul 16, 2017 Jul 16, 2017

One thing that you can do to test the dynamic textfield is to enter some text into it and then test. That will assure you that the field is formatted correctly and the font is embedded.

There may be a problem with your function. You should add a bunch of trace() statements to the function to show you the values that it is using and the result of the if conditionals.

Translate
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 16, 2017 Jul 16, 2017

The buttons seem to work now. I found out that the cause of this error was because of my BMI variable. Now that the statement and BMI labels work most of the time, I just have a few questions.

1. How can I make make the BMI have two decimal places? I know you need .toFixed(2) but I do not know where to put it.

2. My statements appear to get cut off of when my test window is not enlarged. How can I make my whole statement fit in separate lines instead of the statement being in one long line?

3. How can I change the color of my BMI text label and Statement text label?

4. Sometimes my BMI and Statement labels won't appear somehow. Here are examples to visualize what I am talking about:

Working Metric: http://prntscr.com/fwiwzg

Working Imperial: http://prntscr.com/fwixk9

Nonworking Imperial: http://prntscr.com/fwixfy

Here is my revised code:

//import controls

import fl.controls.RadioButtonGroup;

import flash.events.MouseEvent;

import fl.controls.RadioButton;

// This line makes the button, btnDetermine wait for a mouse click

// When the button is clicked, the determineBMI function is called

btnDetermine.addEventListener(MouseEvent.CLICK, determineBMI);

// This is the determineBMI function

// e:MouseEvent is the click event experienced by the button

// void indicates that the function does not return a value

function determineBMI(e:MouseEvent):void

{

// declare the variables

var Weight:Number;

var Height:Number;

var MI:RadioButtonGroup = new RadioButtonGroup("MI");

// put the groups together

RadioMetric.group = MI;

RadioImperial.group = MI;

// get the number from the user

Weight = Number(txtinWeight.text);

Height = Number(txtinHeight.text);

// determine the BMI

if (MI.selection.label == "Metric" && Weight / (Height * Height) < 15)

{

lblStatement.text = "STARVATION: Consider gaining weight by eating healthy foods and building your muscles by strength training.";

lblBMI.text = String(Weight / (Height * Height));

}

else if (MI.selection.label == "Imperial" && (Weight * 703) / (Height * Height) < 15)

{

lblStatement.text = "STARVATION: Consider gaining weight by eating healthy foods and building your muscles by strength training.";

lblBMI.text = String((Weight * 703) / (Height * Height));

}

else if (MI.selection.label == "Metric" && Weight / (Height * Height) <= 18.5)

{

lblStatement.text = "UNDERWEIGHT: Consider eating nutrient-rich foods more frequently.";

lblBMI.text = String(Weight / (Height * Height));

}

else if (MI.selection.label == "Imperial" && (Weight * 703) / (Height * Height) <= 18.5)

{

lblStatement.text = "UNDERWEIGHT: Consider eating nutrient-rich foods more frequently.";

lblBMI.text = String((Weight * 703) / (Height * Height));

}

else if (MI.selection.label == "Metric" && Weight / (Height * Height) == 18.5 && Weight / (Height * Height) <= 25)

{

lblStatement.text = "IDEAL: Consider doing your habits on a regular basis to maintain your ideal body weight.";

lblBMI.text = String(Weight / (Height * Height));

}

else if (MI.selection.label == "Imperial" && (Weight * 703) / (Height * Height) == 18.5 && (Weight * 703) / (Height * Height) <= 25)

{

lblStatement.text = "IDEAL: Consider doing your habits on a regular basis to maintain your ideal body weight.";

lblBMI.text = String((Weight * 703) / (Height * Height));

}

else if (MI.selection.label == "Metric" && Weight / (Height * Height) == 25 && Weight / (Height * Height) <= 30)

{

lblStatement.text = "OVERWEIGHT: Consider removing calories from your diet.";

lblBMI.text = String(Weight / (Height * Height));

}

else if (MI.selection.label == "Imperial" && (Weight * 703) / (Height * Height) == 25 && (Weight * 703) / (Height * Height) <= 30)

{

lblStatement.text = "OVERWEIGHT: Consider removing calories from your diet.";

lblBMI.text = String((Weight * 703) / (Height * Height));

}

else if (MI.selection.label == "Metric" && Weight / (Height * Height) == 30 && Weight / (Height * Height) <= 40)

{

lblStatement.text = "OBESE: Consider setting a goal of sow and steady weight loss and participate in safe weight-loss programs.";

lblBMI.text = String(Weight / (Height * Height));

}

else if (MI.selection.label == "Imperial" && (Weight * 703) / (Height * Height) == 30 && (Weight * 703) / (Height * Height)  <= 40)

{

lblStatement.text = "OBESE: Consider setting a goal of sow and steady weight loss and participate in safe weight-loss programs.";

lblBMI.text = String((Weight * 703) / (Height * Height));

}

else if (MI.selection.label == "Metric" && Weight / (Height * Height) >= 40)

{

lblStatement.text = "MORBIDLY OBESE: Consider talking to a physician or medical weight loss team for them to provide you with advice pertaining to your medical issues.";

lblBMI.text = String(Weight / (Height * Height));

}

else if (MI.selection.label == "Imperial" && (Weight * 703) / (Height * Height) >= 40)

{

lblStatement.text = String("MORBIDLY OBESE: Consider talking to a physician or medical weight loss team for them to provide you with advice pertaining to your medical issues.");

lblBMI.text = String((Weight * 703) / (Height * Height));

}

}

// This is the clearLabels function

// e:MouseEvent is the click event experienced by the textinput

// void indicates that the function does not return a value

function clearLabels(e:MouseEvent):void

{

lblStatement.text = "";

lblBMI.text = "";

txtinWeight.text = "";

txtinHeight.text = "";

}

Translate
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
LEGEND ,
Jul 16, 2017 Jul 16, 2017

1. Here's the reference for setting a fixed decimal number: Number - Adobe ActionScript® 3 (AS3 ) API Reference

What it's telling you is to set the BMI calculation to a variable and then apply the toFixed(2)  argument to that.

2. FIrst, make the dynamic textfield large enough to contain more than one line and, second, set the Paragraph option for the textfield to be Multiline. Now your text should flow into the textfield in multiple lines.

3. You can set the color of your text by selecting the textfield and then setting the color in the Properties window, it's in the Charactors section.

4. I have no idea. You'll have to use trace() statements to track down what might be failing.

Translate
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 16, 2017 Jul 16, 2017

Finally got my code to work correctly now. Thank you Rob and Colin!

Translate
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
LEGEND ,
Jul 17, 2017 Jul 17, 2017
LATEST

You're welcome. Good luck with your project.

Translate
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