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

2015/4/21

Bullets

jess204 jess204

2015/4/21

#
Hi everyone, Whenever I try to fire a bullet, it shows up in the world and rotates, but it won't move. It just stays completely stationary. I'm very new to all this, so any help would be greatly appreciated! Thanks! if (Greenfoot.isKeyDown("space")) { fire(); } private void fire() { Bullet bullet = new Bullet(); getWorld().addObject(bullet, getX(), getY()); bullet.setRotation(getRotation()); bullet.move(); }
danpost danpost

2015/4/21

#
You need to show the code of your Bullet class. Oh, and please use code tags (see the 'Posting code? read this!' link below the reply box).
jess204 jess204

2015/4/21

#

import greenfoot.*;  // (World, Actor, GreenfootImage, and Greenfoot)
import java.util.List;


import java.awt.Color;
import java.awt.Font;

/**
 * A bullet that can hit boid or Predator.
 * 
 * @author Poul Henriksen
 */ 
public class Bullet extends SmoothMover
{
      
    /**  
     * A bullet looses one life each act, and will disappear when life = 0
     * Increase the life to make the bullet fly further
    */
    private int life = 5;
    
    public Bullet()
    {
        
    }
    
    public Bullet (Vector speed, int rotation)
    {
        super(speed);
        setRotation(rotation);
        addForce(new Vector(rotation, 15));
        
    }
    
    /**
     * The bullet will kill the boids if it hits them.
     */
    public void act()
    {
        
        /**
         * Check to see if the bullet is at the word's edge, if so remove it
         */
        if(isAtEdge())
        {
            getWorld().removeObject(this);
        }
        /**
         * If the bullet isn't at the world's edge then continue
         */
        else
        {
            /**
             * If the bullet has flown far enough remove it from the world
             */
            if(life <= 0) 
            {
                getWorld().removeObject(this);
            }
            /**
             * If not decrement the life by one, move it and then check and see if it hit anything
             */
            else
            {
                life--;
                move();
                /**
                 * Inside of the if statement we will check and see if we hit a boid
                 * If we do the method will return a true, which means that the bullet
                 * doesn't exist any more and we don't want to check to see if we hit a predator
                 * So, only run the if statement if we don't hit a boid.
                 */
                if(!checkBoidHit())
                {
                    checkPredatorHit();
                }
            }
        }
        
    }
    
    /**
     * Check whether we have hit a boid.
     */
    private boolean checkBoidHit()
    {
        /**
         * To ensure that our bullet actually "hits" the boid we have to check a range around it
         * This is due to the small size of both objects and the speed at which they are moving.
         */
        int range = 10;
        boolean hit = false;
        List<Boid> boids = getObjectsInRange(range,Boid.class);
        /**
         * If the bullet hits a boid, then remove the boid
         */
        for (Boid boid : boids)
        {
            boid.hit();
            hit = true;   
        }

        /**
         * If the bullet hit a boid then remove the bullet from the world
        */ 
        if(hit)
        {
            getWorld().removeObject(this);
        }
        
        return hit;
    }
    
    /**
     * Check whether we have hit a predator.
     */
    private void checkPredatorHit()
    {
        Sky sky = (Sky)getWorld();
        /**
         * Check and see if we have anymore predators in the world
         */
        if(sky.getNumberOfPredators() > 0)
        {
            Predator predator = (Predator)getOneIntersectingObject(Predator.class);
            /**
               * If the bullet hit a predator then remove it from the world
               * Note: if the bullet doesn't hit anything then the predator object
               * will be null, meaning nothing
             */ 
            if(predator != null)
            {
                //predator.hit();
                getWorld().removeObject(this);
            }
        }
    }
}

danpost danpost

2015/4/21

#
Line 20 sets the value of 'life' to '5'. If you are to subtract one from it every act cycle before removing the bullet, then bullet will stay in the world approximately '0.1' seconds. Try setting its initial value to something move in the range of '200' to '300'.
danpost danpost

2015/4/21

#
Note: the middle word in the last line of my last post should be 'more' -- not 'move'. Also, the Bullet constructor you are calling does not implement the SmoothMover class. Try changing your 'fire' method that you posted previously to something like this:
private void fire()
{
    Bullet bullet = new Bullet(15, getRotation()); 
    getWorld().addObject(bullet, getX(), getY());
    bullet.move();
}
jess204 jess204

2015/4/22

#
I changed the life value, and that seemed to do the trick. But when I changed the fire method to what you posted, I got an error code saying "no suitable constructor found for Bullet(int,int)" Also, the gun is now firing like a machine gun would. How would I add a delay to it? thanks again!
jess204 jess204

2015/4/22

#
I figured it out, thank you so much for all your help!
You need to login to post a reply.