Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I properly extend the AS3 Point class?

I come to need a bit more info from my points, so I thought I'd add a previousX, previousY so that I can get a deltaX and deltaY. Now, this works fine if I make a simple self-contained class.

I think, however, that I'd like to extend the flash.geom.Point class to benefit from the other functions and calculations it offers.

So I extended Point and modified the x and y setters as I needed. When I compile, I get an error that these setters are not marked for override when they should be. So I override, but then I get an error that says these do not override any function.

Any clue where I screwed up?

Here is the class:

package net.jansensan.geom
{
    import flash.geom.Point;

    /**
     * @author Mat Janson Blanchet
     */
    public class UpdatablePoint extends Point
    {
        private var _previousX : Number = 0;
        private var _previousY : Number = 0;
        private var _deltaX    : Number = 0;
        private var _deltaY    : Number = 0;

        // ********************************************************************
        //                         [ PUBLIC METHODS ]
        // ********************************************************************

        public function UpdatablePoint(x:Number=0, y:Number=0)
        {
            super(x, y);
        }

        override public function toString():String
        {
            return "(x=" + super.x + ", y=" + super.y + ", previousX=" +
                _previousX + ", previousY=" + _previousY + ", deltaX=" +
                _deltaX + ", deltaY=" + _deltaY + ")";
        }

        // ********************************************************************
        //                        [ GETTERS / SETTERS ]
        // ********************************************************************

        override public function set x(x:Number):void
        {
            _previousX = super.x;
            super.x = x;
            _deltaX = super.x - _previousX;
        }

        override public function set y(y:Number):void
        {
            _previousY = super.y;
            super.y = y;
            _deltaY = super.y - _previousY;
        }

        public function get previousX():Number
        {
            return _previousX;
        }

        public function get previousY():Number
        {
            return _previousY;
        }

        public function get deltaX():Number
        {
            return _deltaX;
        }

        public function get deltaY():Number
        {
            return _deltaY;
        }
    }
}
like image 317
jansensan Avatar asked Jan 25 '12 22:01

jansensan


2 Answers

The Point class isn't implemented using get/set-methods, rather it contains only public variables: public var x : Number and public var y : Number. See the documentation here.

Since you cannot override any public getters or setters you can't detect when anyone using your class is writing to these variables. What you can do is remove the extends Point and add a Point instance variable to your UpdatablePoint. Then change your getters and setters for x/y from super.[x/y] to myPointInstance.[x/y].

If you need to expose more functionality from the Point class, you could easily just wrap it. I.e. Say you want to use the 'equals'-method from Point, just create a copy of that method signature and make it's body contain: return myPointInstance.equals(p);

like image 145
Bakapii Avatar answered Oct 31 '22 20:10

Bakapii


Point does not have public function set x or public function set y.

So you can't override them as they don't exist. Point is very low-level, my guess would be that Adobe didn't add these functions to avoid the increase in overload.

like image 38
sean Avatar answered Oct 31 '22 18:10

sean