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

2015/3/14

Getting an int from another class trouble

Krhanos Krhanos

2015/3/14

#
I need help getting my bullets from counter to bullets for my rocket Counter code
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;

/**
 * A Counter class that allows you to display a numerical value on screen.
 * 
 * The Counter is an actor, so you will need to create it, and then add it to
 * the world in Greenfoot.  If you keep a reference to the Counter then you
 * can adjust its value.  Here's an example of a world class that
 * displays a counter with the number of act cycles that have occurred:
 * 
 * <pre>
 * class CountingWorld
 * {
 *     private Counter actCounter;
 *     
 *     public CountingWorld()
 *     {
 *         super(600, 400, 1);
 *         actCounter = new Counter("Act Cycles: ");
 *         addObject(actCounter, 100, 100);
 *     }
 *     
 *     public void act()
 *     {
 *         actCounter.setValue(actCounter.getValue() + 1);
 *     }
 * }
 * </pre>
 * 
 * @author Neil Brown and Michael Kölling 
 * @version 1.0
 */
public class Counter extends Actor
{
    private boolean shooting = false;
    
    private static final Color transparent = new Color(0,0,0,0);
    private GreenfootImage background;
    public int bullets ;
    private int maximum;
    private String prefix;
    
    public Counter()
    {
        this(new String());
    }

    /**
     * Create a new counter, initialised to 0.
     */
    public Counter(String prefix)
    {
        background = getImage();  // get image from class
        bullets = 25;
        maximum = 25;
        this.prefix = prefix;
        updateImage();
    }
    
    /**
     * Animate the display to count up (or down) to the current target value.
     */
    public void act() 
    {
        if (bullets > 0)
        {
        if(Greenfoot.isKeyDown("space") && !shooting) 
        {
            bullets = bullets - 1;
            updateImage();
            shooting = true;
        }
        if(!Greenfoot.isKeyDown("space"))
        {
            shooting = false;
        }
            
        }
    }

    /**
     * Add a new score to the current counter value.  This will animate
     * the counter over consecutive frames until it reaches the new value.
     */
    public void add(int score)
    {
       maximum += score;
    }

    /**
     * Return the current counter value.
     */
    public void getValue(int bullets)
    {
        
    }

    /**
     * Set a new counter value.  This will not animate the counter.
     */
    public void setValue(int newValue)
    {
        maximum = newValue;
        bullets = newValue;
        updateImage();
    }
    
    /**
     * Sets a text prefix that should be displayed before
     * the counter value (e.g. "Score: ").
     */
    public void setPrefix(String prefix)
    {
        this.prefix = prefix;
        updateImage();
    }

    /**
     * Update the image on screen to show the current value.
     */
    private void updateImage()
    {
        GreenfootImage image = new GreenfootImage(background);
        GreenfootImage text = new GreenfootImage(prefix + bullets, 22, Color.BLACK, transparent);
        
        if (text.getWidth() > image.getWidth() - 20)
        {
            image.scale(text.getWidth() + 20, image.getHeight());
        }
        
        image.drawImage(text, (image.getWidth()-text.getWidth())/2, 
                        (image.getHeight()-text.getHeight())/2);
        setImage(image);
    }
}
World class code
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.*;

public class Space extends World 
{
    
    private Counter theCounter;
    
    private int speed =3;
    
    Score score = new Score();
    private Score sb;
    
  
    
    GreenfootSound backgroundMusic = new GreenfootSound("Flight.wav");
    
   
    public Space()
    {    
        
        
        super(1400, 800, 1); 
        

         if(Greenfoot.isKeyDown("enter")) 
        {
           backgroundMusic.setVolume(45);
           backgroundMusic.playLoop();
          
        }
        addObject(sb = new Score(),77,45);
        
        addObject(new Counter(),250, 45);
        theCounter = new Counter();
        
        GreenfootImage img = new GreenfootImage(800,700);
        img.fill();
        setBackground(img);
        
        
        Explosion.initializeImages();
        
        prepare();
        
        addStars(200);
        
     
    }
    
    public void act()
    {
        if( Greenfoot.getRandomNumber(1000) <8)
        {
            addObject(new Asteroid(), getWidth()-5, Greenfoot.getRandomNumber(getHeight() ) );
        }
        
        
        
    }
    
    
    public Counter getCounter()
    {
        return theCounter;
    }
    
    public void hitAsteroid()
    {
      sb.hitAsteroid();
    
    }
    
    public void stopped()
    {
     backgroundMusic.pause();
    }
    
   

    
    private void prepare()
    {
        Rocket rocket = new Rocket();
        addObject(rocket, 80, getHeight()/2);
    }
    
    
    public void addStars( int n)
    {
        for( int s=0; s<n; s++ )
        {
            int x = Greenfoot.getRandomNumber( getWidth() );
            int y = Greenfoot.getRandomNumber( getHeight() );
            addObject(new Star(), x, y);
        }
    }
    
    
   
}
Rocket code
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class Rocket extends Actor
{
    private boolean shooting = false;
    private boolean helper = false;
    public int bullets;
    
   
    
    public void act() 
    {
        ShipControl();
        
        
      
        
        
        
        Actor powerup = getOneIntersectingObject(PowerUp.class);
        
        if(powerup != null)
        {
            getWorld().removeObject(powerup);
            
            if(!helper)
            {
     
               getWorld().addObject(new Helper(), getX(), getY()-60);
               helper = true;
               Greenfoot.playSound("PowerUp.wav");
            }
        
        }
        
        if (getWorld().getObjects(Helper.class).isEmpty())
        {
            helper = false;
       }
    }    
    

    
    public void ShipControl()
    {
        
        if(getWorld()!=null)
        {
            Space spaceWorld= (Space) getWorld();
            Counter counter = spaceWorld.getCounter();
            counter.setValue(bullets =  bullets);
            
            if(Greenfoot.isKeyDown("up")) 
            {
                setLocation(getX(), getY()-5);
            }
        
            if(Greenfoot.isKeyDown("down")) 
            {
                setLocation(getX(), getY()+5);
            }
            
            if (bullets > 1)
            {
                
                if(Greenfoot.isKeyDown("space") && !shooting) 
                {
                    getWorld().addObject(new Bullet(), getX(), getY() );
                    Greenfoot.playSound("Lasergun2.wav");
                    shooting = true;
                }
            
                if(!Greenfoot.isKeyDown("space"))
                {
                    shooting = false;
                }
            
            }
        
        }
    }
}
Please help
davmac davmac

2015/3/14

#
For a start, consider the getValue method in your Counter class:
    /**
     * Return the current counter value.
     */
    public void getValue(int bullets)
    {
         
    }
Can you see what's wrong here?
  • What is the method supposed to return (according to the comment and the name)?
  • OTOH: What type is it declared to return?
  • Should it need any parameter?
  • OTOH: How many parameters is it declared to need?
I think your first step is to fix this method.
davmac davmac

2015/3/14

#
In fact, this is an imported class. So the real question is: why did you change it? The first step is probably to change it back (i.e. delete it and re-import it).
Krhanos Krhanos

2015/3/14

#
Okay so i changed it back to the original counter
Krhanos Krhanos

2015/3/14

#
Bullet = value now but i still cant get value from the counter for the rocket
davmac davmac

2015/3/14

#
((Space) getWorld()).getCounter().getValue()
This is covered in tutorial #6
You need to login to post a reply.