Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Actionscript 3 AS3从函数中在根目录中创建变量_Actionscript 3_Flash_Variables_Reload_Reset - Fatal编程技术网

Actionscript 3 AS3从函数中在根目录中创建变量

Actionscript 3 AS3从函数中在根目录中创建变量,actionscript-3,flash,variables,reload,reset,Actionscript 3,Flash,Variables,Reload,Reset,我现在有一个相当大的swf,已经有一些编码了。大多数变量都是在根目录中创建的,但现在我遇到了一个问题 我想重新加载flashswf(reset),为此,我需要创建一个销毁所有变量的函数和另一个创建它们的函数。目前,我有一个javascript函数可以重新加载页面,但这并不是一个好的解决方案 问题是,当我在函数中创建var时,它不会在“MovieClip(root)”中创建,而是只与函数相关,从而使我的swf无法工作 有没有办法从函数中创建MovieClip(root)中的变量?或者除了我想做的事

我现在有一个相当大的swf,已经有一些编码了。大多数变量都是在根目录中创建的,但现在我遇到了一个问题

我想重新加载flashswf(reset),为此,我需要创建一个销毁所有变量的函数和另一个创建它们的函数。目前,我有一个javascript函数可以重新加载页面,但这并不是一个好的解决方案

问题是,当我在函数中创建var时,它不会在“MovieClip(root)”中创建,而是只与函数相关,从而使我的swf无法工作

有没有办法从函数中创建MovieClip(root)中的变量?或者除了我想做的事,还有别的选择吗

编辑:添加了一些示例代码。

function SetVar():void{
var test:String= new String("foobar");
}

SetVar();

trace(test);
…输出为:

Scene 1, Layer 'Layer 1', Frame 1, Line 7   1120: Access of undefined property test.

这是正常的,因为“var测试”不是全局的,所以在函数结束时它丢失了。我想让函数“SetVar()”将变量添加到根或全局。

我不完全确定您在寻找什么,因为没有与您的问题相关的代码。不过,我会透露一些我认为与这个主题有关的信息

如果在类中声明变量,则可以从函数中引用它们:

package{
    import flash.display.MovieClip;

    public class DocumentClass extends MovieClip{

        public var example:String = 'dog';

        public function DocumentClass(){
            trace(example); // dog
            testFctn();
            trace(example); // frog
        }

        public function testFctn(){
            example = 'frog'
        }
    }
}

如果要引用父类的变量,此.parent['variableName']也很有用。或者您的工作阶级的兄弟姐妹共享一个父类,
this.parent['childClass']['variableName']

我不完全确定您在寻找什么,因为没有与您的问题相关联的代码。不过,我会透露一些我认为与这个主题有关的信息

如果在类中声明变量,则可以从函数中引用它们:

package{
    import flash.display.MovieClip;

    public class DocumentClass extends MovieClip{

        public var example:String = 'dog';

        public function DocumentClass(){
            trace(example); // dog
            testFctn();
            trace(example); // frog
        }

        public function testFctn(){
            example = 'frog'
        }
    }
}

如果要引用父类的变量,此.parent['variableName']也很有用。或者您的工作阶级的兄弟姐妹共享一个父类,
this.parent['childClass']['variableName']

我不完全确定您在寻找什么,因为没有与您的问题相关联的代码。不过,我会透露一些我认为与这个主题有关的信息

如果在类中声明变量,则可以从函数中引用它们:

package{
    import flash.display.MovieClip;

    public class DocumentClass extends MovieClip{

        public var example:String = 'dog';

        public function DocumentClass(){
            trace(example); // dog
            testFctn();
            trace(example); // frog
        }

        public function testFctn(){
            example = 'frog'
        }
    }
}

如果要引用父类的变量,此.parent['variableName']也很有用。或者您的工作阶级的兄弟姐妹共享一个父类,
this.parent['childClass']['variableName']

我不完全确定您在寻找什么,因为没有与您的问题相关联的代码。不过,我会透露一些我认为与这个主题有关的信息

如果在类中声明变量,则可以从函数中引用它们:

package{
    import flash.display.MovieClip;

    public class DocumentClass extends MovieClip{

        public var example:String = 'dog';

        public function DocumentClass(){
            trace(example); // dog
            testFctn();
            trace(example); // frog
        }

        public function testFctn(){
            example = 'frog'
        }
    }
}

如果要引用父类的变量,此.parent['variableName']也很有用。或者您的工作类的同级共享父类this.parent['childClass']['variableName']..

由于您在函数中声明变量,其作用域仅限于该函数

尝试在函数外部声明变量,然后在函数中初始化它。 然后,您应该能够从根目录访问它

但是,如果您希望从函数中声明根上的变量(非常不寻常的要求),则可以尝试执行以下操作:

document["variableName'] = value;


函数内部。

由于您在函数中声明变量,因此其范围仅限于该函数

var test:String;
var anotherTest:String;
function setVar():String {
    return 'foorBar';
}

this.text = setVar();
this.anotherTest = setVar();    

trace(this.test); // output: fooBar
trace(this.anotherTest); // output: fooBar
尝试在函数外部声明变量,然后在函数中初始化它。 然后,您应该能够从根目录访问它

但是,如果您希望从函数中声明根上的变量(非常不寻常的要求),则可以尝试执行以下操作:

document["variableName'] = value;


函数内部。

由于您在函数中声明变量,因此其范围仅限于该函数

var test:String;
var anotherTest:String;
function setVar():String {
    return 'foorBar';
}

this.text = setVar();
this.anotherTest = setVar();    

trace(this.test); // output: fooBar
trace(this.anotherTest); // output: fooBar
尝试在函数外部声明变量,然后在函数中初始化它。 然后,您应该能够从根目录访问它

但是,如果您希望从函数中声明根上的变量(非常不寻常的要求),则可以尝试执行以下操作:

document["variableName'] = value;


函数内部。

由于您在函数中声明变量,因此其范围仅限于该函数

var test:String;
var anotherTest:String;
function setVar():String {
    return 'foorBar';
}

this.text = setVar();
this.anotherTest = setVar();    

trace(this.test); // output: fooBar
trace(this.anotherTest); // output: fooBar
尝试在函数外部声明变量,然后在函数中初始化它。 然后,您应该能够从根目录访问它

但是,如果您希望从函数中声明根上的变量(非常不寻常的要求),则可以尝试执行以下操作:

document["variableName'] = value;


函数内部。

您需要了解作用域的工作原理

var test:String;
var anotherTest:String;
function setVar():String {
    return 'foorBar';
}

this.text = setVar();
this.anotherTest = setVar();    

trace(this.test); // output: fooBar
trace(this.anotherTest); // output: fooBar
基本上:

  • 在另一个对象(无论是类、函数、对象还是循环)中声明的对象仅在该特定对象或循环迭代中可用
  • 对象范围由子对象继承,而不是由父对象继承。因此,类中的函数可以访问该类中声明的对象,但类不能访问函数中声明的对象
  • 父对象(或任何其他对象)可以访问在子类中声明的对象,但仅当它是
    公共对象时
  • 因此,看看这些基本规则(它们非常非常基本。如果您刚刚开始,我建议您对OOP中的对象范围进行一些适当的研究。它是您将用几十种语言执行的所有操作的基础),您在函数中声明一个对象,并尝试从该函数外部访问它。这违反了上面的规则#1

    相反,请尝试以下方法:

    var test:String;
    function setVar():void{
        this.test = 'foorBar';
    }
    
    trace(test); //output: null (undeclared)
    
    setVar();
    
    trace(this.test); // output: fooBar
    
    看到这一点,我做了两件事:

  • 我将
    test
    的声明移动到了全局空间,这意味着该对象中的任何对象都可以访问它