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

2019/2/28

How to make detection area of sprites larger than 1.

KWAKamole KWAKamole

2019/2/28

#
I am working on a tetris game, and all of the tetrominos are 4 blocks in size in their respective shapes. However, Greenfoot automatically makes all of the detection areas for boundaries and all of the "touching methods" the one block in the middle of the entire sprite, disallowing me to make them stack on one another. How can I either 1) change their boundaries(to make them stop when they touch) or 2) protect for it some other way?
Super_Hippo Super_Hippo

2019/2/28

#
Try to have four objects forming one tetromino. When moving or turning the tetromino, you can check each object for intersections individually and move/turn/stop them together.
danpost danpost

2019/2/28

#
KWAKamole wrote...
I am working on a tetris game, and all of the tetrominos are 4 blocks in size in their respective shapes. However, Greenfoot automatically makes all of the detection areas for boundaries and all of the "touching methods" the one block in the middle of the entire sprite, disallowing me to make them stack on one another. How can I either 1) change their boundaries(to make them stop when they touch) or 2) protect for it some other way?
An easy way is to use 4 square blocks for each tetromino. These blocks can be used for collision detection for the unit as a whole. I would suggest that the Block class be a class defined within the Tetromino class. That will allow a Tetromino to create its own blocks and the blocks will "belong" to the tetromino that created it. The following is not the exact or actual code, but only shows the general format of the inner class I am suggesting:
public class Tetromino extends Actor
{
    private Block[] blocks = { new Block(), new Block(), new Block(), new Block() };
    ...
    
    public void act()
    {
        // action code here;
        // any move or turn will require all blocks to be repositioned  and their return value checked
        // to verify no collisions or boundary crossings;
        // upon either, the move or turn will need to be undone
    
    public class Block extends Actor
    {
        ...
        
        public boolean setPosition()
        {
            Actor myTetromino = Tetromino.this;
            setLocation(myTetromino.getX(), myTetromino.getY());
            setRotation(myTetromino.getRotation());
            // move to assigned position with respect to tetromino
            return isTouching(Block.class) || isAtEdge();
        }
    }
}
You need to login to post a reply.