I just started programming OOP and I'm running into a scope problem.
In the following project, I have a masterClass called App. The App-class has Screens:Screen-class and a Navigation-class as it's children. From the navigation class I want to control which screens will be displayed. I don't know how to do this...
Please check the code to fully understand my intentions
Your help is really appreciated, I'd love to really learn programming and not just a dirty solution :) but all suggestions are welcome!
// Main Class //
public class App extends Sprite
{
private var screens:Array;
private var screen1:Screen;
private var screen2:Screen;
private var screen3:Screen;
private var screen4:Screen;
public var currentScreen:String;
//
private var navigation:Navigation;
public function App()
{
init();
}
private function init():void {
buildScreens();
buildNavigation();
}
private function buildScreens():void {
screen1 = new Screen();
screen1.name = 'startScreen';
currentScreen = screen1.name;
addChild(screen1);
screen2 = new Screen();
screen2.name = 'irrelevantA';
screen3 = new Screen();
screen3.name = 'irrelevantB';
screen4 = new Screen();
screen4.name = 'irrelevantC';
screens = new Array(screen1, screen2, screen3, screen4);
}
private function buildNavigation():void {
navigation = new Navigation(screens);
}
}
// Screen Class //
public class Screen extends Sprite
{
public function Screen()
{
// creates a new screen
}
}
// Navigation Class //
public class Navigation extends Sprite
{
private var buttons:Array;
public function Navigation(screens:Array)
{
addButtons(screens);
}
private function addButtons(screens:Array):void {
buttons = new Array();
for each(var screen:Screen in screens) {
var button:Button = new Button();
button.link = screen.name;
button.addEventListener(MouseEvent.MOUSE_DOWN, mouseDown);
buttons.push(button);
}
}
private function mouseDown(e:MouseEvent):void {
// THIS IS WHAT MY QUESTION IS ABOUT: How should I talk to the parent class in an OOP correct way?
// and how can I add and remove a screen in the App class from here?
// Here some of my tries
// I don't think using parent to get there is a good way because next time it might be; parent.parent.parent
trace(e.target.parent.parent.currentScreen);
this.parent.currentScreen;
stage.App.currentScreen;
App.currentScreen;
//---------------------------------
}
}
// Button Class //
public class Button extends Sprite
{
public var link:String;
public function Button()
{
// creates a new button
}
}
If you directly access parent classes from child objects, you create strong coupling - which is exactly what you don't want in a well-built system. It is best not to access the application object directly, but to use event listeners and custom events to promote changes from e.g. the navigation.
Here's an example. First, create a custom event:
public class MyCustomEvent extends Event {
public static const MENU_ITEM_SELECTED : String = "MENU_ITEM_SELECTED";
public var selectedItem:String;
}
Then, let the navigation dispatch it, when a button is clicked:
public class Navigation extends Sprite () {
// ...
private function onButtonClicked(ev:Event) : void {
ev.stopPropagation();
var custEvent:MyCustomEvent = new MyCustomEvent(MyCustomEvent.MENU_ITEM_SELECTED);
custEvent.selectedItem = ev.target.name;
this.dispatchEvent (custEvent);
}
// ...
}
Finally, let the application handle the custom event and bring up a different screen:
public class App {
// ...
public function createNavigation () : void {
navigation = new Navigation ();
navigation.addEventListener (MyCustomEvent.MENU_ITEM_SELECTED, onMenuItemSelected);
// ... more stuff happening
}
// ...
private function onMenuItemSelected (ev:MyCustomEvent) : void {
switchToScreen (ev.selectedItem);
}
private function switchToScreen (name:String) : void {
// choose screen by name, etc.
}
}
For all of this, neither the screen, nor the navigation have to know anything about any other objects involved, so you can easily replace each one without breaking the rest of the system.
You basically want to communicate downward (parent to child) by passing in references as arguments (as you're doing with the screens array) and upwards (child to parent) by calling public functions.
So, in your case something like this:
App class:
private function buildNavigation():void {
navigation = new Navigation(this, screens);
}
//etc
public function changeScreen(newScreen:int):void{
//Your logic for adding/removing screens goes here
}
Navigation class:
private var app:App
public function Navigation(app:App, screens:Array)
{
this.app = app
addButtons(screens);
}
private function addButtons(screens:Array):void {
buttons = new Array();
for each(var screen:Screen in screens) {
var button:Button = new Button();
button.link = screen.name;
button.addEventListener(MouseEvent.MOUSE_DOWN, mouseDown);
buttons.push(button);
}
}
private function mouseDown(e:MouseEvent):void {
app.changeScreens(2);
}
}
Obviously, change the implementation to suit your needs (for example, now that you have a reference to the App class, consider if you need to pass in a separate reference to the screens array or not) - this is just an example of how you can communicate.
Related
I am brand new to AS3/Flash and am learning about event handling. I have a "Bell" class which dispatches an event, and if I add an event listener to an instance of that Bell class, it works fine. That's not what I want, though. I've got another class, Pet, which I would like to have listening for the Bell. I see the trace from the bell, but not the one for the pet.
Here is the code for the Bell:
public class Bell extends EventDispatcher {
public static const BELL_RING:String = "bellRing";
public static const RING_INTERVAL:int = 1500;
private var ringIntervalID:int;
public function Bell()
{
ringIntervalID = setInterval(ringBell,RING_INTERVAL);
}
public function ringBell():void {
trace("RINGING THE BELL");
dispatchEvent(new Event(Bell.BELL_RING));
}
}
...and for the Pet:
public class VirtualPet {
private var senseOfHearing:EventDispatcher = new EventDispatcher();
public function Pet(name:String):void
{
senseOfHearing.addEventListener(Bell.BELL_RING,heardBell);
}
public function heardBell(e:Event):void {
trace("Pet hears bell ringing");
}
}
...and for the Main Class:
public class VirtualZoo extends Sprite
{
public function VirtualZoo()
{
var bell:Bell = new Bell();
var pet:VirtualPet = new VirtualPet('Stan');
}
}
Any help is much appreciated! Events are properly imported and there are no compiler errors.
There are a few ways to do this, this is the most straight-forward...
public function Pet(name:String, bell:Bell):void {
bell.addEventListener(Bell.BELL_RING, heardBell)
}
Then:
myBell = new Bell()
myPet = new Pet("dog", myBell)
Or...
if Bell is a child of Pet, then you can bubble the event up the DisplayList and capture it in Pet (but I doubt this is the case).
Or...
You can use a proxy class, like your senseOfHearing and pass that instance to both Bell and Dog and use it to dispatch events between the two.
EDIT
Based on your comment:
var bells:Array = [];
// add some bells
var pet:Pet = new Pet("Marvin", bells);
public function Pet(name:String, bells:Array):void {
for (var i:int = 0; i < bells.length; i++) {
bells[i].addEventListener(Bell.BELL_RING, heardBell);
}
}
I'm trying to make a level select screen, and instead of copy and pasting a bunch of level select buttons onto my level select screen, I wanted to try to create and position the buttons using code for a little more flexibility in the future. I've run into a few problems while doing this...the main problem being that my custom navigation events, for whatever reason, fail to bubble up to my main document class from my button class.
I've used over 20 navigation events in my project, so I'm pretty certain the problem is not my method of doing navigation events as a whole, but the problem is specific to using a button class. Or something else I'm missing. Anyhow, I'll post the code below. I performed a few traces to try and figure out what was going on. My custom navigation events class is creating the proper event, then my button class is dispatching the event, but the the code that listens for this event in my document class never runs. So I'm assuming there's some kind of problem in bubbling the navigation event up to the top. That's my best guess, could be something completely different. If you guys also have different suggestions about how to make my level select screen work better by completely restructuring what I've done, that's fine too...but this has been somewhat of a mystery and I was hoping to get it working this way. Thanks a bunch, here's the relevant code!
---NOTE: If this helps, the hierarchy is DocumentClass --> TitleScreen --> LevelSelectScreen --> LevelSelectButtons
LevelSelectScreen class
public class LevelSelectScreen extends MovieClip {
public var levelSelectButtons:LevelSelectButtons;
public function LevelSelectScreen() {
for (var i:int = 1; i<=2; i++)
{
levelSelectButtons = new LevelSelectButtons(i);
addChild(levelSelectButtons);
}
}
}
LevelSelectButtons Class
public class LevelSelectButtons extends SimpleButton {
public var levelNumber:int;
public var levelSelectScreen:LevelSelectScreen;
public function LevelSelectButtons(i) {
x = 200;
y = 100 + 50*i;
addEventListener(MouseEvent.CLICK,LevelSelectClicked,false,0,true)
levelNumber = i;
}
public function LevelSelectClicked(mouseEvent:MouseEvent):void
{
if (levelNumber == 1)
{
dispatchEvent( new NavigationEvent( NavigationEvent.START ) );
}
if (levelNumber == 2)
{
dispatchEvent( new NavigationEvent( NavigationEvent.STAGE2 ) ); }
}
}
}
NavigationEvent class
package
{
import flash.events.Event;
public class NavigationEvent extends Event
{
public static const START:String = "start";
public static const LEVELSELECTSCREEN:String = "levelselectscreen";
public static const STAGE2:String = "stage2";
public function NavigationEvent( type:String, bubbles:Boolean = false, cancelable:Boolean = false )
{
super( type, bubbles, cancelable );
trace(type);
}
public override function clone():Event
{
return new NavigationEvent( type, bubbles, cancelable );
}
public override function toString():String
{
return formatToString( "NavigationEvent", "type", "bubbles", "cancelable", "eventPhase" );
}
}
}
If you want the events to bubble up you need to set the bubbles property to true.
dispatchEvent( new NavigationEvent(NavigationEvent.START, true) );
lots of help from you guys :). My next question is here :).
I have timer in class MyTimer.as and Thief1_mc.as movie clip.
How can I addChild(Thief1_mc) on the stage from MyTimer? Everything looks simple, the only problem is "stage" property. MyTimer class cannot send "stage" as an argument because is not on the stage itself. I tried adding MyTimer on the stage in Main class like addChild (MyTimer), the trace says MyTimer is on the stage but I still cannot pass the stage argument to the Thief1_mc. I need this argument to be sent because the class Thief1_mc has to add itself on the stage using the property "stage".
The code:
public class Thief1_mc extends MovieClip
{
//this variable type Stage will contain stage
private var stageHolder:Stage;
public function Thief1_mc()
{
//constructor
}
//function that creates this object with passed "stage" argument from the caller
public function createItself(st):void
{
//variable that contain the stage so I can use this argument anywhere in the class
stageHolder = st;
//i have to refer to the stage by passed "st" parameter to create this object
stageHolder.addChild(this);
//initial position
this.x = 380;
this.y = 230;
}
}
}
MyTimer class and "_thief1.createItself(stage)" caller with stage arument
public class MyTimer extends Sprite
{
private static var nCount:Number = 120;
private static var currentCount:Number;
private static var _timer:Timer = new Timer(1000,nCount);
private static var _timerDispather:Timer;
private static var _thief1:Thief1_mc = new Thief1_mc ;
public function MyTimer()
{
// constructor code
}
//another timer
private static function increaseInterval(interval:int):void
{
_timerDispather = new Timer(interval);
_timerDispather.addEventListener(TimerEvent.TIMER, onUpdateTimeAnotherTimer);
_timerDispather.start();
}
//another timer;
private static function onUpdateTimeAnotherTimer(e:Event):void
{
_thief1.createItself(stage);//the most important part
}
public static function activateTimer():void
{
currentCount = nCount;
_timer.addEventListener(TimerEvent.TIMER, onUpdateTime);
_timer.start();
}
public static function deactivateTimer():void
{
_timer.removeEventListener(TimerEvent.TIMER, onUpdateTime);
_timer.stop();
_timer.reset();
currentCount = nCount;
//another timer
_timerDispather.removeEventListener(TimerEvent.TIMER, onUpdateTimeAnotherTimer);
_timerDispather.stop();
_timerDispather.reset();
}
private static function onUpdateTime(e:Event):void
{
currentCount--;
if (currentCount == 0)
{
_timer.removeEventListener(TimerEvent.TIMER, onUpdateTime);
_timer.stop();
_timer.reset();
}
}
}
}
Your code is backwards in a few places. It does not flow very nicely, and the issues you having now are going to be tenfold at some stage in your project.
Firstly, your MyTimer class should not be extending Sprite. It does not get rendered and does not represent anything graphically.
Secondly, your timer class is taking on more than it should. I would revise it to manage your timers and timer events only. Create a list within your timer class that will contain some other elements which can have a method triggers to do other stuff, like creating and adding Thief1_mc.
A simplified version of this might look like:
public class Updater
{
private var _timer:Timer;
private var _toUpdate:Vector.<IUpdatable> = new Vector.<IUpdatable>();
public function Updater()
{
_timer = new Timer(60);
_timer.start();
_timer.addEventListener(TimerEvent.TIMER, _notifyUpdatables);
}
private function _notifyUpdatables(e:TimerEvent):void
{
for each(var i:IUpdatable in _toUpdate)
{
i.update(this);
}
}
public function addUpdatable(updatable:IUpdatable):void
{
_toUpdate.push(updatable);
}
public function removeUpdatable(updatable:IUpdatable):void
{
var index:int = _toUpdate.indexOf(updatable);
if(index >= 0) _toUpdate.splice(index, 1);
}
}
From here we need to create an interface which we will implement on classes that we want to be able to call update() on each time the Updater timer ticks:
public interface IUpdatable
{
function update(updater:Updater):void;
}
Now what I would do in your case is have a class that does extend Sprite and manages the graphics of the application / game. It will implement the IUpdatable interface like I have described and also could deal with adding your Thief1_mc:
public class View extends Sprite implements IUpdatable
{
public function update(updater:Updater):void
{
// Create a Thief.
var thief:Thief = new Thief();
updater.addUpdatable(thief);
addChild(thief);
}
}
Your Thief can take advantage of the IUpdatable interface we have and be added to the update queue when it is created, as I've done above. Just to have a complete example, here's the Thief class:
public class Thief extends Sprite implements IUpdatable
{
public function update(updater:Updater):void
{
// Make this Thief so some stuff.
//
}
}
And here's how you can tie it all together in your document class:
public class App extends Sprite
{
private var _updater:Updater;
private var _view:View;
public function App()
{
_updater = new Updater();
_view = new View();
_updater.addUpdatable(_view);
stage.addChild(_view);
}
}
This might be a bit overwhelming at first, and seem like a lot of work, but you now have a nice clean foundation to add more elements easily.
Rather than having your one class trying to manage timers and add Thieves like you had initially, we've separated the responsibilities and tightened up the flow a little. The Updater deals purely with storing IUpdatable instances and calling their update() method each time the Timer within it ticks. The View class manages the graphics and will also add a Thief each time it is updated via the Updater. The View was added to the stage initially, so all you need to do is add the thieves into itself to have them show up.
If you take this and restructure how the timers work within Updater, I think you'll be where you wanted but with a significantly better understanding and structure.
I have this class structure below
public class Main extends Sprite
{
private var myObject:MyObject;
private var mySecondObject:MySecondObject;
private var myThirdObject:MyThirdObject;
public function Main()
{
myObject = new MyObject();// also extends sprite
addChild(myObject );
mySecondsObject = new MySecondObject(this);
addChild(mySecondsObject);
myThirdObject = new MyThirdObject(this);
addChild(myThirdObject);
}
public function getMyObject():MyObject
{
return myObject;
}
}
public class MySecondObject extends Sprite
{
private var _main:Main;
private var _myObject:MyObject;
public function MySecondObject(main:Main)
{
_main = main;
_myObject = _main.getMyObject();
// do some stuff with _myObject, add on enter frame listener etc...
}
public function start(): { /* do some stuff */}
public function stop(): { /* do some stuff */}
}
public class MyThirdObject extends Sprite
{
private var _main:Main;
private var _myObject:MyObject;
public function MyThirdObject(main:Main)
{
_main = main;
_myObject = _main.getMyObject();
// do some stuff with _myObject, add on enter frame listener etc...
}
public function start(): { /* do some stuff */}
public function stop(): { /* do some stuff */}
}
The problem that appears is the sprite of MyObject dissapears when I do this with MySecondObject and MyThirdObject
MySecondObject.stop();
MyThirdObject.start();
MyObject still exists in memory but I cannot see it on the stage. WTF ??? :)
To make it clear I dont remove anything from the stage with removeChild().
The problem I try to solve is I want to have accessors to update state of MyObject from MySecondObject and MyThirdObject
Problem solved. I changed to add all children on stage like this.stage.addChild(child) instead of this.addChild(child) so now it works and it show all objects on the stage, after start/stop
In an attempt to organize my code, I'm trying to split up my (lengthy) main controller class into separate files, but my new files must still have access to the variables and functions of the main controller class.
I'm trying to cut and paste code from my controller class into a new class/file, allowing the controller class to call the new class, and allowing the new class to have access to the controller class's properties and function.
Assuming I'm not totally bludgeoning appropriate design patterns, below is my unsuccessful attempt at accomplishing this task:
package
{
import flash.display.Sprite;
import flash.events.Event;
public class Test extends Sprite
{
public var myString:String;
public function Test()
{
if (stage)
init(null);
else
addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(evt:Event):void
{
if (hasEventListener(Event.ADDED_TO_STAGE))
removeEventListener(Event.ADDED_TO_STAGE, init);
/////////////// MOVE COMMENTED CODE TO NEW FILE ///////////////////////
//
// //Assign The String A Value
// myString = "Hello world";
//
// //Draw A Blue Square
// var sq:Sprite = new Sprite();
// sq.graphics.beginFill(0x0000FF);
// sq.graphics.drawRect(10, 10, 100, 100);
// sq.graphics.endFill();
// super.addChild(sq);
//
// //Call Tracer Function
// tracer();
//
//////////////////////////////////////////////////////////////////////
//Call pasted method in NewFile.as
NewFile.myNewFunction(); // <- this doesn't work
}
public function tracer():void
{
trace(myString);
}
}
}
new file doesn't have access to the Controller class - doesn't work. how can i write the new file so that it does have access to the properties, functions, stage, etc. of the Controller class, as if its code was never removed and is still in its original place.
package
{
public class NewFile
{
public static function myNewFuntion():void
{
//Assign The String A Value
myString = "Hello world";
//Draw A Blue Square
var sq:Sprite = new Sprite();
sq.graphics.beginFill(0x0000FF);
sq.graphics.drawRect(10, 10, 100, 100);
sq.graphics.endFill();
super.addChild(sq);
//Call Tracer Function
tracer();
}
}
}
public class MainClass extends Sprite
{
private var subClass:SubClass;
public function MainClass
{
var controller:Controller = new Controller();
subClass = new SubClass(controller);
addChild( subClass );
}
private function init():void
{
subClass.doWhatever();
}
}
public class Controller
{
public function doThis():void
{
}
public function doThat():void
{
trace("controller do that...");
}
public function doSomethingElse():void
{
}
}
public class Subclass extends Sprite
{
private var controller:Controller;
public function Subclass(controller:Controller)
{
this.controller = controller;
trace( "new Subclass instance!" );
}
public function doWhatever():void
{
controller.doThat();
}
}
This code
//Call Tracer Function
tracer();
is not going to work, since tracer is not a static method.
This one :
super.addChild(sq);
won't work either, since NewFile doesn't inherit any class ; super() calls the homonyme method of the mother class in an INHERITANCE relation. What you should do there is more probably a COMPOSITION or AGGREGATION relation : newFile IS NOT a Controller, but Controller HAS a newFile.
It's difficult to know exactly what is wrong if you do not give us an error message.
On the design aspect, I have to agree with PatrickS. You might want to check the composite pattern, which could be what you need there.