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

2015/9/11

why won't "s" work?

hoothoot hoothoot

2015/9/11

#
I can't figure out why my s key command doesn't work. here is my code
import greenfoot.*;

/**
 * Write a description of class Gohan here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Gohan extends Mover
{
         private int gunReloadTime;              // The minimum delay between firing the gun.
    private int reloadDelayCount;  
    public int health = 900000; //its over 9000   
     private int shotsFired; 
    public void setHealth(int points) {
        health += points;  
    }
    public int getHealth() {
        return health;
    }
    private GreenfootImage image1;
    private GreenfootImage image2;
    private GreenfootImage image3;
    private GreenfootImage image4;
    private GreenfootImage image5;
    private GreenfootImage image6; 
    private GreenfootImage image7;
    private GreenfootImage image8;
    public Gohan() 
    {
        image1 = new GreenfootImage("gohan.png"); 
        image2 = new GreenfootImage("gohanstep.png");
        image3 = new GreenfootImage("gohanstep2.png");
        image4 = new GreenfootImage("powerattack.png");
        image5 = new GreenfootImage("gohanpunch.png");
        image6 = new GreenfootImage("Gohankick1.png");
        image7 = new GreenfootImage("HealthBar.png");
        image8 = new GreenfootImage("Gpowerup.png");
        setImage(image1); 
        reloadDelayCount = 0;
        gunReloadTime = 20;
        
    }
        /**
     * Return the number of shots fired from this rocket.
     */
    public int getShotsFired()
    {
        return shotsFired;
    }
    /**
     * Set the time needed for re-loading the rocket's gun. The shorter this time is,
     * the faster the rocket can fire. The (initial) standard time is 20.
     */
    public void setGunReloadTime(int reloadTime)
    {
        gunReloadTime = reloadTime;
    }
    /**
     * Act - do whatever the Gohan wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
       checkKeypress ();
        reloadDelayCount++;
    }
    public void checkKeypress() 
    {
        if (Greenfoot.isKeyDown("left"))
            {
                move(-30);
            if ( getImage() == image1 ) 
                {   setImage(image2);
                }
            else
                {if ( getImage() == image8 ){}
                 else{   setImage(image1);
                      
                    }
                }
        }
        if (Greenfoot.isKeyDown("right"))
             {    move (30);
                 if ( getImage() == image1 ) 
            {   setImage(image2);
            }
        
           else{ setImage(image1);
            }
        }
        if (Greenfoot.isKeyDown("s"))
        {
                setImage(image3);
                hitEnemy();
                Greenfoot.playSound("Gpunch.wav");
        }
                else if ( getImage()== image3)
                    {setImage(image1);}  
        if (Greenfoot.isKeyDown("z"))
        {
                setImage(image3);
                hitEnemy();
                Greenfoot.playSound("Gpunch.wav");
        }
                 else if ( getImage()== image3)
                     {setImage(image1);}
        if (Greenfoot.isKeyDown("x"))
        {
                setImage(image6);
                hitEnemy();
                Greenfoot.playSound("Gpunch.wav");
        
            }else if ( getImage()== image6)
            {setImage(image1);}        
                if (Greenfoot.isKeyDown("a"))
        {   
                setImage(image5);
                hitEnemy();
               Greenfoot.playSound("Gpunch.wav");
                
        }
        else if ( getImage()== image5)
            {setImage(image1);}
                if (Greenfoot.isKeyDown("space"))
        {   
                setImage(image1);
                fire();

        }        
        if (health<=0)
        {
            Greenfoot.playSound("cellyelling.wav");
            getWorld().removeObject(this);
        } 
        }
       private void hitEnemy() {
        Cell cell = (Cell) getOneObjectAtOffset(0, 0, Cell.class);
        if (cell != null) {
            cell.setHealth(-1001);
            cell.move(30);
            if (cell.health <=0)
            {getWorld().removeObject(cell);
                setImage(image8);
                Greenfoot.playSound("gohanyelling.wav");
            }
        } 
    }
    private void fire() 
    {
        move(-250);
        if (reloadDelayCount >= gunReloadTime) {
            Bullet b = new Bullet(getMovement().copy(), getRotation());
            getWorld().addObject(b, getX(), getY());
            b.move();
            shotsFired++;
            reloadDelayCount = 0;   // time since last shot fired
        }
    }
    
}
hoothoot hoothoot

2015/9/11

#
turns out everything is working but the image change. it works if i press "z", though
hoothoot hoothoot

2015/9/11

#
i figured it out. I just deleted the "s" command and changed the "z" to an "s"
You need to login to post a reply.