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

2013/1/2

Im at an impass...

Oxy Oxy

2013/1/2

#
i've been trying to get my counter to work correctly so i could keep track of the points the dolphin gets when it eats a fish in my game but i just can not get it to work. can anyone offer some help?
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

import java.awt.Font;
/**
 * Write a description of class Points here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Points extends Actor
{
    private int value = 0;
    private int target = 0;
    private int stringLength;
    private String text;
    
    
    public Points()
    {
        this("");
    }
    
    
    public Points(String t)
    {
        text = t;
        stringLength = (text.length() + 2) * 16;
    
        setImage(new GreenfootImage(stringLength, 24));
        GreenfootImage image = getImage();
        Font font = image.getFont();
        image.setFont(font.deriveFont(24.0F));  // use larger font
        
        updateImage();
    }
   
    public void act(){
    
        if(value > target) {
            value++;
            updateImage();
        }
        else if(value > target) {
            value--;
            updateImage();
        }
      
    }
    
        public void add(int score)
    {
        target += score;
    }

    public void subtract(int score)
    {
        target -= score;
    }

    public int getValue()
    {
        return value;
    }
    
    
    
    /*
     * Make the image
     */
    private void updateImage()
    {
        GreenfootImage image = getImage();
        image.clear();
        image.drawString(text + value, 1, 18);
    }
}
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Dolphin here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Dolphin extends Actor
{
    private int urSpeed = 4;
    private Points counter;
    
    public Dolphin(Points pointCounter)
    {
        counter = pointCounter;
    }
    
    
    /**
     * Act - do whatever the Dolphin wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        moveAround();
        eatFish();
    }    
    
    
    /*
     * moveAround is just to move up down so 
     * on and so forth.
     */
    public void moveAround()
    {
        
        move(urSpeed);
        
        if( Greenfoot.isKeyDown("left"))
        {
            turn(-urSpeed);
        }
        
        if(Greenfoot.isKeyDown("right"))
        {
            turn(urSpeed);
        }
    }
    
    
    public void eatFish()
    {
    Actor[] fish = new Actor[5];
       
        //Make all the fish part of an array so i dont have to write if(fish!=null) so many times.
        //use a for statement to check em all quickly.
        fish[0] = getOneObjectAtOffset(5,5, BlueFish.class);
        fish[1] = getOneObjectAtOffset(5,5, GreenFish.class);
        fish[2] = getOneObjectAtOffset(5,5, OrangeFish.class);
        fish[3] = getOneObjectAtOffset(5,5, PurpleFish.class);
        fish[4] = getOneObjectAtOffset(5,5, RedFish.class);
        
        for (int i=0; i < fish.length; i++)
        {
            if (fish[i] != null)
            {
                World world;
                world = getWorld();
                world.removeObject(fish[i]);
                
               
                switch (i)
                {
                    case 0:
                        counter.add(1);
                        break;
                        
                    case 1:
                        counter.add(2);
                           break;
                           
                    case 2:
                        counter.add(3);
                           break;
                           
                    case 3: //If it is a purple fish it shall make the 
                            //dolphin faster.
                        counter.add(10);
                        urSpeed++;
                           break;
                           
                    case 4:
                        counter.add(5);
                           break;
                           
                    default:
                        counter.add(0);
                            break;
                }
            }
        }
    }
    
}
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.Random;
/**
 * Write a description of class UnderWater here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class UnderWater extends World
{
    Points Dpoints = new Points("Points: ");
    
    /**
     * Constructor for objects of class UnderWater.
     * 
     */
    public UnderWater()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(1000, 600, 1);
        
        prepare();
    }
    
    public void act()
    {
       int fishNumber; //To hold a value for a random fish number
       
       fishNumber = Greenfoot.getRandomNumber(1500);
        //if statements to find out when to spawn.
        if ( fishNumber <= 20)
         addObject(new BlueFish(),Greenfoot.getRandomNumber(getWidth()),Greenfoot.getRandomNumber(getHeight()));     
        else if (fishNumber >= 21 && fishNumber <= 25)
            addObject(new GreenFish(), Greenfoot.getRandomNumber(getWidth()), Greenfoot.getRandomNumber(getHeight()));
        else if (fishNumber >= 26 && fishNumber <= 29)
            addObject(new OrangeFish(), Greenfoot.getRandomNumber(getWidth()), Greenfoot.getRandomNumber(getHeight()));
        else if (fishNumber >= 30 && fishNumber <= 33)
            addObject(new PurpleFish(), Greenfoot.getRandomNumber(getWidth()), Greenfoot.getRandomNumber(getHeight()));
        else if (fishNumber >= 34 && fishNumber <= 36)
            addObject(new RedFish(), Greenfoot.getRandomNumber(getWidth()), Greenfoot.getRandomNumber(getHeight()));
        else if (fishNumber == 666)
            addObject(new Shark(), 235, 368 );
    }
    
  
    
        private void prepare()
    {
       addObject(Dpoints, 100, 375);
       Dolphin d = new Dolphin(Dpoints);
       
       addObject(d, getWidth() / 2, getHeight() / 2); 
       
    }
}
vonmeth vonmeth

2013/1/2

#
public void act(){  
      
        if(value > target) {  
            value++;  
            updateImage();  
        }  
        else if(value > target) {  
            value--;  
            updateImage();  
        }  
        
    }  
This is a bit odd. Both are checking if value is greater than target. The second 'if' statement, when evaluated, will never be true because the only time it will be evaluated is when the first 'if' statement is false. But since they are both testing for the same thing, it will be false as well. But that is besides the point, you just want to increment value up till it is equal to target. You want less than, not greater than.
public void act(){  
      
        if(value < target) {  
            value++;  
            updateImage();  
        }  
    }  
That will increment value till it is equal to target. Which is probably what you want.
Oxy Oxy

2013/1/3

#
omg Thank You! I can't believe i didn't see that!
You need to login to post a reply.