如何在AS3的类文件的功能链接到的DocumentClass和影片剪辑? [英] How to link functionality from Class file to DocumentClass and to Movieclips in AS3?

查看:262
本文介绍了如何在AS3的类文件的功能链接到的DocumentClass和影片剪辑?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

和我有问题,从时间轴code切换到OOP /文档类。我设法用AS3时间轴上没有问题的构建佛罗里达州。但完全一无所知面向对象的时候。

and i am having problem, switching from Timeline code to OOP/Document Class. I managed to build the Fla with AS3 on timeline with no problem. But totally clueless when on OOP.

我被告知,场景都没有好,我要坚持救了我的场景为影片剪辑。

I had been told that Scenes are no good and i should stick to saving my scenes as Movieclips.

我的情况是这样: 我有8页的PSD文件,我进口的每PSD到​​Flash Pro和每个PSD有几个按钮和TextInput。第一页是登录页,第二页是注册页面等。

My situation is as such: I have 8 pages of PSD files, and i imported each PSD into Flash Pro, and each PSD has a few buttons and textinput. First page is Login page, second page is register page etc.

我的问题是:

1。)我应该如何保存PSD?我把它们保存为嵌套的动画,(给予PSD的一个符号?按钮和TextInput每个项目?)然后保存该PSD为1影片剪辑(嵌套的动画?) 我已经尝试过引进与Flash图层的PSD到舞台上,然后给每个按钮和TextInput的属性,然后萨它们作为嵌套动画,但我认为这种影片剪辑的类文件?还是我在链接的文档类(Main.as)?

1.) How should i save the PSD? Do i save them as nested Animation,(Giving each item on PSD a symbol? Buttons and textinput?) Then saving that PSD as 1 movieclip(Nested Animation?) I already tried importing the PSD with Flash Layers onto Stage,then giving each buttons and Textinput their Properties then savin them as Nested Animation, but do i call that Movieclip from the Class document? Or do i link All Movieclips in the Document Class(Main.as)?

2)如何从类文件访问的影片剪辑,我想VAR登录:登录=新建登录,然后的addChild(登录)。这增加了影片剪辑但没有功能的作品,和错误说未定义的属性的每一个按钮的名称访问。

2.) How do i access the Movieclip from Class file, I tried var login:Login = New Login, then addChild(login). That adds the movieclip but none of the functionality works, and errors saying Access of Undefined Properties for every single button's Name.

3)。而如果单击1按钮和链接到另一个页面(PSD)办下面?

3.) And if 1 button is clicked and that links to another page(PSD) do i do the Below?

h.addEventListener(MouseEvent.CLICK, fl_ClickToGohome);

 function fl_ClickToGohome(event:MouseEvent):void
{
    gotoAndStop("register.as");

感谢您的时间

Thanks for your time

推荐答案

编程。如除了时间轴文件也差不太多。如果你使用的文档类(在ActionScript设置面板中找到),你可能会觉得在课堂上特定的编程有点望而生畏的突然变化。对于一个平滑的过渡,你可以简单地转储当前时间轴code到一个新的 code.as 文件(原样),并简单地把下列在你的时间表行:

Programming in .as files apart from the timeline isn't too different. If you're using the document class (found in Actionscript Settings panel), you may be feeling the abrupt change in class specific programming a bit too daunting. For a smoother transition, you can simply dump your current timeline code into a new code.as file (as-is), and simply drop the following line in your timeline:

include "code.as";

这有效地只复制/贴牌code和运行它,但现在你可以使用适当的code编辑器(如的崇高文字)。此外,因为它不是一个新的类,你在里面写有相同的范围,因为你的时间线中的code。

This effectively just copy/pastes the code and runs it, except now you can use a proper code editor (such as Sublime Text). Furthermore, as it's not a new class, the code you write there has the same scope as your timeline.

2)。的功能只能够访问自己的空间中创建(这可能是为什么你的文档类被扔错误未定义的属性的访问)。你总是可以传递一个引用一个对象(通过扩展,它的命名空间)通过的函数参数的。

2). Functions only have access to the namespace their created in (this is probably why your document class was throwing "Access of Undefined Properties" errors). You can always pass a reference to an object (and by extension, its namespace) through function arguments.

1)。的要回答你的第一个问题,您可以创建你的影片剪辑,其嵌套深深为你想要的,你想要的任何命令。重要的是要知道如何导航到该对象。就拿下一阶段heiarchy:

1). To answer your first question, you can create your movieclips, and nest them as deeply as your want, in any order you want. The important thing is to be aware of how to navigate to that object. Take for example the following stage heiarchy:

root:MainTimeline ¬
    0: PSD_one:MovieClip ¬
        0: backgroundImage:Bitmap
        1: button:Sprite
        2: txt:TextField
    1: PSD_two:MovieClip
    2: PSD_three:MovieClip

时间线有3个对象,每一个从零开始的索引。第一个PSD是一个影片剪辑<一个href="http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/DisplayObjectContainer.html"相对=nofollow>的DisplayObjectContainer 有3子元素,每个名称的属性,我们可以用它来解决的对象。在Flash IDE中,我们将它们标记为实例,它们自动成为父对象上,由于内部的魔法属性的IDE在ActionScript设置面板上标示,使自动声明舞台实例。

The timeline has 3 objects, each with a zero-based index. The first PSD is a MovieClip DisplayObjectContainer with 3 child elements, each with name properties that we can use to address the objects. In Flash IDE we label these "instances", and they automatically become properties on the parent object due to internal magic the IDE has enabled in the Actionscript Settings panel labeled "Automatically declare stage instances".

这意味着,要更改 TXT ,我们可以写的文字 PSD_one.txt.text =富。如果没有这个设置,您需要使用容器的方法,如<一href="http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/DisplayObject.html#root"相对=nofollow> <一个href="http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/DisplayObjectContainer.html#getChildByName%28%29"相对=nofollow> getChildByName(PSD_one) <一个href="http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/DisplayObjectContainer.html#getChildAt%28%29"相对=nofollow> getChildAt(2) 。夺标调用 txt.text =富行不通,因为是在当前范围称那(即, this.txt <无属性/ code>是暗示)。

This means, to change the text on txt, we can write PSD_one.txt.text = "foo". Without this setting, you'll need to use container methods like root.getChildByName("PSD_one").getChildAt(2). Outright calling txt.text = "foo" will never work because there is no property on the current scope called that (ie., this.txt is implied).

3)。的<一个href="http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/MovieClip.html#gotoAndStop%28%29"相对=nofollow> gotoAndStop 是控制时间轴帧的电影剪辑的方法。如果你真的打算让从做事的旧方式划清界限,你应该放下你的使用框架。

3). gotoAndStop is a MovieClip method that controls timeline frames. If you're really going to make a clean break from the old way of doing things, you should drop your use of frames.

有两种,你可以接近显示这些PSD的方式。

There are two ways you could approach displaying these PSDs.

方法1: 你比如你的月台幕门上用Flash IDE的舞台,并给每个人一个唯一的名称,然后可以在code参考。假设上面的布局,您可以简单地将每一个PSD屏幕外(如用 PSD_two.x = this.loaderInfo.width ),并将其交换到的画面时的中心你想走出去到下一个框架。

Method 1: You instance your PSDs on the stage with the Flash IDE and give each one a unique name that you can then reference in your code. Assuming the layout above, you may simply move each PSD offscreen (such as with PSD_two.x = this.loaderInfo.width), and swap them to the center of the screen when you want to "go" to the next "frame".

方法2: 你输入你的PSD到库你习惯了,但不要比如他们。相反,你直接跳到你的code,当有人点击按钮 ^ h fl_ClickToGohome ,你功能采库对象,手动情况下,它和家长到台上与<一个href="http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/DisplayObjectContainer.html#addChild%28%29"相对=nofollow>的addChild()。

Method 2: You've imported your PSDs into your library as you're accustomed to, but do not instance them. Instead, you jump straight into your code and when someone clicks on button h to go to fl_ClickToGohome, your function picks the library object and manually instances it, and parents it to the stage with addChild().

function fl_ClickToGohome(e:MouseEvent):void {
    var psd:PSD_one = new PSD_one();
    addChild(psd);
}

这样的方法将是preferable,如果你要开始进入资产动态加载。当然,而不是在您的图书馆有一个已经创建 PSD_one 类,你只需要使用的的URLLoader()或的装载机(),你会想保存了COM pressed图像,而不是全面的月台幕门。

This sort of approach would be preferable if you're going to start getting into dynamic loading of assets. Of course, rather than having an already created PSD_one class in your library, you'll simply use URLLoader() or Loader(), and you'll want to save out compressed images, not full-blown PSDs.

我希望这有助于。我知道我没有直接回答你的问题,但是从实施方式有时问题的茎,没有具体的code。

I hope this helps. I know I haven't answered your questions directly, but sometimes the issue stems from manner of implementation, not specific code.

-Cheers

1:路径为对象

如果你不知道的路径是你的对象是什么,试试这个功能。这是一个我在我自己的工作中使用的下调版本。

If you're not sure what the path is to your objects, try this function. It's a trimmed down version of the one I use in my own work.

function listLayers(obj = null, indent:String = ""):void {
    // If no object was provided, start with the MainTimeline
    if (obj == null) { obj = root; }

    for (var i:int = 0; i < obj.numChildren; i++) {
        // Make a pointer to the object at this index
        var item = obj.getChildAt(i);

        // Get the item type
        var type:String = flash.utils.getQualifiedClassName(item);
        if (type.lastIndexOf("::") != -1) {type = type.split("::")[1];}

        var msg:String = indent + i + ": " + item.name + ":" + type;
        if (type != "TLFTextField" && item.hasOwnProperty("numChildren") && item.numChildren > 0) {
            trace(msg + " ¬");
            listLayers(item, indent + "  ");
        } else {
            trace(msg);
        }
    }   
}

这将打印出你的舞台的结构大致相同的格式,你上面看到的(与月台幕门)。然后,您可以使用 getChildByName 从根到孩子(完整路径必须提供,非常喜欢你的硬盘驱动器上浏览文件夹)。

This will print out the structure of your stage in much the same format as you saw above (with the PSDs). You can then use getChildByName from the root to the child (the full path must be provided, much like navigating folders on your hard drive).

2:公开/私有命名空间

这些仅在专用类使用。如果您使用的是包括办法我提到的,你省略这些,你会在你的时间表,code。他们表示一个属性/方法是否是从类的外访问。如果你是刚刚起步的,把一切都公开。你可以不用担心内存优化后,当你准备应对它。

These are only to be used in dedicated classes. If you're using the include method I mentioned, you omit these as you would in your timeline code. They denote whether a property/method is accessible from "the outside" of the class. If you're just starting out, leave everything Public. You can worry about memory optimizations later when you're ready to tackle it.

?3:如何进行访问类的按钮

想到的图像显示的最简单的方法是作为一个文件夹结构。

The easiest way to think of the DisplayList is as a folder structure.

您可以写

C:/用户/我

其中在图像显示看起来更象

Which in the DisplayList would look more like

C.Users.Me

当然,我们并没有所谓的 C 根的位置,和你的对象很可能被称为 PSD_one 则myButton ,所以我们可以如下改写......

Of course, we don't have a root location called C, and your objects are probably called PSD_one and myButton, so we can rewrite it as follows...

root.PSD_one.myButton

这是假设你确实有这些属性pre定义上的对象,在动态创建对象,这不会发生。在这种情况下,你最好绾你的命令,如下:

This is assuming you actually have those properties pre-defined on the objects, which does not happen when dynamically creating objects. In those cases, you'd string together your commands, as follows:

root.getChildByName(PSD_one)。getChildByName(myButton的)

当你写一个类,它就像你的网络上创建一个新的计算机。有这个类,你的MainTimeline之间没有直接的关系。你需要将它们连接起来。

When you write a Class, it's like creating a new computer on your network. There is no direct relationship between that Class and your MainTimeline. You need to connect them.

如果您实例DisplayListObject(如Sprite),并将其添加到舞台上时,对象会自动获得一些性能,如其中,直到它被父实际上是。这些属性的类可以从内部自行设置参考连接到 MainTimeline 和访问对象就可以了。

If you instance a DisplayListObject (such as a Sprite) and add it to the stage, the object automatically gains a few properties like stage, root, and parent which until it was parented were in fact null. These are properties that the class can reference from inside itself to connect to the MainTimeline and access objects on it.

相反,如果你想,你可以传递一个参考的阶段,从构造函数参数的类,如下所示:

Conversely, if you wanted to you could pass a reference to the stage to the class from the constructor arguments, as follows:

VAR富:= MySprite的MySprite的新的(阶段);

这篇关于如何在AS3的类文件的功能链接到的DocumentClass和影片剪辑?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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