简体   繁体   中英

Minusassign (-=) syntax error in ActionScript-3

Why can't I compile this little piece of code ?
I'm overriding x and y in my class so that's why I need super.x .

public class SimpleSprite extends Sprite
{
    override public function set x(value: Number): void
    {
        super.x -= 12;
        //super.x = super.x - 12;
    }
}

// or
public class SimpleSprite2 extends Sprite
{
    public function get xx(): Number
    {
        return super.x;
    }

    public function set xx(value: Number): void
    {
        super.x = value;
    }

    public function SimpleSprite2()
    {
        xx -= 12;
        super.x -= 12;
        // Error: Syntax error: expecting semicolon before minusassign.
    }
}

I know I can write super.x = super.x - 12; but im lazy and i dont want 2 look 4 these inconsistencies when i get hit by syntax errors im also very much accustomed to those shortcuts with -=


Looks like my IDE (FlashDevelop 4.2.3) bears the blame for this.

There's nothing syntactically wrong with what your doing. Your code works fine in my Flash Builder 4.7.

However, remember that instance.x -= value; is short for

instance.x = instance.x - value; 
//ie. 
instance.[set]x = instance.[get]x - value;

I mention this because of your top code:

override public function set x(value: Number): void
{
    super.x -= 12;
    //super.x = super.x - 12;
}

Makes me think you're trying to do something funny. Why are you decrementing in the set function?

If you need to have a seperate x value for a subclass, but can't change the interface (ie. you still want users to be able to say mySubClassInstance.x = 123.0; ) do this:

public class DerivedSprite extends Sprite
{
    protected var derivedX:Number;
    public override function get x():Number
    {
        super.x = derivedX;
        return derivedX;
    }

    public override function set x(value:Number):void
    {
        super.x = value;
        derivedX = value;
    }

    public function DerivedSprite()
    {
        super();
        derivedX = super.x;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM