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

Trying to complete a simple class example

Explorer ,
Jan 10, 2014 Jan 10, 2014

Hi, in an effort to better understand AS3 classes I have set up a little example for myself which consists of a Flash file containing three text input boxes and a button.

The idea is that the user enters numbers in the first two text boxes then clicks the "Calc" button. The program sends the numeric data for processing and returns the sum of the two numbers appearing in the third text box. At the stage that I'm at I keep getting an error message which states: "Implicit coercion of a value of type Number to an unrelated type Function." Can someone explain to me where I've gone wrong? Here is my code.

Code from Calc.fla

//Creates an object from the class called mySample

var externalFile:mySample = new mySample();

//allows the program to read the text box as a number

var num1 = Number(txtBox1.text)

var num2 = Number(txtBox2.text)

//Button event listener

btnCalc.addEventListener(MouseEvent.CLICK,externalFile.addNumbers(num1, num2));

Code from mySample.as

package  {

 

          public class mySample {

                    //set a few number variables

                    var num1:Number = 0;

                    var num2:Number = 0;

                    var answer:Number = 0;

 

          //create a function

          public function addNumbers(num1, num2):Number {

                    answer = num1 + num2;

                    return answer; //this function returns something that can be used elsewhere

             }

       }

 

}

TOPICS
ActionScript
678
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 ,
Jan 10, 2014 Jan 10, 2014

You are using even listener wrong. Event handler is just reference to function - not function invocation. Also, event handler accepts only one parameter - Event (or whatever extends Event Class). Thus, you cannot possibly pass any other parameters into Event handler.

One for the options for your case can be:

//Creates an object from the class called mySample

var externalFile:mySample = new mySample();

//allows the program to read the text box as a number

var num1 = Number(txtBox1.text)

var num2 = Number(txtBox2.text)

//Button event listener

btnCalc.addEventListener(MouseEvent.CLICK, clickHandler);

function clickHandler(e:MouseEvent):void {

          resultTextBox.text = String(externalFile.addNumbers(num1 , num2));

}

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
Explorer ,
Jan 11, 2014 Jan 11, 2014

Andrei,

Thank you for the response. Your suggestion made total sense to me and I tried it with the code that you see below and the error message no longer appears. Would you entertain one additional question? When I run the movie in Flash and input a few numbers in the first and second text boxes and click the calc button, the result comes back zero. Do you know why this is happening? Thank you.

Calc.fla

//Creates an object from the class called mySample

var externalFile:mySample = new mySample();

//allows the program to read the text box as a number

var num1 = Number(txtBox1.text)

var num2 = Number(txtBox2.text)

//Button event listener

btnCalc.addEventListener(MouseEvent.CLICK, clickHandler);

function clickHandler(e:MouseEvent):void {

          resultTextBox.text = String(externalFile.addNumbers(num1 , num2));

}

mySample.as

package  {

 

          public class mySample {

                    //set a few number variables

                    var num1:Number;

                    var num2:Number;

                    var answer:Number;

 

          //create a function

          public function addNumbers(num1, num2):Number {

                    answer = num1 + num2;

                    return answer; //this function returns something that can be used elsewhere

                    }

}

}

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 ,
Jan 11, 2014 Jan 11, 2014
LATEST

1. I guess you need to change the code to the following (read the comments):

// Creates an object from the class called mySample

var externalFile:mySample = new mySample();

// allows the program to read the text box as a number

/**

* these two lines DO NOT read text field values when they change

* so these two vars are useless unless you want to retain original values

*/

var num1 = Number(txtBox1.text)

var num2 = Number(txtBox2.text)

//Button event listener

btnCalc.addEventListener(MouseEvent.CLICK, clickHandler);

function clickHandler(e:MouseEvent):void

{

          // read textfields' values every time user clicks the button

          resultTextBox.text = String(externalFile.addNumbers(Number(txtBox1.text), Number(txtBox2.text)));

}

2. You need to get into a habit to strictly type you variables and objects.

3. You should get into a habit to declare variable namespace (private, public, etc.) of the variables. SO you class variables declarations should be:

private var num1:Number;

private var num2:Number;

private var answer:Number;

4. It is wise to name classes starting with upper case - it is a convention that makes AS programmer life easier.

5. Since your class has no other task but calculate and return new value - declaring additional calss level variables (num1, num2, and answer) introduces an unnecessary overhead. These variables existence would be justified only if they were reused.

I suggest your class should be like that (note that addNumber method's parameters are typed to Number):

package

{

          public class MySample

          {

                    //create a function

                    public function addNumbers(num1:Number, num2:Number):Number

                    {

                              return num1 + num2; //this function returns something that can be used elsewhere

                    }

          }

}

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