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

2019/10/17

How to set image of a class within an actor

PowerDj PowerDj

2019/10/17

#
I'm making a game that's somewhat similar to Tetris, where a player is given blocks and must place them on a grid. Currently, when the user places a block (which is made of individual squares), individual squares of the "placedBlock" class are instantiated to re-construct the block that was placed wherever it was. For example, if the user places a 2x2 block, four squares will be made to re-construct the block. Because each block can have one of four colours, I need the placedBlock squares to share this colour. I've tried using the setImage() method within an actor to change the image for the placedBlock class, but this does not work. All of the squares appear as they should, but they will always be the colour that the placedBlock class is initially set to. Here is the relevant code:
            GreenfootImage buildBlock;

            buildBlock = new GreenfootImage(blockColour + "_b" + ".png");
            
            placedBlock placedBlock = new placedBlock();
            
            placedBlock.setImage(buildBlock);

            if (blockShape.equals("0")){
                world.addObject(new placedBlock(), getX()-13, getY()+13);
                world.addObject(new placedBlock(), getX()-13, getY()-13);
                world.addObject(new placedBlock(), getX()+13, getY()+13);
                world.addObject(new placedBlock(), getX()+13, getY()-13);
            }
Essentially, I need something that will allow me to permanently change the image of the placedBlock class until it needs to be changed again.
danpost danpost

2019/10/17

#
In your code, line 7 only changes the image of the actor created on line 5 -- and this actor is not even being used at all. Each new placedBlock object must have its image changed individually either during or after creation. After creation image changing would involve splitting lines 10 thru 13 up into two lines and adding the image change for each one:
// line 10 changes to
Actor pb = new placedBlock();
pb.setImage(buildBlock);
world.addObject(pb, getX()-13, getY()+13);
// other lines (11 thru 13) similarly
During creation can be done a couple of ways. One is by adding a parameter to the placedBlock constructor to pass the image and have it set by the constructor. The other way is by setting a class field in the placedBlock class with the "to-be" default image; then having the constructor set that image to the new objects.
PowerDj PowerDj

2019/10/17

#
danpost wrote...
During creation can be done a couple of ways. One is by adding a parameter to the placedBlock constructor to pass the image and have it set by the constructor.
Thanks for the reply! It would be best if I could do this in one line, but I'm not sure how I would allow the placedBlock class to accept a parameter. I'm assuming I need to make it so the constructor will be
new placedBlock(blockImage)
but I don't know how this would be done.
danpost danpost

2019/10/17

#
PowerDj wrote...
It would be best if I could do this in one line, but I'm not sure how I would allow the placedBlock class to accept a parameter. I'm assuming I need to make it so the constructor will be
new placedBlock(blockImage)
but I don't know how this would be done.
In your placedBlock class, you would need:
public placedBlock(GreenfootImage img)
{
    setImage(img);
}
PowerDj PowerDj

2019/10/17

#
Just tried this out and it worked perfectly, huge thanks!
You need to login to post a reply.