开发者

get equation from XML, AS3

开发者 https://www.devze.com 2022-12-24 22:38 出处:网络
There\'s an variable in my swf I want to receive XML. It\'s an integer value in the form of an equation. How do I receive the XML value for \'formatcount\'?

There's an variable in my swf I want to receive XML. It's an integer value in the form of an equation. How do I receive the XML value for 'formatcount'?

My Variable

//Variable I want to grab XML<br>
//formatcoun开发者_如何学Pythont=int('want xml value to go here');

formatcount=int(count*count/100);

Path

formatcount = myXML.FORMATCOUNT.text()

My XML

<?xml version="1.0" encoding="utf-8"?>
   <SESSION>
      <TIMER TITLE="speed">1000</TIMER>
      <COUNT TITLE="starting position">10000</COUNT>
      <FORMATCOUNT TITLE="ramp">count*count/1000</FORMATCOUNT>
</SESSION>


You seem to be trying to do a calculation inside your XML file, and while you could be trying to read in an equation from XML, I don't see why either needs to be done at the level of the XML file itself. It'd be much easier to just read in values from the XML file, and do any calculations in your AS.

If you've read the value of <COUNT> into a variable in your code (I presume through myXML.COUNT), then just do:

formatcount = count*count / 10000;

Where count is:

var count:Number = myXML.COUNT;

As is usual with your questions, I'm not sure if I'm answering your actual question or if I've misinterpreted it. Good luck anyway.


You will need to write or use a parser that will be able to parse and evaluate your expression:

Check for example this evaluator of expression:

Based on the above lib (not tested):

import it.sephiroth.expr.CompiledExpression;
import it.sephiroth.expr.Parser;
import it.sephiroth.expr.Scanner;
import flash.display.Sprite;


public class Example extends Sprite
{
  public function Example() {
    super();

    var myXML:XML=<SESSION>
      <TIMER TITLE="speed">1000</TIMER>
      <COUNT TITLE="starting position">10000</COUNT>
      <FORMATCOUNT TITLE="ramp">count*count/1000</FORMATCOUNT>
    </SESSION>;

    var expression: String = myXML.FORMATCOUNT.toString();
    var scanner: Scanner = new Scanner( expression );
    var parser: Parser = new Parser( scanner );

    var compiled: CompiledExpression = parser.parse();

    var count:Number=Number(myXML.COUNT.toString());

    var context: Object = {
      count:count
    };

    var formatcount:Number = compiled.execute( context );
  }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号