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

2014/9/25

My score is going up too Fast

coder04 coder04

2014/9/25

#
When I Hold space to the score is going up too fast Can you help me make it so it goes up one when ever my bullet eats/kills another enemy Shot or bullet code
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;

/**
 * Write a description of class Counter here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Counter extends Actor
{
    int score = 0;
    /**
     * Act - do whatever the Counter wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        setImage(new GreenfootImage("Score : " + score, 24, Color.GREEN, Color.BLACK));
    }    
    
    public void addScore()
    {
        score++;
    }
}
Counter or score code
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;

/**
 * Write a description of class Counter here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Counter extends Actor
{
    int score = 0;
    /**
     * Act - do whatever the Counter wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        setImage(new GreenfootImage("Score : " + score, 24, Color.GREEN, Color.BLACK));
    }    
    
    public void addScore()
    {
        score++;
    }
}
Super_Hippo Super_Hippo

2014/9/25

#
You posted the 'Counter' class twice.
coder04 coder04

2014/9/25

#
bullet class updated
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)  
  
/** 
 * Write a description of class Shot here. 
 *  
 */  
public class Shot extends Mover  
{  
   private int life;  
      
    /** 
     * Act - do whatever the Shot wants to do. This method is called whenever 
     * the 'Act' or 'Run' button gets pressed in the environment. 
     */  
    public void act()   
    {  
       move(20.0);  
       life--;  
       eat();
       ifAtWorldEdge();
       if (life == 0)
       {  
           getWorld().removeObject(this);  
    }  
    }  

    public void eat()
    {
        //World World;
        // World = getWorld();
        //Space space = (Space)World;
        //Counter counter = space.getCounter();
        //counter.addScore();
        
        Actor Miniship;
        Miniship = getOneObjectAtOffset(0, 0, Miniship.class);
        if (Miniship != null)
        {
            World world;
            world = getWorld();
            world.removeObject(Miniship);
        }  
        
        Actor Warship;
        Warship = getOneObjectAtOffset(0, 0, Warship.class);
        if (Warship != null)
        {
            World world;
            world = getWorld();
            world.removeObject(Warship);
        } 

    }  
    
   public void ifAtWorldEdge()  
    {  
        if (atWorldEdge())  
        {  
             getWorld().removeObject(this);  
        }  
    } 
    
}  
coder04 coder04

2014/9/25

#
help please
Super_Hippo Super_Hippo

2014/9/25

#
If you only want the score to be increased if an enemy is hit, then you should put the related code to the position when it checked if there is an enemy. By the way, why do you use 3 lines of code to remove one object? You can just use 'getWorld().removeObject(...)'.
coder04 coder04

2014/9/25

#
How do you do that?
Super_Hippo Super_Hippo

2014/9/25

#
You are already have the conditions to check if there is an enemy (line 37 and line 46). In this if-blocks, you need to call the 'addScore' method on the Counter object. (the part you commented out has to go there)
You need to login to post a reply.