mercredi 27 janvier 2016

ActionScript game, having trouble implementing decorator class

So, I've created a tile based "match 3" game in ActionScript and there are a few different types of tiles the main tiles are just letter tiles, and then there are special tiles like a bomb for instance and each tile has similar functionality but each one has special functionality.

Right now, I just have a Tile class that handles all of the functionality which is not the way to go. I think a decorator class would be the best route however implementing it, i am running into issues.

So below is how I have it structured, how would I handle storing the tiles in a Vector? and how would I add to stage?

Currently, I store the tiles in a Vector.(I have one Tile class that handles all of the type types which is ugly). If I were to move to a decorator pattern, it seems that I would be storing a Vector of ITile, how would I add "Itiles" which are essentially LetterTiles and BombTiles to stage? I get Type Coercion errors when trying.

Also, the decorator pattern just seems to be basically an interface, which all types would just implement, so for example, if I were to take an existing LetterTile on stage and say it needs to convert to a BombTile, How would I go about changing or updating the current tile into a 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 would do something different than regular letter tile.
    }
}

Hopefully this makes sense, I needed to add as much info as I could without creating a TL;DR

Aucun commentaire:

Enregistrer un commentaire