This site requires JavaScript, please enable it in your browser!
Greenfoot back
Dkcub17
Dkcub17 wrote ...

2017/3/23

Tetris - Class Composition in Actor

Dkcub17 Dkcub17

2017/3/23

#
Hi everyone! I'm attempting to code my own version of tetris. I'm having trouble creating a tetromino actor: So I have a Tile class which represents just a single block. Then I have a tetromino class which is the superclass for all tetrominos: (A Tetromino is just any configuration of 4 blocks) In the constructor of the tetromino class, I attempt to construct an arrayList of Tiles.
public Tetromino( int initialSpeed, int initialrotateCtr )
       {
          tiles = new Tile[4];
          speed = initialSpeed;
          rotateCtr = initialrotateCtr;
       
          int index = 0;
          while( index < tiles.length )
             {
             tiles[index] = new Tile();
             index++;
              }
       }
Then, I have a subclass under this Tetromino class called "JTetro" that represents one configuration of the array of Tiles. Here's the constructor for the JTetro class:
public JTetro( int initialSpeed, int initialrotateCtr )
       {
          super( initialSpeed, initialrotateCtr );

       }
However, when I add a "JTetro" object to the world, it only adds a single Tile, not all four Tiles. What am I doing wrong?
Super_Hippo Super_Hippo

2017/3/23

#
Line 10 creates a new Tile but doesn't add it to the world. It shouldn't add any tile to the world, only the "tetromino" (subclass) which shouldn't have an image. You don't need to create a new class for every configuration because as you see, it is just calling the constructor in the super class but is not doing anything different as the other ones.
You need to login to post a reply.