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

2014/9/24

GreenfootImage not working

CKnox CKnox

2014/9/24

#
In class we are trying to create a rocket game where the rocket shoots bullets. If I add the bullet in manullay it shows up as it should, a small yellow rectangle. However when the rocket fires, the only image that shows up is the Greenfoot icon. This is the Rocket code:
public void fire()
    {
        if ( Greenfoot.isKeyDown("space") ) {
            if (counter==0) {
                getWorld().addObject(new Bullet (getRotation()),getX(),getY());
                counter = counter + 1;
            } else {
                if (counter == 15) {
                    counter = 0;
                } else {
                    counter = counter +1;
                }
           }
        } else {
            counter = 0;
        }
    }
Here is the code in the Bullet:
public Bullet()
    {
       GreenfootImage image = new GreenfootImage(8,20);
       image.setColor(java.awt.Color.YELLOW);
       image.fillRect(0,0,8,2);
       setImage(image);
    }
Any help would be appreciated!
Super_Hippo Super_Hippo

2014/9/24

#
In the 'fire' method, you create the bullet with a parameter (the rotation). This means that you have another constructor in the 'Bullet' class. This should look a bit like this:
public Bullet(int rot) //or however you named it
{
    //add this line. It calls the default constructor of this class (the one you posted above)
    this();
    setRotation(rot);
}
CKnox CKnox

2014/9/25

#
Thanks for your help!
You need to login to post a reply.