Hello everyone,
I have a problem with my code. Because I would like my block to only release an item when Mario hits the block and for there to be a new image that replaces this image of the block to indicate that an item can no longer exit the block. When my character hits the block, the new image comes out.
However, when my character hits this same object again, there is still an item that comes out.
Thanks in advance to anyone who will find my mistake.
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Block here. * * @author (your name) * @version (a version number or a date) */ public class Block extends Solid { private GreenfootImage image1; private GreenfootImage image2; private int powerUpChance; private boolean bumped; public Block() { image1 = new GreenfootImage("itemblock.png"); image2 = new GreenfootImage("SpentBlock.png"); setImage(image1); } /** * Act - do whatever the Block wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { checkHit(); } public void checkHit() { if (getOneObjectAtOffset (0, getImage().getHeight()/2+1, Mario.class)!= null) { setImage(image2); bumped = true; } if (bumped) { powerUpChance = Greenfoot.getRandomNumber(10); if (powerUpChance <= 5) { getWorld().addObject(new Mushroom(), getX(), getY()-26); bumped = false; } if (powerUpChance > 5) { getWorld().addObject(new Star(), getX(),getY()-26); bumped = false; } } } }