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

2024/6/5

I have a problem with my code

Nordrasil Nordrasil

2024/6/5

#
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;
        } 
        }
    }
}
danpost danpost

2024/6/6

#
Make line 27:
if (getImage() == image1) checkHit();
Nordrasil Nordrasil

2024/6/6

#
It's working thank you so much danpost !
Nordrasil Nordrasil

2024/6/6

#
I have one last question to ask you. I have a brick class that works almost the same as block. That is to say, it just outputs a coin instead of an item. I had the same problem as with the block at the beginning. So, I used the same solution that you proposed to me for the brick class. But now when Mario hits the brick, the new image is not displayed and there are no coins coming out.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Brick here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Brick extends Solid
{
    private GreenfootImage image1;
    private GreenfootImage image2;
    private boolean bumped;
    public Brick()
    {
        image1 = new GreenfootImage("brick.png");
        image2 = new GreenfootImage("SpentBlock.png");
    }
    /**
     * Act - do whatever the Brick wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        if (getImage() == image1) 
        {
            checkHit();
        }
    }
    public void checkHit()
    {
        if (getOneObjectAtOffset (0, getImage().getHeight()/2+1, Mario.class)!= null)
        {
            setImage(image2);
            bumped = true;
        }
        if (bumped)
        {
            getWorld().addObject(new Coin(), getX(), getY()-26);
            bumped = false;
        }
    }
}
danpost danpost

2024/6/7

#
Nordrasil wrote...
I have a brick class that works almost the same as block. That is to say, it just outputs a coin instead of an item. I had the same problem as with the block at the beginning. So, I used the same solution that you proposed to me for the brick class. But now when Mario hits the brick, the new image is not displayed and there are no coins coming out. << Code Omitted >>
You are not setting the image in the constructor of the class like you did in the Block class.
Nordrasil Nordrasil

2024/6/7

#
You're right. It's working now. Thanks so much for everything.
You need to login to post a reply.