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

2015/4/6

Facing Left and Shooting Left

Programmer1998 Programmer1998

2015/4/6

#
Is there a way that I can shoot left and face left without having to press the left key? At the moment my player only faces left if I'm pressing the left key, but immediately faces right after I let go. Same goes for shooting. Is there a way to fix that?
 public void animateRight()
    {
        swimRight();
        if(Greenfoot.isKeyDown("right"))
        {
            swimRight();
        }
    }
    
    public void animateLeft()
    {
        if(Greenfoot.isKeyDown("left"))
        {
            swimLeft();
        }
        else if("left".equals(Greenfoot.getKey()))
        {
            swimLeft();
        }
    }
Here is my animation code:
   
    public void swimRight()
    {
        frame = frame + 1;
        frame = frame % 3;
        String image = "mario"+frame+".gif";
        setImage(new GreenfootImage(image));
    }
    
    public void swimLeft()
    {
        frame = frame + 1;
        frame = frame % 3;
        String downmario = "leftmario"+frame+".gif";
        setImage(new GreenfootImage(downmario));
    }
And my shooting code:
  public void shoot()
    {  
        if(Greenfoot.isKeyDown("left")) //if left key is pressed, Mario will shoot left
        {
            DownShell fire = new DownShell();
            fire.setSpeed(shellSpeed_);
            fire.setRotation(180);
            
            World w = getWorld();
            w.addObject(fire, this.getX(), this.getY());
            
            reloadTimer_ = 25;
            Greenfoot.playSound("marioshell.wav");
        }
        else //Mario shoots to the right if left key is not pressed
        {
            Shell fireThis = new Shell();
            fireThis.setSpeed(shellSpeed_);
            
            World w = getWorld();
            w.addObject(fireThis, this.getX(), this.getY());
       
            reloadTimer_ = 25;
            Greenfoot.playSound("marioshell.wav");
        }
    }
Super_Hippo Super_Hippo

2015/4/6

#
You can use a boolean to keep track of the direction it is facing. The 'animateRight' and 'animateLeft' methods should look similar.
You need to login to post a reply.