简体   繁体   English

ActionScript 3:将字符串转换为and运算符?

[英]ActionScript 3: Turning a String into and Operator?

I'm trying to create a simple calculator program in ActionScript for a school project, and I'm struggling to find a concise way to take an equation from an array such as this: "4","+","2"; 我正在尝试使用ActionScript为学校项目创建一个简单的计算器程序,并且努力寻找一种简洁的方法来从数组中提取方程式,例如:“ 4”,“ +”,“ 2”; and manipulate it so that the answer to the equation can be deduced. 并对其进行操作,以便可以推导出方程的答案。 The problem is taking the String "+" from the equation array and turning it into a usable operator. 问题是从等式数组中获取字符串“ +”并将其转换为可用的运算符。 At the moment, when an operator button is pressed on the calculator GUI it adds the operator to the equation array as a String (ie pressing the '÷' button will add "/" to the equation array). 此刻,当在计算器GUI上按下运算符按钮时,它将运算符作为字符串添加到方程式数组中(即,按下“÷”按钮将在方程式数组中添加“ /”)。

I looked for an answer to this on Google and found something about a JavaScript function that I used in the code below (in lines 1 and 4), but all I end up with in the Output feed is "null", or "0" depending on whether I changed the variable 'answer' to a String, or a Number. 我在Google上寻找了答案,并发现我在下面的代码中使用的JavaScript函数 (在第1行和第4行中),但在输出feed中最终得到的只是“空”或“ 0”取决于我是否将变量“答案”更改为字符串或数字。

Here's the code I have so far: 这是我到目前为止的代码:

import flash.external.ExternalInterface;

var equationArray:Array = new Array("4","+","2");
var answer:Number = ExternalInterface.call("eval",equationArray[0] + equationArray[1] + equationArray[2]);
trace(answer);

I'd prefer to use something like this rather than writing a long if statement to pick between different operators if that's possible. 我宁愿使用这样的方法,而不是写一个长的if语句来在不同的运算符之间进行选择。 Thanks for the help! 谢谢您的帮助!

I did some research and realized that your code doesn't work only in Chrome. 我进行了一些研究,发现您的代码仅在Chrome中不起作用。 There is an error 发生错误

SecurityError: Error #2060
   at flash.external::ExternalInterface$/call()
   at FlexTemp/preinitializeHandler()
   at FlexTemp/___FlexTemp_Application1_preinitialize()
   at flash.events::EventDispatcher/dispatchEvent()
   at mx.core::UIComponent/dispatchEvent()
   at mx.core::UIComponent/initialize()
   at spark.components::Application/initialize()
   at FlexTemp/initialize()
   at mx.managers.systemClasses::ChildManager/childAdded()
   at mx.managers.systemClasses::ChildManager/initializeTopLevelWindow()
   at mx.managers::SystemManager/initializeTopLevelWindow()
   at mx.managers::SystemManager/http://www.adobe.com/2006/flex/mx/internal::kickOff()
   at mx.managers::SystemManager/http://www.adobe.com/2006/flex/mx/internal::preloader_completeHandler()
   at flash.events::EventDispatcher/dispatchEvent()
   at mx.preloaders::Preloader/timerHandler()
   at flash.utils::Timer/tick()

but if I switch of PepperFlash\\14.0.0.145\\pepflashplayer.dll in chrome://plugins/ 但是如果我在chrome:// plugins /中切换了PepperFlash \\ 14.0.0.145 \\ pepflashplayer.dll 在此处输入图片说明

all works fine. 一切正常。 I think there are some problems with pepflashplayer.dll 我认为pepflashplayer.dll存在一些问题

In your flash file: 在您的Flash文件中:

import flash.external.ExternalInterface;
import flash.text.TextField;
var equationArray:Array = new Array("4","-","8");
var answer:Number = ExternalInterface.call("myJsFunction",equationArray[0] , equationArray[1] , equationArray[2])
var tf:TextField = new TextField();
addChild(tf as TextField);
tf.text = answer.toString();

In the HTML page (into the ): 在HTML页面中(进入):

    <script type="text/javascript">
        function myJsFunction(arg1,arg2,arg3){
            return eval(arg1+arg2+arg3);
        }
    </script>

Is this what you try to do? 这是您尝试做的吗? Using eval like in your code I didn't get the correct answer. 像在您的代码中那样使用eval,我没有得到正确的答案。 When you run as here above on localhost/server this will give you the answer. 当您在本地主机/服务器上以上述方式运行时,这将为您提供答案。

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

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