简体   繁体   English

ActionScript-3中的Minusassign(-=)语法错误

[英]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 . 我在课堂上重写x和y,所以这就是为什么我需要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; 我知道我可以写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 -= 但是我很懒,当我被语法错误击中时,我不希望2看起来4这些不一致之处,我也非常习惯使用-=那些快捷方式


Looks like my IDE (FlashDevelop 4.2.3) bears the blame for this. 看来我的IDE(FlashDevelop 4.2.3)对此负有责任。

There's nothing syntactically wrong with what your doing. 您所做的一切在语法上都没有错。 Your code works fine in my Flash Builder 4.7. 您的代码可以在我的Flash Builder 4.7中正常工作。

However, remember that instance.x -= value; 但是,请记住该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: 如果您需要为子类使用单独的x值,但不能更改接口(即,您仍然希望用户能够说出mySubClassInstance.x = 123.0; ),请执行以下操作:

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;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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