Actionscript错误1083:else是意外的

时间:2017-06-08 17:03:11

标签: actionscript

我正在尝试为Android做一个着色应用游戏,我得到错误1083.这是我的代码:

function colorpart1(e:MouseEvent):void
{
    if (txt_color.text=="sky blue")
    {
    var myColor1 = new ColorTransform();
    myColor1.color = 0xFF7BF0;
    mc_object.mc_p1.transform.colorTransform = myColor1
    }
    else (txt_color.text=="purple")
    {
    var myColor3 = new ColorTransform();
    myColor3.color = a148ff;
    mc_object.mc_p1.transform.colorTransform = myColor3
    }
    else (txt_color.text=="magenta")
    {
    var myColor4 = new ColorTransform();
    myColor4.color = c34796;
    mc_object.mc_p1.transform.colorTransform = myColor4;
    }
    else (txt_color.text=="red")
    {
    var myColor5 = new ColorTransform();
    myColor5.color = e72534;
    mc_object.mc_p1.transform.colorTransform = myColor5;
    }
    else if(txt_color.text=="orange")
    {
    var myColor6 = new ColorTransform();
    myColor6.color = f17d3b;
    mc_object.mc_p1.transform.colorTransform = myColor6;
    }
}

我不知道为什么这个特定的代码/行有其他错误(类似的代码没有):

else (txt_color.text=="magenta")
    {
    var myColor4 = new ColorTransform();
    myColor4.color = c34796;
    mc_object.mc_p1.transform.colorTransform = myColor4;
    }
    else (txt_color.text=="red")
    {
    var myColor5 = new ColorTransform();
    myColor5.color = e72534;
    mc_object.mc_p1.transform.colorTransform = myColor5;
    }

2 个答案:

答案 0 :(得分:1)

您无法在else中使用多个if...else..。如果您想添加更多条件,则应在else if之后使用if而不是else之后。{:p>

function colorpart1(e: MouseEvent): void {
    if (txt_color.text == "sky blue") {
        var myColor1 = new ColorTransform();
        myColor1.color = 0xFF7BF0;
        mc_object.mc_p1.transform.colorTransform = myColor1
    } else if (txt_color.text == "purple") {
        var myColor3 = new ColorTransform();
        myColor3.color = a148ff;
        mc_object.mc_p1.transform.colorTransform = myColor3
    } else if (txt_color.text == "magenta") {
        var myColor4 = new ColorTransform();
        myColor4.color = c34796;
        mc_object.mc_p1.transform.colorTransform = myColor4;
    } else if (txt_color.text == "red") {
        var myColor5 = new ColorTransform();
        myColor5.color = e72534;
        mc_object.mc_p1.transform.colorTransform = myColor5;
    } else if (txt_color.text == "orange") {
        var myColor6 = new ColorTransform();
        myColor6.color = f17d3b;
        mc_object.mc_p1.transform.colorTransform = myColor6;
    }
}

答案 1 :(得分:1)

您多次使用else案例。它无效。您可以在每种情况下定义var myColor。您可以减少一次定义。在下面的代码中,我使用toLowerCase()方法,因为如果txt_color.text返回CamelCase或UPPERCASE,则这些条件都不会满足。

   function colorpart1(e:MouseEvent):void
    {
        var myColor;
        if (txt_color.text.toLowerCase() == "sky blue")
        {
            myColor = new ColorTransform();
            myColor.color = 0xFF7BF0;
            mc_object.mc_p1.transform.colorTransform = myColor;
        }
        else if (txt_color.text.toLowerCase() == "purple")
        {
            myColor = new ColorTransform();
            myColor.color = 0xa148ff;
            mc_object.mc_p1.transform.colorTransform = myColor
        }
        else if (txt_color.text.toLowerCase() == "magenta")
        {
            myColor = new ColorTransform();
            myColor.color = 0xc34796;
            mc_object.mc_p1.transform.colorTransform = myColor;
        }
        else if (txt_color.text.toLowerCase() == "red")
        {
            myColor = new ColorTransform();
            myColor.color = 0xe72534;
            mc_object.mc_p1.transform.colorTransform = myColor;
        }
        else if (txt_color.text.toLowerCase() == "orange")
        {
            myColor = new ColorTransform();
            myColor.color = 0xf17d3b;
            mc_object.mc_p1.transform.colorTransform = myColor;
        }
    }