【问题标题】:Haxe Iteration of defining variables定义变量的 Haxe 迭代
【发布时间】:2015-07-17 22:34:40
【问题描述】:

我有一大段代码将 xml 解析为变量。在其中一个变量上,永远不会超过 12,但可能会更少。我试图每秒多次定义这些变量中的每一个。但是,如果它们有变量,我只需要定义它们,因为现在,如果变量少于 12 个,程序就会崩溃。现在下面的代码有效,但前提是我的 xml 文档中存在 12 个值。我如何迭代这段代码,所以我没有“if!= null”语句的列表?

var _vol1 = (ipts[0].volume);
var _vol2 = (ipts[1].volume);
....
....
var _vol12 = (ipts[11].volume);

当我的 xml 文档中有 12 个值时,此代码有效,但如果少于 12 个,则有时会崩溃。

只有在有值且不为空的情况下,我如何构造代码以定义/重新定义变量?这有什么需要迭代的???

If ((ipts[0].volume) != null ){
    var _vol1 = (ipts[0].volume);
}

我在 Haxe 网站上进行了研究,但不确定这适用于何处。我的语法和编程不是最好的。谢谢您的帮助。对不起,如果这是一个不好的问题。

更新:这是我现在的完整代码。它不会崩溃,但现在我添加了 if 语句,它不再定义卷变量

var xml = Xml.parse(_vMixData);

// wrap the xml for fast access
var data = new haxe.xml.Fast(xml.firstElement());

//Getting the data from inputs (here we are getting the xml node 'inputs', which contain two 'input' nodes, as per your sample xml file.
var inputs = data.node.inputs;
var ipts = new Array<VInput>();

for (input in inputs.nodes.input) {
    var ipt = new VInput();
    if (input.has.state) { 
       ipt.state = input.att.state;
       ipt.value = input.innerHTML; 
           }
    if (input.has.volume) {
        ipt.volume = Std.parseInt(input.att.volume);
          }
    if (input.has.muted) {
        ipt.muted = Std.string(input.att.muted);
          }
    ipt.value = input.innerHTML;          
    ipts.push(ipt);
}

var overlays = data.node.overlays;
var ovlys = new Array<VOverlay>();
for (overlay in overlays.nodes.overlay) {
    var ovly = new VOverlay();
    if (overlay.has.number) { 
        ovly.number = Std.parseInt(overlay.att.number);
    }
    ovly.value = overlay.innerHTML;
    ovlys.push(ovly);
}

//Defines variables based on returned information from vMix
var _fadeToBlack:Bool = data.node.fadeToBlack.innerHTML == "True" ? true : false;

var _version = data.node.version.innerHTML;
var _record:Bool = data.node.recording.innerHTML == "True" ? true : false;
var _external:Bool = data.node.external.innerHTML == "True" ? true : false;
var _stream:Bool = data.node.streaming.innerHTML == "True" ? true : false;
var _active:Int = Std.parseInt(data.node.active.innerHTML);
var _preview:Int = Std.parseInt(data.node.preview.innerHTML);

var _overlay1 = (ovlys[0].value);
var _overlay2 = (ovlys[1].value);
var _overlay3 = (ovlys[2].value);
var _overlay4 = (ovlys[3].value);
var _input1state = (ipts[0].state);


if (ipts[0].volume != null ){
var _vol1 = (ipts[0].volume);
}
if (ipts[1].volume != null ){
var _vol2 = (ipts[1].volume);
}
if (ipts[2].volume != null ){
var _vol3 = (ipts[2].volume);
}
if (ipts[3].volume != null ){
var _vol4 = (ipts[3].volume);
}
if (ipts[4].volume != null ){
var _vol5 = (ipts[4].volume);
}
if (ipts[5].volume != null ){
var _vol6 = (ipts[5].volume);
}
if (ipts[6].volume != null ){
var _vol7 = (ipts[6].volume);
}
if (ipts[7].volume != null ){
var _vol8 = (ipts[7].volume);
}
if (ipts[8].volume != null ){
var _vol9 = (ipts[8].volume);
}
if (ipts[9].volume != null ){
var _vol10 = (ipts[9].volume);
}
if (ipts[10].volume != null ){
var _vol11 = (ipts[10].volume);
}
if (ipts[11].volume != null ){
var _vol12 = (ipts[11].volume);
}

//var _1mute = (ipts[0].muted);
//var _2mute = (ipts[1].muted);
//var _3mute = (ipts[2].muted);
//var _4mute = (ipts[3].muted);
//var _5mute = (ipts[4].muted);
//var _6mute = (ipts[5].muted);
//var _7mute = (ipts[6].muted);
//var _8mute = (ipts[7].muted);
//var _9mute = (ipts[8].muted);
//var _10mute = (ipts[9].muted);
//var _11mute = (ipts[10].muted);
//var _12mute = (ipts[11].muted);



if (ipts[2] != null){
    var _ovly3 = 1;
}
//trace(ovlys[2].value);
//trace(ovlys[0].value);
//trace(ipts[0].state);
//trace(_active);
//trace(_preview);
//trace(_fadeToBlack);

【问题讨论】:

    标签: xml iteration haxe


    【解决方案1】:

    要查看 XML 中的每一件事,请使用 for (child in xml)。要拥有任意数量的变量,您需要使用数组,而不是 var1 到 varInfinity。

    你没有发布足够的代码,所以我在这里猜测:

    var varArray = [];
    for (child in xml) {
        varArray.push(child.volume);
    }
    

    这样,无论你有多少变量,代码都会适应(而不是你通过添加 var1、var2 等来适应)。

    【讨论】:

    • 这是我现在拥有的完整代码。我已将它们定义到正常结构中,但我将它们连接到另一个全局变量中,因此我需要将它们重新定义为通过管道传递到另一个程序的变量。
    • 不完全确定您的意思,但您不能将它们全部放入一个数组并将该数组传递给其他程序?
    • 我正在一个名为 Stencyl 的程序中编译这段代码。它的界面中集成了 Haxe 代码块
    • 我从来没有使用过 stenyl,但是,你能传递数组吗? _vol1 可以是一个数组,你可以在 stencyl 内部做一些事情吗?
    • 上面的代码在没有 if 语句的情况下工作,但是现在我添加了 if 语句,代码不起作用。所以不管这两个程序之间的接口如何,haxe 代码不应该是一样的吗?此代码有效。 var _vol1 = (ipts[0].volume); var _vol2 = (ipts[1].volume); var _vol3 = (ipts[2].volume);但由于某种原因,添加 if 语句会搞砸。
    猜你喜欢
    • 2010-09-08
    • 1970-01-01
    • 2021-02-03
    • 1970-01-01
    • 1970-01-01
    • 2016-04-05
    • 2016-08-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多