AS3在进入if语句之前计算flashVar的数量

发布于 2024-11-19 16:23:56 字数 759 浏览 0 评论 0原文

我正在使用 AS3 创建一个简单的图像查看器,并从 HTML 中的 flashVars 获取图像 URL。我想使用 if

参数让它在只有一个 flashVar 时执行一个函数,如果有更多的话则执行另一个函数。

我让它可以很好地读取 flashVars,并且可以整天编写 if 语句,我遇到的麻烦是计算

从 HTML 传递的 flashVars 的数量。这是我正在尝试做的事情的一个真正的简化版本(这不起作用

,因为我无法找出要遵循的正确过程):

var numberOfVars:Number = 0; // to store the number of flashVars
var paramObj:Object = LoaderInfo(this.root.loaderInfo).parameters; //get the flashVars

for each (paramObj in LoaderInfo(this.root.loaderInfo).parameters){
    numberOfVars + 1;
}

var tf:TextField = new TextField();
addChild(tf);
tf.text = numberOfVars.toString(); // this returns '0' when runningn this code


if (numberOfVars < 2){
    //do this
}

else {
    //do this
}

感谢你们的帮助。

克里斯

I am creating a simple image viewer using AS3 and getting image URLs from flashVars in the HTML. I want to use an if

argument to have it perform one function if there is only one flashVar and another if there are more.

I have it reading the flashVars fine and can write if statments all day long, the trouble I am having is counting the number

of flashVars being passed from the HTML. here is a real dumbed down version of what I am trying to do (which doesnt work

because I can not figure out the correct process to follow):

var numberOfVars:Number = 0; // to store the number of flashVars
var paramObj:Object = LoaderInfo(this.root.loaderInfo).parameters; //get the flashVars

for each (paramObj in LoaderInfo(this.root.loaderInfo).parameters){
    numberOfVars + 1;
}

var tf:TextField = new TextField();
addChild(tf);
tf.text = numberOfVars.toString(); // this returns '0' when runningn this code


if (numberOfVars < 2){
    //do this
}

else {
    //do this
}

thanks for your help guys.

Chris

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

何以心动 2024-11-26 16:23:56

你会想要增加 numberOfVars

看起来你有这个:

 numberOfVars + 1;

当你应该有这个:

 numberOfVars++;

这只是写这个:

 numberOfVars = numberOfVars + 1;

或这个:

 numberOfVars += 1;

You would want to increment numberOfVars

Looks like you have this:

 numberOfVars + 1;

When you should have this:

 numberOfVars++;

which is just short way of writing this:

 numberOfVars = numberOfVars + 1;

or this:

 numberOfVars += 1;
微暖i 2024-11-26 16:23:56

只需使用 for(key in object) 循环来跟踪存储在 root.loaderInfo.parameters 上的变量数量。

var fvars:Object = root.loaderInfo.parameters;

var n:int = 0;
for(var i:String in fvars) n ++;

// n holds the amount of flashvars
if(n <= 1)
{
    // one or no variables found
}
else
{
    // more than one found
}

另外,您的一位操作员做错了:

for each(paramObj in LoaderInfo(this.root.loaderInfo).parameters)
{
    numberOfVars + 1; // <<<<<< here
}

您正在寻找以下其中一项:

numberOfVars ++;
numberOfVars += 1;
numberOfVars = numberOfVars + 1;

Just use a for(key in object) loop to track the amount of vars stored on root.loaderInfo.parameters.

var fvars:Object = root.loaderInfo.parameters;

var n:int = 0;
for(var i:String in fvars) n ++;

// n holds the amount of flashvars
if(n <= 1)
{
    // one or no variables found
}
else
{
    // more than one found
}

Also, you've done one of your operators wrong:

for each(paramObj in LoaderInfo(this.root.loaderInfo).parameters)
{
    numberOfVars + 1; // <<<<<< here
}

You're looking for one of the following:

numberOfVars ++;
numberOfVars += 1;
numberOfVars = numberOfVars + 1;
恏ㄋ傷疤忘ㄋ疼 2024-11-26 16:23:56

我将在这里对条件逻辑提供稍微不同的看法。与其根据变量的数量来对变量做出反应(这会带来大量出现不可预测结果的机会),为什么不根据变量的实际情况来制定条件语句呢?这是我的标准 flashVar 流程:

var _loadParams:Object
private function init():void 
    {
        _loadParams = new Object();
        _loadParams = LoaderInfo(stage.loaderInfo).parameters;

        someVar     = String(   parseParam( "someVar", "default value") );
    }

    private function parseParam(name:String, defaultValue:String):String
    {
        if (_loadParams.hasOwnProperty(name) && _loadParams[name] != "" && _loadParams[name] != "undefined") 
        {
            return _loadParams[name];
        }
        else
        {   
            return defaultValue;
        }
    }

显然,这只是测试某个值是否存在,但可以轻松更改它以根据这些结果调用某些功能:

if (_loadParams.hasOwnProperty(name) && _loadParams[name] != "" && _loadParams[name] != "undefined") 
{               
    switch (name){
        case 'param1':
            doSomthing(name);
            break;
        case 'param2':
            doSomthingElse(name);
            break;
    }
}

我不知道您要做什么正在努力实现这一目标,而这可能还很遥远。但我想我应该插话;)

干杯!

I will offer a slightly different take on the conditional logic here. Instead of reacting to variables based on how many there are (introducing plenty of opportunity for unpredictable results) - why not base your conditional statements on what the vars actually are. This is my standard flashVar process:

var _loadParams:Object
private function init():void 
    {
        _loadParams = new Object();
        _loadParams = LoaderInfo(stage.loaderInfo).parameters;

        someVar     = String(   parseParam( "someVar", "default value") );
    }

    private function parseParam(name:String, defaultValue:String):String
    {
        if (_loadParams.hasOwnProperty(name) && _loadParams[name] != "" && _loadParams[name] != "undefined") 
        {
            return _loadParams[name];
        }
        else
        {   
            return defaultValue;
        }
    }

This, obviously, is just testing for the presence of a value, but it could be easily altered to call some functionality based on those results:

if (_loadParams.hasOwnProperty(name) && _loadParams[name] != "" && _loadParams[name] != "undefined") 
{               
    switch (name){
        case 'param1':
            doSomthing(name);
            break;
        case 'param2':
            doSomthingElse(name);
            break;
    }
}

I don't know what you are trying to achieve, and this may be way off. But I thought I'd chime in ;)

Cheers!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文