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

2019/5/30

Boxworld

Hypergyra Hypergyra

2019/5/30

#
Hello, for school we have to make our own game so I have decided to recreate boxworld. This is the code where I have a problem
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class box here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Box extends Actor
{
    private int amountOfBoxes=0;
    private long startTijd = System.currentTimeMillis();
    private GreenfootImage image1 = new GreenfootImage("apple.png");
    private GreenfootImage image2 = new GreenfootImage("ball.png");
    /**
     * Act - do whatever the box wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        if(rightPlace()){
            changeBox();
            amountOfBoxes++;
        }
        if(rightPlace()==false&&getImage()==image1)
        {
            changeBox2();
            if(amountOfBoxes<0)
                amountOfBoxes--;
        }
        if(getAmountOfBoxes()==4){
            long tijd = System.currentTimeMillis();
            long tijdsduur = (tijd - startTijd) / 1000;
            String message= "winner winner chicken dinner" + " time: " + tijdsduur + " seconds";
            getWorld().showText(message, getWorld().getWidth()/2, getWorld().getHeight()/2);
            Greenfoot.stop();
        }
    }    

    /**
     * checks if the box is in the right place (on a point)
     */
    public Boolean rightPlace()
    {
        Actor actor = getOneObjectAtOffset(0,0,Point.class);
        return actor != null;
    }

    /**
     * returns how many boxes you have placed on the right place
     */
    public int getAmountOfBoxes()
    {
        return amountOfBoxes;
    }

    /**
     * changes the box to indicate that it is in the right place
     */
    public void changeBox()
    {
        Point point = (Point) getOneObjectAtOffset(0,0,Point.class);
        if(point!= null)
            setImage("apple.png");
    }

    public void changeBox2()
    {
        setImage("ball.png");
    }
}
(The images are not set yet so I am using images from Greenfoot) I want that when the box is on the right place amountOfBoxes goes up by one one time. Now when a box is on the right place amountOfBoxes goes up by one, but it doesn't stop going up by one, it keeps going up by one. How can I change it so that when a box is on a right place amountOfBoxes only goes up by one?
danpost danpost

2019/5/30

#
Hypergyra wrote...
I want that when the box is on the right place amountOfBoxes goes up by one one time. Now when a box is on the right place amountOfBoxes goes up by one, but it doesn't stop going up by one, it keeps going up by one. How can I change it so that when a box is on a right place amountOfBoxes only goes up by one?
You need a boolean field to track whether or not the actor is at the right place:
private boolean correctPlace;
Then you can use:
if (rightPlace() != correctPlace)
{
    correctPlace = !correctPlace;
    if (correctPlace)
    {
        changeBox();
        amountOfBoxes++;
    }
}
I do not see a need for lines 28 and 29. The value of the field should not ever be negative to begin with.
Hypergyra Hypergyra

2019/5/30

#
Now the problem doesn't occur anymore, thanks. But when I want to check how many boxes are on the right place it always gives one because it counts it individually. I want to make it so that when one box is on the right place, and another is on the right place, getAmountOfBoxes returns that there are two boxes that are on the same place. When I check getAmountOfBoxes I get this in return: box1.getAmountOfBoxes() returned: int 1. The number differs from which box I choose.
danpost danpost

2019/5/30

#
Hypergyra wrote...
Now the problem doesn't occur anymore, thanks. But when I want to check how many boxes are on the right place it always gives one because it counts it individually. I want to make it so that when one box is on the right place, and another is on the right place, getAmountOfBoxes returns that there are two boxes that are on the same place. When I check getAmountOfBoxes I get this in return: box1.getAmountOfBoxes() returned: int 1. The number differs from which box I choose.
Now that I have a better idea of what you have and what you are trying to accomplish. The timer and end-game code should probably be in your World subclass. The amount of boxes counter should not be an instance field in the Box class. It should either be a class field in the Box class or an instance field in your World subclass.
Hypergyra Hypergyra

2019/5/31

#
I don't know, I want it so that when there are four boxes in four right places the game ends. Could it be possible to check if each individual box is on the right place and then making an if statement that uses for example
box1.getAmountOfBoxes()==1&& box2.getAmountOfBoxes()==1 etc... 
I tried to do this but it wouldn't be recognized. When I try to make a subclass it also doesn't work, the box doesn't change.
danpost danpost

2019/5/31

#
Hypergyra wrote...
I want it so that when there are four boxes in four right places the game ends. Could it be possible to check if each individual box is on the right place and then making an if statement that uses for example << Code Omitted >> I tried to do this but it wouldn't be recognized.
I am sure that would be possible (with a bit of coding); however, let's keep things simple. Move the int field to your World subclass and make it public. Then, a box can increment it when arriving at the right place (line 7 of my snippet above):
((MyWorld)getWorld()).amountOfBoxes++;
The act method in MyWorld can check the value of the field for game over.
When I try to make a subclass it also doesn't work, the box doesn't change.
Subclassing is done to create more specialized (or more specific) objects -- not to alter or change an already created object.
Hypergyra Hypergyra

2019/5/31

#
Yes, thank you so much. It finally worked. I can't thank you enough.
Hypergyra Hypergyra

2019/5/31

#
Sorry to bother but it seems I have another problem. I now have my first world ready and want to make a second level. However when I initiate this second level I can't move my player because there are certain things that I use in my code that refer to Level1( my first world) How can I make it so that when the world switches the code switches?
if (rightPlace() != correctPlace)
        {
            correctPlace = !correctPlace;

            if (correctPlace)
            {
                changeBox();
                ((Level1)getWorld()).amountOfBoxes++;
            }
            if(!correctPlace)
            {
                changeBox2();

                ((Level1)getWorld()).amountOfBoxes--;
            }
        }
        Level1 mw = (Level1) getWorld();
        if(mw.getAmountOfBoxes()==4){
            long tijd = System.currentTimeMillis();
            long tijdsduur = (tijd - startTijd) / 1000;
            String message= "winner winner chicken dinner" + " time: " + tijdsduur + " seconds";
            getWorld().showText(message, getWorld().getWidth()/2, getWorld().getHeight()/2);
            Greenfoot.stop();
            Greenfoot.setWorld(new Level2());
        }
error: java.lang.ClassCastException: Level1 cannot be cast to Level2
Hypergyra Hypergyra

2019/5/31

#
Nevermind my question about the multiple worlds. I have found the answer. I decided to make an extra player, box, point etc... subclass that only works for the second world.
You need to login to post a reply.