如何在Action Script 2中将变量传递给内联函数 [英] How to pass variables into inline functions in Action Script 2

查看:110
本文介绍了如何在Action Script 2中将变量传递给内联函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有以下功能,但似乎无法将myVar变量添加到内联函数中。我在这里做错了什么?我想发生的是,当我单击myMc时,它应该将myVar打印到控制台( hello computer)。

I have the following function, but I can't seem to get the myVar variable into the inline function. What am I doing wrong here? What I would like to have happen is when I click on myMc, it should print myVar to the console ("hello computer").


function doSomething():Void
{   
    myVar = "hello computer";

    myMc.onRelease = function(){
        trace(myVar); //prints as "undefined"
    }
}

ps。 -我无法将myVar声明为全局变量或静态变量,因为在实际代码中,我正在解析XML,而myVar却在不断变化。

ps. - I cannot declare myVar as a global or static variable because in the real code, I'm parsing XML and the myVar is constantly changing.

推荐答案

这是一个范围问题-在as2中应用像这样的onRelease函数时,该函数的范围是您将函数应用于的MovieClip,而不是调用函数。

This is a scope issue - when you apply an onRelease function like this in as2, the scope of the function is the MovieClip you apply the function to, not the calling function.

由于您使用的是AS2并且MovieClip是动态的,因此可以直接将变量分配给MC:

Because you are using AS2 and MovieClip is dynamic, you can assign the variable to the MC directly:

function doSomething():Void
{   
    myMc.myVar = "hello computer";

    myMc.onRelease = function(){
        trace(this.myVar);
    }
}

这篇关于如何在Action Script 2中将变量传递给内联函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆