oop - Pros and Cons of creating a separate class for the GUI (in actionscript) -


this related similar question asked; however, 1 tailored individual project, rather object-oriented programming in general.

i working on version of hangman interesting programming twists. don't need go detail of logic game finished. can run entire game hard-coding variables user input (such guess selection). in process of replacing bits require user interaction trappings of actual game buttons, images, sounds, etc.

i trying figure out whether better have of stuff part of main class, or whether should create class handle all. example, want players able click on on-screen keyboard make guess, each button firing separate event listener call makeguess function. better create buttons direct children of main game class, or should create subclass (called keyboard, example) creates keyboard section of board appropriate events, add keyboard class child main rather pieces? pros , cons of each of these choices?

for record, i'm programming using flashdevelop, nothing timeline me.

i you'd better create @ least keyboard class parse events fired tapping/clicking keys inside, , give callback reference main class, or gamelogic class, can themain.guess(letter); , main class logic come life , process callback. since structure not related game logic, , technically can reused implementing interface callback, can use keyboard elsewhere want have player type letters using mouse, it's better separated main logic.

public class keyboard extends sprite {     public var callback:acceptingkeys; // interface     ... // class implementation, listeners, children , stuff     // , call in there: callback.acceptkey(key); } public interface acceptingkeys {     public function acceptkey(key:string):void; // or whatever type need } 

and main class:

public class main extends sprite implements acceptingkeys {      ...      var keyboard:keyboard;      private function init(e:event=null):void {          ... // other code. it's fd, function exists          keyboard=new keyboard();          keyboard.callback=this;          // past point instances can talk      }      public function acceptkey(key:string):void {          // match interface description          ... // game logic parsing key      } } 

Comments

Popular posts from this blog

python - Subclassed QStyledItemDelegate ignores Stylesheet -

java - HttpClient 3.1 Connection pooling vs HttpClient 4.3.2 -

SQL: Divide the sum of values in one table with the count of rows in another -