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

2015/12/30

Next Level

RobotDJS RobotDJS

2015/12/30

#
Hi guys. I created a game were a monkey shoots bananas at Targets (teddy bears.) I want to make it so when 5 of a certain object is shot they start falling from the sky instead of just being created randomly. I made a variable to count the number of Targets were hit, but instead of adding 1 for every Target hit, it sets the variable shot to 1 for each target hit. Here is the code for Target
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class target here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Target extends Actor
{
    private GreenfootImage target = new GreenfootImage("teddybear.png");
    private GreenfootImage dead = new GreenfootImage("skull.png");
    private int shot = 0;
    /**
     * Act - do whatever the target wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        checkCollision();
    }
    
    public void checkCollision()
    {
        Actor collided;
        collided = getOneIntersectingObject(Projectile.class);
        if (collided != null)
        {
            if (getImage() != dead)
            {
               ProjectileWorld world = (ProjectileWorld)getWorld();
               world.updateScore();
               shot = shot + 1;
               if (shot>=5)
               {
                   getWorld().addObject (new Target (), Greenfoot.getRandomNumber (600), 400);
               }
               else
               {
                   getWorld().addObject (new Target (), Greenfoot.getRandomNumber (600), Greenfoot.getRandomNumber (600 - 250) + 200);
               }
            }
               setImage(dead);
        }
    }
}
Here is the code for the world
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class ProjectileWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class ProjectileWorld extends World
{
    private long startMillis = 0;
    private int numFrames = 0;
    private final int groundHeight;
    
    public static final double MAX_FORCE = 8.5;
    public static final double MIN_FORCE = 3.5;
    public static final double GRAVITY = 0.1;
    public static final int LOWEST_ANGLE = 30;
    
    private Player[] players;
    private int curPlayer;
    private int turnCountdown;
    private Scoreboard sb;
    private double t;
    private Target tg;

    /**
     * Constructor for objects of class ProjectileWorld.
     * 
     */
    public ProjectileWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(800, 600, 1, false); 
        
        groundHeight = 550;
        getBackground().setColor(new java.awt.Color(0.8f, 0.95f, 1.0f, 1.0f));
        getBackground().fill();
        getBackground().setColor(new java.awt.Color(0.4f, 0.7f, 0.4f, 1.0f));
        getBackground().fillRect(0, groundHeight, getWidth(), getHeight());
        sb = new Scoreboard();
        addObject(sb,70,30);
        addObject(new Target(), Greenfoot.getRandomNumber(getWidth()), Greenfoot.getRandomNumber(getHeight() - 250) + 200);
        
        players = new Player[] {new HumanPlayer()};
        
        for (Player p : players)
        {
            addObject(p, Greenfoot.getRandomNumber(getWidth() - 100), groundHeight);
        }
        curPlayer = 0;
        turnCountdown = 10;
    }
    
    public void updateScore()
    {
        sb.changeScore();
    }
    
    public void started()
    {
        numFrames = 0;
        startMillis = System.currentTimeMillis();
    }
    
    public void act()
    {
        numFrames += 1;
        t = getTimePerFrame();
        
        if (turnCountdown > 0)
        {
            turnCountdown -= 1;
            if (turnCountdown == 0)
            {
                players[curPlayer].startTurn();
            }
        }
    }
    
    public double getTimePerFrame()
    {
        return (double)(System.currentTimeMillis() - startMillis) / (1000.0 * (double)(numFrames));
    }
    
    public boolean hitsGround(double startX, double startY, double endX, double endY)
    {
        return endY > groundHeight;
    }
    
    public boolean hitsTarget(double startX, double startY, double endX, double endY)
    {
        return endY > groundHeight;
    }
    
    public void landed()
    {
        curPlayer = (curPlayer + 1) % players.length;
        turnCountdown = 10;
    }
}
That should be all the code you need. Please help if you can. Thanks.
danpost danpost

2015/12/30

#
Line 13 of the Target class declares an instance field -- one that is given to each instance created from the class. To declare a field that all objects created from the class can share, use a class field (not an instance field). A class field is one that is declared 'static'. Change line 13 to:
public static int shot;
Static field are only reset during compilation of the project, so in your World subclass constructor, make sure to set its value back to zero (so resetting the project, which does not recompile it, zeroes its value):
Target.shot = 0;
RobotDJS RobotDJS

2015/12/30

#
Thanks! It worked!
You need to login to post a reply.