闪光CS5显示不同的"屏幕"在舞台上的影片剪辑 [英] Flash CS5 Displaying Different "screens" on the stage as movieclips

查看:214
本文介绍了闪光CS5显示不同的"屏幕"在舞台上的影片剪辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个线程到一个新的水平,现在做一个Main.as类来显示不同​​的屏幕我的游戏。对于现在的我只有1画面叫做ControlPanel控制,但是这最终将有多个级别(每个级别将是一个单独的屏幕)和水平选择屏幕等补充。所以在这一点上,我失去了控制权如何给东西进入阶段(换句话说...它变得非常厚,多层次,这个菜鸟的大脑超负荷笑)。

Taking this thread to the next level and now making a Main.as class to display different "screens" of my game. For right now I only have 1 screen called ControlPanel, but this will eventually have multiple levels (each level will be a separate screen) and a level selection screen, etc. added. So at this point I'm losing control over how to give things access to the stage (in other words... it's getting really thick with multiple levels and this noob's brain is being overloaded lol).

要开始了,我把我所有的图形是舞台上的默认(按钮,指示灯,仪表,成绩文本等),并创建了一个新的符号(影片剪辑),我叫ControlPanel控制和检查关出口为ActionScript与ControlPanel控制的类名。所以,现在我的游戏的FLA阶段是黑色的,我把它的文档类Main.as.它看起来是这样的:

To start off, I took all of my graphics that were on the stage by default (buttons, lights, meters, score text, etc.) and created a new symbol (MovieClip) that I called ControlPanel and checked off "Export for ActionScript" with a class name of ControlPanel. So now my games fla stage is black and I made it's document class Main.as. which looks like this:

public class Main extends MovieClip {

    public var controlPanel:ControlPanel;

    public function Main() {
        addEventListener(Event.ADDED_TO_STAGE, added);
    }

    private function added(evt:Event):void {
        removeEventListener(Event.ADDED_TO_STAGE, added);

        controlPanel = new ControlPanel(this);
        addChild(controlPanel);
    }
}

跑在我的控制面板的屏幕右侧​​弹出到屏幕这个完美工作。当然,所有的按钮没有工作,这是下一个步骤。所以我修改我的老Game.as现在被称为ControlPanel控制和读取,像这样:

Running this worked perfectly in that my Control Panel screen popped right onto the screen. Of course all the buttons didn't work yet but that is the next step. So I modified my old Game.as to now be called ControlPanel and read like so:

public class ControlPanel extends MovieClip {
    private var docRef:Main;

    private var _player:Player;
    private var _controller:Controller;

    public function ControlPanel($docRef:Main):void {
        this.docRef = $docRef;

        addEventListener(Event.ADDED_TO_STAGE, added);
    }

    private function added(evt:Event):void {
        removeEventListener(Event.ADDED_TO_STAGE, added);

        _player = new Player(docRef);
        _controller = new Controller(_player, docRef);

        addChild(_player);
    }

现在,这是我加入Player类和我的Controller类。他们基本上有类似的妆,所以我只是要在这里展示的Player.as类:

Now this is adding my Player class and my Controller class. They basically have a similar makeup so I'm just going to show the Player.as class here:

public class Player extends MovieClip {
    private var docRef:Main;
    private var _lights:uint;

    public function Player($docRef:Main):void {
        this.docRef = $docRef;

        addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
    }

    private function lightsOut():void {
        switch(_lights) {
        case 1:
        docRef.controlPanel.greenLight1.visible=false;
        break;
        case 2:
        docRef.controlPanel.greenLight2.visible=false;//works
        break;
        case 3:
        docRef.controlPanel.greenLight3.visible=false;//works
        break;
        }
    }

现在的问题是,现在我尝试和测试它,我不再让控制面板的图形,我是看到过,但我现在得到的错误消息:

The problem is, now that I try and test it, I'm no longer getting the Control Panel graphic that I was seeing before, but I'm now getting the error message:

类型错误:错误#1009:无法访问空对象引用的属性或方法。   在ControlPanel控制()   在主/加()

TypeError: Error #1009: Cannot access a property or method of a null object reference. at ControlPanel() at Main/added()

此外,我注释掉在主要的ControlPanel线,只是做了跟踪(这一点);它返回

Also I commented out the controlPanel lines in Main and just did a trace(this); and it returned

[对象主要]

难道不应该是目的舞台或类似的东西?没试过之前,所以我不知道它曾经是。我上哪儿去错在这里?

Shouldn't it be object Stage or something like that? Never tried that before so I don't know what it used to be. Where did I go wrong here?

更新:OK,所以我的控制器是不一样的播放器类,但需要两个阶段,并传递给它也许认为实例???那么这里是我有一个控制器和一些需要的事件侦听器:

UPDATE: OK so my Controller isn't just like the Player class but needs instances of both the stage and the view passed to it maybe??? Well here is what I've got for a controller and some of the event listeners that are needed:

public class Controller extends MovieClip {
    private var _model:Object;
    private var _dHandle:Object;
    private var panelView:ControlPanelView; //started playing with this idea but maybe that is the wrong way to go?? Stick with docRef here? or both?

    public function Controller(model:Object, dHandle:Object, $view:ControlPanelView):void {
        this._model = model;
        this._dHandle = dHandle;
        this.panelView = $view;

        docRef.stage.addEventListener(KeyboardEvent.KEY_DOWN, processKeyDown);
        docRef.stage.addEventListener(MouseEvent.MOUSE_DOWN, processMouseDown);
        docRef.ControlPanelView.fireButton.addEventListener(MouseEvent.CLICK, processFirePress);
        docRef.ControlPanelView.cWButton.addEventListener(MouseEvent.CLICK, processCWPress);
        docRef.ControlPanelView.cCWButton.addEventListener(MouseEvent.CLICK, processCCWPress);

正如你所看到的,这是对docRef和View想法之间徘徊思想的大杂烩。我需要有进入阶段我的鼠标和键盘监听,但我也需要访问的按钮ControlPanelView象征。我是否需要通过双方的观点和这里的docRef?如何我保持进入阶段和ControlPanelView图形?

As you can see, this is a hodgepodge of ideas torn between the docRef and the View idea. I need to have access to the stage for my mouse and keyboard listeners, but then I also need access to the buttons in the ControlPanelView symbol. Do I need to pass both the view and the docRef here? How to I maintain access to the stage and the ControlPanelView graphic?

推荐答案

您不能有东西从库,并与同名的类文件导出。当您设置一个库项目,以为ActionScript导出,Flash会创建一个类,名称重新present的对象,所以来然后创建一个类的同名自己会引起冲突。

You can't have something exporting from the library and a class file with the same name. When you set a library item to 'Export for ActionScript', Flash creates a class of that name to represent that object, and so to then create a class of the same name yourself will cause a conflict.

您有两个选择如何继续进行:

You have two options for how to proceed:

继承

您可以使用您为ControlPanel控制为库中的符号code基地创建的类。要做到这一点,首先给它一个不同的名称,比如 ControlPanelBase 。然后你就可以将其指定为您的符号的基类。为此,您在IDE中通过进入你的符号属性和改变从的flash.display.MovieClip 的基类 ControlPanelBase (或任何名称你选择)。点击小绿勾,以确保该类被发现,然后单击确定。

You can use the class that you have created for your ControlPanel as a code base for the symbol in your library. To do this, first give it a different name, say ControlPanelBase. Then you can assign it to be the base class of your symbol. You do this in the IDE by going into your symbol properties and changing the Base class from flash.display.MovieClip to ControlPanelBase (or whatever name you choose). Click the little green tick to make sure the class is found, then click OK.

现在,当您创建使用新ControlPanel控制新ControlPanel控制(这一点)符号,它会创建一个使用图形对象,同时也是code从基类。因为你是继承,则需要更改单词的所有出现私人保护,让他们可以看到任何子类。

Now, when you create a new ControlPanel using the new ControlPanel(this) notation, it will create an object that uses the graphics, but also the code from the base class. Because you are inheriting, you will need to change all occurances of the word private to protected to allow them to be seen by any descendant classes.

分离视图

这是我的preference因为当你停止使用IDE的任何超过产生的资产包,您就不必重新编译你的资产,只要你改变了code。

This is my preference because when you stop using the IDE for anything more than generating asset packs, you avoid having to recompile your assets whenever you change the code.

更改您的库元件的名称更具描述性,如 ControlPanelView 。现在,当您使用创建ControlPanel控制新ControlPanel控制(这一点)也不会附加任何图形,所以你需要做的是自己:

Change your library symbol's name to something more descriptive, like ControlPanelView. Now, when you create your ControlPanel using new ControlPanel(this) it will not attach any graphics, so you will need to do that yourself:

public class ControlPanel extends MovieClip {
    private var docRef:Main;

    private var _player:Player;
    private var _controller:Controller;

    public var view:ControlPanelView; //note that this is public

    public function ControlPanel($docRef:Main):void {
        this.docRef = $docRef;

        addEventListener(Event.ADDED_TO_STAGE, added);
    }

    private function added(evt:Event):void {
        removeEventListener(Event.ADDED_TO_STAGE, added);

        view = addChild(new ControlPanelView()) as ControlPanelView; //add the graphics

        _player = new Player(docRef);
        _controller = new Controller(_player, docRef);

        addChild(_player);
    }
}

现在,来自内部的播放器访问这些灯,你会说

Now, to access those lights from inside Player, you would say

docRef.controlPanel.view.greenLight1.visible=false;

您可以看到,这是成为一个有点拗口,但缩短它,你可以传递一个参考的ControlPanel,而不是主,当你创建的球员,或者甚至可以传递一个参考的观点,如果这是所有你需要跟。

You can see that this is becoming a bit of a mouthful, but to shorten it you could pass a reference to the ControlPanel instead of Main when you create the player, or indeed you could pass a reference to the view if that is all you ever need to talk to.

在ControlPanel控制:

in ControlPanel:

_player = new Player(view);

新的播放器类:

new Player class:

public class Player extends MovieClip {
    private var panelView:ControlPanelView;
    private var _lights:uint;

    public function Player($view:ControlPanelView):void {
        this.panelView = $view;

        addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
    }

    private function lightsOut():void {
        switch(_lights) {
        case 1:
        panelView.greenLight1.visible=false;
        break;
        case 2:
        panelView.greenLight2.visible=false;//works
        break;
        case 3:
        panelView.greenLight3.visible=false;//works
        break;
        }
    }
}


关于你提到的更新,请记住,你的主类和阶段不一样的东西。每个显示对象都有一个属性,它都将指向同样的事情。这意味着,你的听众添加到舞台,之间有 Main.stage view.stage 没有区别的, this.stage (假设控制器是在舞台上)。所以,你的控制器可能是这样的:


Regarding your update, remember that your Main class and the stage are not the same thing. Every display object has a stage property, which will all point to the same thing. That means that to add your listeners to the stage, there is no difference between Main.stage, view.stage and this.stage (assuming the controller is on the stage). So your controller could look like this:

public class Controller extends EventDispatcher {
    private var _model:Player;
    private var panelView:ControlPanelView;

    public function Controller($model:Player, $view:ControlPanelView):void {
        this._model = $model;
        this.panelView = $view;

        panelView.stage.addEventListener(KeyboardEvent.KEY_DOWN, processKeyDown);
        panelView.stage.addEventListener(MouseEvent.MOUSE_DOWN, processMouseDown);
        panelView.fireButton.addEventListener(MouseEvent.CLICK, processFirePress);
        panelView.cWButton.addEventListener(MouseEvent.CLICK, processCWPress);
        panelView.cCWButton.addEventListener(MouseEvent.CLICK, processCCWPress);
    }
}

和在ControlPanel控制,您可以创建这样的:

and in ControlPanel, you would create it like this:

_controller = new Controller(_player, view);

请注意,我还做你的控制器扩展<一href="http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/events/EventDispatcher.html"相对=nofollow> EventDispatcher受而不是影片剪辑。这是很好的做法,从你所需要的最低限度类扩展,因此,如果这永远不会在舞台上,但它会放出来活动,EventDispatcher受的是最低限度。如果它永远不会需要调度事件,只监听器添加到其他人的话,就不需要扩展任何东西。

Note that I've also made your controller extend EventDispatcher rather than MovieClip. It is good practise to extend from the bare minimum class that you need, so if this will never be on the stage but it will put out events, EventDispatcher is the bare minimum. If it will never need to dispatch events, only add listeners to others, then it need not extend anything.

基本上,只有延长影片剪辑,如果你需要的时间线功能或你的类将是基类的东西在库中。对于任何其他标​​准的显示对象应扩展要么雪碧图形,如果你想要添加的孩子,后者如果你只打算绘制矢量图形前者。如果你的对象是永远不会在显示屏树,通常你需要扩展或者EventDispatcher受或什么都没有。

Basically, only extend MovieClip if you need timeline functionality or your class is going to be a Base Class for something in the library. For any other standard display object you should extend either Sprite or Shape, the former if you intend to add children, the latter if you only intend to draw vector graphics. If your object is never going on the display tree, most often you will need to extend either EventDispatcher or nothing at all.

这篇关于闪光CS5显示不同的&QUOT;屏幕&QUOT;在舞台上的影片剪辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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