闪光CS5引用的显示对象从一类非文件类其他 [英] Flash CS5 reference a display object from a class other than the document class

查看:127
本文介绍了闪光CS5引用的显示对象从一类非文件类其他的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Flash CS5专业我已经创建了一个符号,把它拖上舞台,并给它GreenLight1实例名称。如果我想打从文档类此可见,我可以简单地做GreenLight1.visible = TRUE;和噗它的好,当我测试文件中去。只要我留在文档类,我好去,但现在我想移动到另一个类,并创下各种麻烦只是试图让闪光,让我来访问这个简单的对象。所有我希望做的是有这个GreenLight1去无形的(可见= FALSE)时,在一定条件下发生在这个新的类和Flash是不会让我访问GreenLight1的。

我试过

东西迄今:

  1. 阶段传递给类,是由_stage引用,并且工作得很好,当我做_stage.addchild或类似的东西。所以,我曾尝试_stage.GreenLight1.visible = FALSE;和我的ReferenceError:错误#1069:房产GreenLight1不是的flash.display.Stage发现并没有默认值。

  2. 我的文档类扩展雪碧,所以我想我会尝试的根本功能。所以,我想雪碧(根).GreenLight1.visible = FALSE;而我得到的1119:可能未定义的属性GreenLight1通过与静态类型flash.display使用一个参考访问:雪碧

  3. 最后的答案在这个线程我试着如其中所述创建资源类。而我碰到,我在开始不知道是什么GreenLight1是开始,所以我得到了同样的问题,1120:未定义的属性GreenLight1的访问这是我的$ C $下Resource.as(我应该通过什么这一类的文档类?):

`

 包{

进口对象类型:flash.events.Event;
进口flash.display.Sprite;
进口flash.display.DisplayObject;

公共类资源扩展Sprite {
    公共静态无功GL1:绿激光;

    公共职能资源(){
        的addEventListener(Event.ADDED_TO_STAGE,初始化,假,0,真正的);
    }

    私有函数初始化(五:事件):无效{
        Resource.GL1 = GreenLight1;
    }
}
 

}

类型绿激光是GreenLight1的源符号。我有为ActionScript导出勾掉和基类被称为绿激光。所以这就是来自。我应该做一个新绿激光的地方或者类似的东西?我说我想从我用来访问它的类Resource.GL1.visible = FALSE;,但它从来没有真正得到担心,因为我得到#3上面列出的编译错误。

在任何情况下,我在一个损失,以什么尝试下一个。所以...如何在世界上我得到一个类,不是文档类识别GreenLight1?

解决方案

1 不工作的对象,因为属性(假设它在显示器上)是Stage对象。默认情况下,你的文档类将成为舞台的第一个孩子,除非你已经在那里使用 setChildIndex(0) addChildAt(someObject插入一些东西,0)。所以,你应该能够通过文档类与访问

  this.stage.getChildAt(0).GreenLight1;
 


2 不工作,因为你投你的根作为一个雪碧。这不是一个Sprite,这是你的文档类是雪碧的后代,所以这应该工作:

  this.root.GreenLight1
 


我要跳过第三,并尝试提供一个更直接的解决方案。你在你的库中这个符号,你将其设置为为ActionScript导出,并给它的GreenLight 的类名。良好的开端。所以,现在在任何地方你code,你可以做这样的事情:

  VAR myGreenLight:绿激光=新绿激光();
 

这创造了一个引用( myGreenLight )添加到您的GreenLight符号的新实例。现在,您可以连接到您调用类的显示树

 的addChild(myGreenLight);
 

假设你的编码中的类本身就是在舞台上,那么你的绿激光的情况下应该是可见的。你也可以从任何对象是在舞台上,调用 this.stage.addChild(myGreenLight); 来直接连接到舞台上你的GreenLight例如,如果这是你想要的

所以,现在,终于到了真正的问题。你所谓的舞台有的GreenLight 的实例 GreenLight1 。 (请注意,按照惯例用大写,变量名和实例名唯一的类名就应该开始用小写字符)。你有另一个类,这也是在显示目录树,你需要得到一个引用 GreenLight1 这就是在舞台上。这里有一个函数来做到这一点:

 函数getMovieClip($实例:字符串,$范围:级DisplayObjectContainer)的DisplayObject
{
    VAR孩子:的DisplayObject;
    VAR loopLength:INT = $ scope.numChildren;
    对于(VAR我:= 0; I< loopLength;我++){
        子= $ scope.getChildAt(ⅰ);
        如果(child.name == $实例)返回的孩子;
    }
    //没找到
    返回null;
}
 

和使用它的任何对象可以访问阶段是这样的:

  VAR greenLightRef:绿激光= getMovieClip('GreenLight1',this.stage)为绿激光;
 

Using Flash CS5 Professional I have created a symbol, dragged it onto the stage, and given it an instance name of GreenLight1. If I want to make this visible from the document class, I can simply do the GreenLight1.visible=true; and poof it's good to go when I test the file. As long as I stay in the document class I am good to go, but now I'm trying to move to another class and hitting ALL kinds of trouble just trying to get Flash to allow me to access this simple object. All I am looking to do is have this GreenLight1 go invisible (visible=false) when a certain condition occurs in this new class and Flash just won't let me access GreenLight1 at all.

Things I've tried thus far:

  1. stage is passed to the class and is referenced by _stage and is working just fine when I do _stage.addchild or anything like that. So I have tried "_stage.GreenLight1.visible=false;" and I get "ReferenceError: Error #1069: Property GreenLight1 not found on flash.display.Stage and there is no default value."

  2. My document class extends Sprite, so I figured I'd try the root function. So I tried "Sprite(root).GreenLight1.visible=false;" and I get "1119: Access of possibly undefined property GreenLight1 through a reference with static type flash.display:Sprite."

  3. Finally on the advice of the answer in this thread I tried to create the Resource class as described therein. To which I came across the same problem that I started with in that it doesn't know what GreenLight1 is to begin with so I got "1120: Access of undefined property GreenLight1." Here is my code for Resource.as (am I supposed to pass something to this class from the document class?):

`

package {

import flash.events.Event;
import flash.display.Sprite;
import flash.display.DisplayObject;

public class Resource extends Sprite {
    public static var GL1:GreenLight;

    public function Resource() {
        addEventListener(Event.ADDED_TO_STAGE, init, false, 0, true);
    }

    private function init(e:Event):void{
        Resource.GL1 = GreenLight1;
    }
}

}

The type "GreenLight" is from the source symbol of GreenLight1. I have "Export for ActionScript" checked off and the base class is called GreenLight. So that is where that comes from. Am I supposed to make a "new GreenLight" somewhere or something like that?? I the class that I'm trying to access it from I am using "Resource.GL1.visible=false;", but it never really gets to worry about that because I get the compile error listed in #3 above.

In any event, I'm at a loss as to what to try next. So... How in the world do I get a class that isn't the document class to recognize GreenLight1?

解决方案

1 doesn't work because the stage property of an object(assuming it's on the display) is a Stage object. By default, your document class will be the first child of the stage, unless you have inserted something in there using setChildIndex(0) or addChildAt(someObject,0). So you should be able to access via the document class with

this.stage.getChildAt(0).GreenLight1;


2 doesn't work because you are casting your root as a Sprite. It's not a Sprite, it's your document class that is a descendant of Sprite, so this should work:

this.root.GreenLight1


I'm going to skip over number three and try to offer up a more direct solution. You have this symbol in your library, you've set it to 'Export for actionscript' and have given it a class name of GreenLight. Good start. So now anywhere in your code you can do something like this:

var myGreenLight:GreenLight = new GreenLight();

which has created a reference (myGreenLight) to a new instance of your GreenLight symbol. You can now attach this to the display tree of your calling class with

addChild(myGreenLight);

Assuming that the class you're coding in is itself on the stage, then your instance of GreenLight should be visible. You could also, from any object that's on the stage, call this.stage.addChild(myGreenLight); to attach your GreenLight instance directly to the stage if that was what you wanted.

So now, finally to the real question. You have an instance of GreenLight on the stage called GreenLight1. (Please note, by convention only class names start with a capital, variable names and instance names should start with a lower case character). You have another class which is also on the display tree and you need to get a reference to GreenLight1 that's on the stage. Here's a function to do that:

function getMovieClip($instanceName:String,$scope:DisplayObjectContainer):DisplayObject
{
    var child:DisplayObject;
    var loopLength:int = $scope.numChildren;
    for(var i:int = 0; i < loopLength; i++) {
        child = $scope.getChildAt(i);
        if(child.name == $instanceName) return child;
    }
    //didn't find it
    return null;
}

and you use it from any object that can access stage like this:

var greenLightRef:GreenLight = getMovieClip('GreenLight1',this.stage) as GreenLight;

这篇关于闪光CS5引用的显示对象从一类非文件类其他的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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