1

File: "point_direction.as"


    package {   
        public class point_direction {

            var radianAngle:Number;
            var degreeAngle:Number;

            public function point_direction(x1:Number, y1:Number, x2:Number, y2:Number) : Number {
                radianAngle = Math.atan2(y1 - y2, x1 - x2);
                degreeAngle = radianAngle * 180 / Math.PI();

                return degreeAngle;
            }
        }
    }

Above is the file I'm having trouble fixing. I would like the file to return the final calculated angle in degrees when its done, but it throws this error.

Line 7, 1130: A constructor cannot specify a return type.
Joban
  • 11
  • 1

3 Answers3

0

You should be careful here. A class constructor method shouldn't return a value as it is actually returning itself. This explains why you got that error message.

So choose a different function name see the following example

Public function calculate_point_dirction(x1:Number, ...):Number
{
 // do staff
 Return degreeAngle;
 }
Future2020
  • 9,939
  • 1
  • 37
  • 51
0

Your error is as expected. A Constructor is the creation method of a class so it returns the instance of that class - adding any kind of return type besides void will throw that error.

Looks like you may be wanting to make this a static function? if so here is what your package would look like (assuming all it does is this function):

package {   
    public class point_direction {

        public static function pointDirection(x1:Number, y1:Number, x2:Number, y2:Number):Number {
            var radianAngle = Math.atan2(y1 - y2, x1 - x2);
            var degreeAngle = radianAngle * 180 / Math.PI();

            return degreeAngle;
        }
    }
}

you'd access this by calling point_direction.pointDirection(x1,y1,x2,y2)

BadFeelingAboutThis
  • 14,445
  • 2
  • 33
  • 40
0

If you want an instance of the class point_direction to represent degreeAngle, you can do this:

package
{
    public class point_direction
    {

        private var _radianAngle:Number;
        private var _degreeAngle:Number;


        public function point_direction(x1:Number, y1:Number, x2:Number, y2:Number)
        {
            _radianAngle = Math.atan2(y1 - y2, x1 - x2);
            _degreeAngle = radianAngle * 180 / Math.PI();
        }


        public function get degreeAngle():Number
        {
            return _degreeAngle;
        }
    }
}

Notice that the constructor doesn't return anything (because constructor's can't do that).

You'll instead do this:

var dir:point_direction = new point_direction(0, 1, 2, 3);
trace(dir.degreeAngle);
Marty
  • 39,033
  • 19
  • 93
  • 162