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

2016/3/30

Issues with Bullet Code

sillfish sillfish

2016/3/30

#
I am working on a project for class and I am having trouble with my shooting code. The first issue is making a displayed counter for our bullets. We don't know how to do that. Next, we need to figure out how to set a starting value for the bulletCount. Right now we have it set to stop the scenario at -300 bullets shot, because our current starting value is zero. Also, we need a way to stop the character from shooting after he is out of bullets, rather than stopping the scenario. Here is the code for our main class:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class catMario here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class catMario extends Actor
{
    private int gunReloadTime;
    private int reloadDelayCount;
    private int direction;
    private int speed;
    private int bulletCount;
    
    /**
     * 
     * Act - do whatever the catMario wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        keyMovement();
        bounceBack();
        checkFire();
        bulletCounter();
    } // Add your action codif (Greenfoot.isKeyDown("w"))
       
   
    
    
    
    public void keyMovement()
    {
    
        if (Greenfoot.isKeyDown("w"))
        {
            jumpUp(1);
        }
        
        if (Greenfoot.isKeyDown("s"))
        {
            jumpDown(1);
        }
        
        if (Greenfoot.isKeyDown("a"))
        {
            move(-10);
        }
        
        if (Greenfoot.isKeyDown("d"))
        {
            move(10);
        }
        
        if (Greenfoot.isKeyDown("space"))
        {
            checkFire();
        }
        
    }
    
    public void jump(int distance, int direction)
    {
        for (int i = 0; i < distance; i++)
        {
            super.setLocation(super.getX( ), super.getY() + direction);
        }
    }
    
    public void jumpUp(int distance)
    {
        this.jump(distance, -10);
    }
    
    public void jumpDown(int distance)
    {
        this.jump(distance,10);
    }
    
    
    public void checkFire()
    {
        if(Greenfoot.isKeyDown("space")) 
        {
            getWorld().addObject(new Lemon(), getX(), getY());
            bulletCount= bulletCount - 1;
        }
    }
    
    public void bulletCounter()
    {
        if ( bulletCount == -300){
            Greenfoot.stop();
        }
    }
    
    Here is the code for our bullet class, which is currently called lemon.
}
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Lemon here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Lemon extends Actor
{
    /**
     * Act - do whatever the Lemon wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        
            move(-20);

    }
    
     
    }
   
sillfish sillfish

2016/3/30

#
Update: We have set the starting value. We still need help with a counter, a way to stop shooting after out of bullets, and code for killing the enemy after they are hit
brooklynnetswm brooklynnetswm

2016/3/30

#
whats up homie
danpost danpost

2016/3/30

#
sillfish wrote...
We still need help with a counter
You could check my Value Display Tutorial scenario out on this issue.
a way to stop shooting after out of bullets
Do not look at it as a way to stop something. Look at it as a way to control something. It is not what to do to stop shooting -- it is what condition is required to shoot. If the counter not zero, you can shoot (the flip side to this is -- if the counter is zero, do not shoot).
code for killing the enemy after they are hit
Have enemy check for hit in its act method and remove (possibly the object hitting and) itself when hit.
You need to login to post a reply.