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).
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);
}
}
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);
}
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:
TypeError: Error #1009: Cannot access a property or method of a null object reference. at ControlPanel() at Main/added()
Also I commented out the controlPanel lines in Main and just did a trace(this); and it returned
[object Main]
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?
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);
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?
ControlPanel
, and you have also got a .as file that contains a class calledControlPanel
? – shanethehat