具有Actionscript3的数组

时间:2018-11-21 11:31:46

标签: actionscript-3 actionscript flash-cs6

我对Actionscript3相对较新。我正在尝试制作一个简单的计算器,该计算器从舞台上的文本框对象获取不同的值,并将它们相乘以在其他文本框中提供结果。我正在Flash CS6中尝试此操作。 Screenshot of the .FLA file。我将如何使用数组实现以下代码?

    var txtbox:TextFormat=new TextFormat();
    txtbox.size = 11;
    txtbox.bold = true;

    value1.setStyle("textFormat",txtbox);
    value2.setStyle("textFormat",txtbox);
    value3.setStyle("textFormat",txtbox);
    value4.setStyle("textFormat",txtbox);
    value5.setStyle("textFormat",txtbox);
  multiplier.setStyle("textFormat",txtbox);

    result1.setStyle("textFormat",txtbox);
    result2.setStyle("textFormat",txtbox);
    result3.setStyle("textFormat",txtbox);
    result4.setStyle("textFormat",txtbox);
    result5.setStyle("textFormat",txtbox);


 compute.addEventListener(MouseEvent.CLICK, calBtn);
    function calBtn(event:MouseEvent):void
    {
         var myArray:Array = int(value1),int(value2),int(value3),int(value4),int(value5)];

var m:Number=Number(multiplier.text);

var r1:Number = int((value1*m)*1000)/1000;
var r2:Number = int((value2*m)*1000)/1000;
var r3:Number = int((value3*m)*1000)/1000;
var r4:Number = int((value4*m)*1000)/1000;
var r5:Number = int((value5*m)*1000)/1000;

result1.text=String(r1);
result2.text=String(r2);
result3.text=String(r3);
result4.text=String(r4);
result5.text=String(r5);
     }

2 个答案:

答案 0 :(得分:0)

您收到的错误是告诉您您正在使用TextInput,就像它是Number

浏览您的代码,我们可以看到确实是这样:

int(value1)  //value1 presumably is a TextInput, and can't be converted to an int

//and later...
int((value1*m) //value1 is a TextInput, and can't be multiplied

您需要做的是先将TextInput的text属性转换为Number,然后进行数学运算。 (就像您当前正在使用乘法器文本框一样)

int(value1.text) * m

尽管您的代码还有其他一些问题,例如如何创建数组。 这是一个潜在的重构

var txtbox:TextFormat = new TextFormat();
    txtbox.size = 11;
    txtbox.bold = true;

//put all the text inputs and in an array so you don't have to repeat the same code so much
var textInputs:Array = [value1,value2,value3, value4, value5];
var textOutputs:Array = [result1, result2, result3, result4, result5];

//set the style in a for loop
for(var i:int=0;i<textInputs.length;i++){
    textInputs[i].setStyle("textFormat",txtbox);
}

multiplier.setStyle("textFormat",txtbox);

compute.addEventListener(MouseEvent.CLICK, calBtn);

function calBtn(event:MouseEvent):void
{
    var m:Number=Number(multiplier.text);
    //make sure the multiplier is a valid number
    if(isNaN(m)){
        //do something here to tell the user they entered something invalid
        return; //don't keep going in this function
    }

    //loop through each text input
    for(var i:int=0;i<textInputs.length;i++){
        //convert text input value to number
        var num:Number = int(textInputs[i].text);

        //check if the input is valid
        if(!isNaN(num)){
            textOutputs[i].text = String( (num * 1000) / 100 );
        }else{
            textOutputs[i].text = "Invalid Number";
        }
    }
 }

答案 1 :(得分:0)

此问题现已解决。我将代码发布在这里,以便其他有类似问题的人可以将其用作参考。我还发布了结果here的屏幕截图。

var txtbox:TextFormat=new TextFormat();
txtbox.size = 11;
txtbox.bold = true;

var textInputs:Array = [value1,value2,value3,value4,value5];
var textOutputs:Array = [result1,result2,result3,result4,result5];

for (var i:Number=0; i<textInputs.length; i++)
{
    textInputs[i].setStyle("textFormat",txtbox);
}

multiplier.setStyle("textFormat",txtbox);

for (var j:Number=0; j<textOutputs.length; j++)
{
    textOutputs[j].setStyle("textFormat",txtbox);
}

compute.addEventListener(MouseEvent.CLICK, calBtn);
function calBtn(event:MouseEvent):void
{
    var m:Number=Number(multiplier.text);

for (var i:Number=0;i<textInputs.length;i++)
{
    var num:Number = Number(textInputs[i].text);
    textOutputs[i].text = String(int((num*m)*1000)/1000);
}
}