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

2013/11/20

Bullet image left

pr2alede pr2alede

2013/11/20

#
Hi, I'm currently writing a game which requires a rocket to shoot a bullet. Currently the bullet gets shot but it leaves and image of the bullet behind of where the actor was places. Any ideas how to solve this. I have included the code
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Bullet here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Bullet extends Actor
{
    private int direction, speed;
    
     public boolean atWorldEdge()
    {
        if(getX() < 20 || getX() > getWorld().getWidth() - 20)
            return true;
        if(getY() < 20 || getY() > getWorld().getHeight() - 20)
            return true;
        else
            return false;
    }
    
    public Bullet(int dir)
    {
        direction = dir;
        speed = 10;
    }

    public void act()
    {
        setLocation(getX(), getY() - speed);
        if(atWorldEdge()==true)
        {
            getWorld().removeObject(this);
        }
    }

    public Bullet()
    {
        {
            GreenfootImage image = getImage();
            image.scale(image.getWidth() - 160, image.getHeight() - 200);
            setImage(image);
        }
    }
  
    }
Gevater_Tod4711 Gevater_Tod4711

2013/11/20

#
The problem probably is that your bullet's speed value is zero when you create the bullet using the second constructor (public Bullet() in line 38). If you set the speed in this constructor like in the other constructor (speed = 10 in line 26) it should work.
danpost danpost

2013/11/20

#
I noticed that 'direction' is not used anywhere in the code (it is a parameter in one of the constructors which sets an instance field to its value; but that is it). No matter what value is given it, there will be no change in the behavior of the bullet. The movement of the bullet objects is strictly upward because of the way the 'setLocation' statement on line 31 is used.
You need to login to post a reply.