【问题标题】:as3 if statement not working (var loaded from php)as3 if 语句不起作用(从 php 加载的变量)
【发布时间】:2014-05-15 13:51:43
【问题描述】:

更新:在下面回答!

我正在从为 Air 编码为 as3 的移动设备调用 php 脚本。 php 脚本运行良好,并将正确的 var 发送回 as3。我得到了正确的跟踪语句,但 if 语句不起作用。

这是 php 行:

echo "done=success";

这是as3代码

var variables:URLVariables = new URLVariables(event.target.data);
trace(variables.done); // WORKS echos out: success

// So I figured this line should work too, but it doesn't. Any ideas why?           

if(variables.done=="success")
{
// Does not work            
}

【问题讨论】:

  • 不知道,但您可以尝试将值存储在临时变量中吗?就像var done:String = variables.done 一样,看看这是否也能找到“成功”。如果是这样,请尝试在您的 if 语句中使用它,看看是否可行。
  • 我想过尝试一下。我还考虑了另外两个我也会尝试的选项。 1.也许“done”是flash中的一个var,已经被系统使用并且它不喜欢它或者2.也许我在if语句中需要空格。我将尝试所有 3 个选项,我会告诉你。谢谢
  • 感谢 DidgerThud。我发现问题是最后有一个额外的空白。我用下面的代码提出了我的答案。

标签: php actionscript-3 if-statement


【解决方案1】:

空白!

我发现答案是 php 或 as3 最后添加了一个额外的空白。在跟踪语句中看不到它。我发现了这一点,因为我复制了跟踪语句并注意到有一个额外的空白可以突出显示。

简而言之,trace 语句中的单词“success”(不带引号)实际上是带有额外空格的 var “success”。因此 if 语句不能将其视为“成功”,因为它确实是“成功”。

为了修复它,我使用了以下代码:

var variables:URLVariables = new URLVariables(event.target.data);
trace(variables.done);

var itWorked:String = variables.done;
var rex:RegExp = /[\s\r\n]*/gim;
itWorked = itWorked.replace(rex,'');
if (itWorked == "success")
{
// Now it works. I hope this helps someone. 
}

【讨论】:

  • 如果您不想使用RegExp,也可以使用indexOf - 例如if( itWorked.indexOf( "success ) != -1 )
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-16
  • 1970-01-01
  • 2018-01-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多