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

2019/1/16

Logic errors(?). boomArea no checking to see if bomb has hit class

1
2
Notted Notted

2019/1/16

#
What I want to happen is that every time a bomb hits this area, the game just stops and restarts. While the game stopping action works, it's not as it should. It's stopping all the time, and my game is unplayable due to that.
private void checkGameEnd() 
    {
        if (bombBoom(!true))
        {
        }
        else
        {
            
        Greenfoot.stop();
       
        }
    }
Is there anything I'm doing wrong?
danpost danpost

2019/1/16

#
Notted wrote...
Is there anything I'm doing wrong?
More context (code) is needed to determine what might be going on. You can start with the bombBoom method; however, more will probably be required.
Notted Notted

2019/1/17

#
Here is the entirety of the bombBoom method:
private boolean bombBoom(boolean bombIsHit) 
    {
        Actor bomb;
        bomb = getOneObjectAtOffset(0, 0, Bomb.class);
        if (bomb != null)
        {
           getWorld().removeObject(bomb);
           bombIsHit = true;
        }
        return bombIsHit;
    }
All it does is return a boolean (bombIsHit). That's it. It (along with checkGameEnd()) is inside of the class boomArea. Do I need to put another condtion where if the bomb hits the area, it will excute the code?
danpost danpost

2019/1/17

#
Notted wrote...
Here is the entirety of the bombBoom method: << Code Omitted >> All it does is return a boolean (bombIsHit). That's it. It (along with checkGameEnd()) is inside of the class boomArea. Do I need to put another condtion where if the bomb hits the area, it will excute the code?
I do not believe another condition is required. I will get to the main problem below. First, I want to mention that you do not need to pass a boolean to the bombBoom method. The following should be sufficient:
private boolean bombBoom()
{
    boolean hit = isTouching(Bomb.class);
    removeTouching(Bomb.class);
    return hit;
}
Okay, I think the main problem is in your checkGameEnd method. I probably would have figured it out before, had it not been for the parameter argument in the bombBoom call. You have the Greenfoot.stop command in the else block, which executes when not hitting a bomb. At any rate, you would not want to stop the running of the scenario (which that command does). You just want to create a new world object and set it to be the active world:
if (bombBoom()) Greenfoot.setWorld(new MyWorld());
danpost danpost

2019/1/17

#
You could remove the bombBoom method altogether using the following:
public void checkGameEnd()
{
    Actor bomb = getOneObjectAtOffset(0, 0, Bomb.class);
    if (bomb != null)
    {
        while (intersects(bomb)) bomb.act(); // or, Greenfoot.delay(30); // or nothing here
        Greenfoot.setWorld(new MyWorld());
    }
}
Notted Notted

2019/1/17

#
The game does not seem to end whenever the bomb hits the boomArea. Is likely that the bomb is disappearing too fast to check to see that there is a bomb there?
danpost danpost

2019/1/17

#
Notted wrote...
The game does not seem to end whenever the bomb hits the boomArea. Is likely that the bomb is disappearing too fast to check to see that there is a bomb there?
What are your codes, now? (you may want to provide your world class, boomArea class and Bomb class codes for review)
Notted Notted

2019/1/17

#
Here they are: MyWorld:
public class MyWorld extends World
{
    private bucket playerBucket;
    public bucket getABucket()
    {
        return playerBucket;
    }
    /**
     * The world of Kaboom. Makes buckets, bombs
     * 
     */
    public MyWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(320, 480, 1); 
        playerBucket = new bucket();
        addObject(new Bomb(), 171, 210);
        addObject(new boomArea(), 171, 465);
        addObject(playerBucket, 155, 324);
    }
}
boomArea:
public class boomArea extends Actor
{
    /**
     * A booming area. Makes booms explode.
     * 
     */
    public void act() 
    {
        bombBoom();
        checkGameEnd();
    }    
    
    private boolean bombBoom()
    {
        boolean hit = isTouching(Bomb.class);
        removeTouching(Bomb.class);
        return hit;
    }
    
    private void checkGameEnd() 
    {
        if (bombBoom())
        {
            Greenfoot.setWorld(new MyWorld());
        }
    }
}
Bomb:
public class Bomb extends Actor
{
    /**
     * Act - do whatever the Bomb wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
       fall(4);
    }
   
    private int fall(int speed) 
    {
        setLocation(getX(), getY() + speed);
        return speed;
    } 
}
danpost danpost

2019/1/17

#
Remove line 9 from boomArea code.
Notted Notted

2019/1/17

#
Wow. Ok, it works. I don't know why. Could you tell me?
danpost danpost

2019/1/17

#
Notted wrote...
Wow. Ok, it works. I don't know why. Could you tell me?
Look at what was removed (line 9 above). It removes the bomb without setting a new world active. So, when checkGameEnd executes (which does lead to a new world), there is no bomb present. That is why I suggested the revised checkGameEnd method without a bombBoom method.
Notted Notted

2019/1/17

#
Ok. Now I need the game to check how many bombs I have. I already have a method for that, but now I need to check to see if I collect a number of bombs, then the game will end.
private boolean checkGameObjs()
    {
        if (numOfBombs == 100)
        {
        
        }
    }
Do I need to have the collectBomb method called inside this method in order to check to see if the number of bombs is equal to 100?
danpost danpost

2019/1/17

#
Notted wrote...
Ok. Now I need the game to check how many bombs I have. I already have a method for that, but now I need to check to see if I collect a number of bombs, then the game will end. << Code Omitted >> Do I need to have the collectBomb method called inside this method in order to check to see if the number of bombs is equal to 100?
No, you will not. Actually, it would make more sense to call the checkGameObjs method from inside the collectBomb method. Call it immediately after a bomb is collected. That way you are only checking for game over once each time the score is bumped (instead of all the time).
Notted Notted

2019/1/17

#
danpost wrote...
Notted wrote...
Ok. Now I need the game to check how many bombs I have. I already have a method for that, but now I need to check to see if I collect a number of bombs, then the game will end. << Code Omitted >> Do I need to have the collectBomb method called inside this method in order to check to see if the number of bombs is equal to 100?
No, you will not. Actually, it would make more sense to call the checkGameObjs method from inside the collectBomb method. Call it immediately after a bomb is collected. That way you are only checking for game over once each time the score is bumped (instead of all the time).
My collectBomb method has a return thing called numOfBombs, which is how many bombs the bucket has. I just need to make some sort of reference to it in the checkGameObjs method so that the method has a correct number of bombs to check to see that if game is complete. The game will stop if the game is complete.
danpost danpost

2019/1/17

#
You should not pass field values back and forth among methods in a class where that field is defined. All fields, and their values, are directly accessible to all (non-static) methods in that class without having to pass them around. Somewhere in your code, you increment the numOfBombs field. On the next line check its value (or call the method that does that -- without passing its value to it).
There are more replies on the next page.
1
2