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

2015/3/19

How do you get a bullet to shoot left help plz

Linniah Linniah

2015/3/19

#
import greenfoot.*;

/**
 * Write a description of class Hero here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Hero extends Animal
{
    private int speed_;
    private GreenfootImage seq1l = new GreenfootImage("tmp-1.gif");
    private GreenfootImage seq2l = new GreenfootImage("tmp-2.gif");
    private GreenfootImage seq3l = new GreenfootImage("tmp-3.gif");
    private GreenfootImage seq4l = new GreenfootImage("tmp-4.gif");
    private GreenfootImage seq1r = new GreenfootImage("tmp-1.png");
    private GreenfootImage seq2r = new GreenfootImage("tmp-2.png");
    private GreenfootImage seq3r = new GreenfootImage("tmp-3.png");
    private GreenfootImage seq4r = new GreenfootImage("tmp-4.png");
    private int frame = 1; 
    private int animationCounter = 0;
    
    private int reloadTimer_;
 
    /**
     * Act - do whatever the Hero wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    
   
    public Hero()
    {
        super();
        speed_= 3;
        reloadTimer_ = 0;
        
    }
    
    public void act()
    {
        heroWalk();
        animationCounter ++;
        lookForLeeks();
        
    }
    
    public void animateLeft()
    {
        if(frame == 1)
        {
            setImage(seq1l);
        }
        else if(frame == 2)
        {
            setImage(seq2l);
        }
        else if(frame == 3)
        {
            setImage(seq3l);
        }
        else if(frame == 4)
        {
            setImage(seq4l);
            frame = 1;
            return;
        }
        
        frame ++;
    }
    
    public void animateRight()
    {
        if(frame == 1)
        {
            setImage(seq1r);
        }
        else if(frame == 2)
        {
            setImage(seq2r);
        }
        else if(frame ==3)
        {
            setImage(seq3r);
        }
        else if(frame ==4)
        {
            setImage(seq4r);
            frame = 1;
            return;
        }
        
        frame ++;
    }
 
    public void heroWalk() 
    {
       if(Greenfoot.isKeyDown("left")) 
        {
            setLocation(getX() -speed_, getY());
            if(animationCounter % 4 == 0)
            animateLeft();
            
        }
        
       if(Greenfoot.isKeyDown("right"))
        {
            setLocation(getX() +speed_, getY());
            if(animationCounter % 4 == 0)
            animateRight();
        }
        
       if(Greenfoot.isKeyDown("up"))
        {
            setLocation(getX(), getY() -speed_);
            if(animationCounter % 4 == 0)
            animateLeft();
        }
        
       if(Greenfoot.isKeyDown("down"))
        {
            setLocation(getX(), getY() +speed_);
            if(animationCounter % 4 == 0)
            animateLeft();
        }
       
      if(reloadTimer_ <=0)
      {
          if(Greenfoot.isKeyDown("z"))
          {
              shoot();
          }
      }
      else
      {
          reloadTimer_--;
      }
      
        
    }
    
    public void shoot()
    {
        Laser fireThis = new Laser();
        World w = getWorld();
        w.addObject(fireThis, this.getX(), this.getY());
        reloadTimer_ = 15;
    }
 
    
    
    public void lookForLeeks()
    {
        if(canSee(Leek.class))
        {
            eat(Leek.class);
            Greenfoot.playSound("Nom.wav");
        }
    }
   
    
  
 
   
    }
Linniah Linniah

2015/3/19

#
This is driving me crazy OTL
danpost danpost

2015/3/19

#
Atm, the only way to tell which way the hero is facing is by what image it is currently displaying:
public int getFacing()
{
    int facing = 0;:
    if (getImage() == seq1l || getImage() == seq2l || getImage() == seq3l || getImage() == seq4l) facing = 180;
    return facing;
};
Now, when a laser is created:
fireThis.setRotation(getFacing());
can be used at around line 145 to fix its direction.
Linniah Linniah

2015/3/19

#
It work!!! Ty so much
You need to login to post a reply.