flash - ActionScript game, having trouble implementing decorator class -


so, i've created tile based "match 3" game in actionscript , there few different types of tiles main tiles letter tiles, , there special tiles bomb instance , each tile has similar functionality each 1 has special functionality.

right now, have tile class handles of functionality not way go. think decorator class best route implementing it, running issues.

so below how have structured, how handle storing tiles in vector? , how add stage?

currently, store tiles in vector.(i have 1 tile class handles of type types ugly). if move decorator pattern, seems storing vector of itile, how add "itiles" lettertiles , bombtiles stage? type coercion errors when trying.

also, decorator pattern seems interface, types implement, example, if take existing lettertile on stage , needs convert bombtile, how go changing or updating current tile bomb tile?

example:

public interface itile {     function create():void;     function remove():void; } 

and basetile.as

public class basetile extends sprite {     public function create():void     {         throw new illegaloperationerror("must override in concrete class");     }      public class remove():void     {         throw new illegaloperationerror("must override in concrete class");     } } 

lettertile.as

public class lettertile extends basetile implements itile {     public override function create():void     {         trace("letter tile created");     }      public override function remove():void     {         trace("letter tile removed");     } } 

bombtile.as

public class bombtile extends basetile implements itile {     public override function create():void     {         trace('bomb tile created');     }      public override function remove():void     {         trace('bomb tile removed');         doexplosion();     }      public function doexplosion():void     {          //this different regular letter tile.     } } 

hopefully makes sense, needed add info without creating tl;dr

like said in comment, isn't decorator pattern, it's abstract class basetile.

to answer questions whether use decorator or base class: need use casting. specifically:

how handle storing tiles in vector?

var tiles:vector.<itile> = new <itile>[];  tiles.push(new bombtile()); 

to check specific tile types in vector use is operator, use as operator cast specific tile type:

for each(var tile:itile in tiles){     if(tile bombtile){         var bombtile:bombtile = tile bombtile;         bombtile.explode();     }else if(tile lettertile){         var lettertile:lettertile = tile lettertile;         lettertile.letter = "a";     } } 

and how add stage?

if have itile, must cast display object:

stage.addchild(tile displayobject); 

if have basetile extends sprite no casting necessary.

if take existing lettertile on stage , needs convert bombtile, how go changing or updating current tile bomb tile?

this depends on want do. can't change object object, can replace in various ways:

var tiles:vector.<itile> = new <itile>[]; tiles.push(new lettertile(), new lettertile(), ...etc);  function replacetile(existingtile:itile, replacementtile:itile):void {     var index:int = tiles.indexof(existingtile);     stage.removechild(existingtile displayobject);     tiles[index] = replacementtile;     stage.addchild(replacementtile displayobject); }  function changetiletype(tile:itile, type:class):void {     if( !(tile type)){         replacetile(tile, new type());     } }  // example: changetiletype(tiles[0], bombtile); 

Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -