I need help getting my bullets from counter to bullets for my rocket
Counter code
World class code
Rocket code
Please help
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);
}
}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);
}
}
}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;
}
}
}
}
}
