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

2021/2/20

Shooting in both directions?

Genota Genota

2021/2/20

#
I´ve made a 2D platform game, wich include a player, that can shoot, but only to the right side. I know somehow it must detect, wich direction the player is facing, but I really don´t know exactly what I have to do. All other discussions got a player who rotates, but mine should be straight left and right. Code "Player":
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Player here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Player extends Actor
{
    /**
     * Act - do whatever the Player wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    private int vSpeed = 0;
    private int accel = 1;
    private int speed = 5;
    private int jumpHeight= -20;
    private boolean jumping = false;
    private int attacking = -1;
    private int animationSpeed = 2;
    private int shotDelay;
    
    private GreenfootImage run1 = new GreenfootImage("Glumanda1right1.png");
    private GreenfootImage run2 = new GreenfootImage("Glumanda2right1.png");
    private GreenfootImage run3 = new GreenfootImage("Glumanda3right1.png");
    private GreenfootImage run4 = new GreenfootImage("Glumanda1right2.png");
    private GreenfootImage run5 = new GreenfootImage("Glumanda2right2.png");
    private GreenfootImage run6 = new GreenfootImage("Glumanda3right2.png");
    private GreenfootImage run7 = new GreenfootImage("Glumanda1left1.png");
    private GreenfootImage run8 = new GreenfootImage("Glumanda2left1.png");
    private GreenfootImage run9 = new GreenfootImage("Glumanda3left1.png");
    private GreenfootImage run10 = new GreenfootImage("Glumanda1left2.png");
    private GreenfootImage run11 = new GreenfootImage("Glumanda2left2.png");
    private GreenfootImage run12 = new GreenfootImage("Glumanda3left2.png");
    private GreenfootImage run13 = new GreenfootImage("Glumandamouth1.png");
    private GreenfootImage run14 = new GreenfootImage("Glumandamouth2.png");
    private GreenfootImage run15 = new GreenfootImage("Glumandamouth3.png");
    private GreenfootImage run16 = new GreenfootImage("Glumandamouth4.png");
    private int frame = 1;
    private int animationCounter = 0;
    GifImage myGif = new GifImage ("Glgif.gif");
    
    public void act() 
    {
        checkFalling();
        fall();
        jump();
        moveAround();
        animationCounter ++;
        fireProjectile();
    }  
    public void moveAround()
    {
   
      if(Greenfoot.isKeyDown("d"))
      
      {
          setLocation(getX() + speed, getY());
          if(animationCounter % 8 == 0)
          animateRight();
      }
      if(Greenfoot.isKeyDown("a"))
       {
         setLocation(getX() - speed, getY());
         if(animationCounter % 8 == 0)
         animateLeft();
      }
    }
    public void fall()
    {
        setLocation(getX(), getY() + vSpeed);
    }
    public void checkFalling()
    {
        if (!isTouching(Ground.class))
        {
            vSpeed++;
        }
        else 
            vSpeed = 0;
        
    }
    public void jump()
    {
        if(Greenfoot.isKeyDown("space")&& (onGround()==true))
        {
            vSpeed = jumpHeight;
            fall();
        }
    }
    boolean onGround()
    {
        Actor under = getOneObjectAtOffset(0, getImage().getHeight()/2, Ground.class);
        return under != null;
    }
    public void moveRight()
    {
        setLocation (getX()+speed, getY());
    }
    public void animateRight()
    {
        if (frame == 1)
        {
            setImage(run1);
        }
        else if(frame ==2)
        {
            setImage(run2);
        }
        else if(frame ==3)
        {
            setImage(run3);
        }
        else if(frame ==4)
        {
            setImage(run4);
        }
        else if(frame ==5)
        {
            setImage(run5);
        }
        else if(frame ==6)
        {
            setImage(run6);
            frame = 1;
            return;
        }
        
        frame ++;
    }
    public void animateLeft()
    {
        if (frame == 1)
        {
            setImage(run7);
        }
        else if(frame ==2)
        {
            setImage(run8);
        }
        else if(frame ==3)
        {
            setImage(run9);
        }
        else if(frame ==4)
        {
            setImage(run10);
        }
        else if(frame ==5)
        {
            setImage(run11);
        }
        else if(frame ==6)
        {
            setImage(run12);
            frame = 1;
            return;
        }
        
        frame ++;
    }
    public void fireProjectile()
    {
        if (shotDelay > 0) shotDelay--; 
        if (shotDelay == 0 && Greenfoot.mousePressed(null))
        {           
            shotDelay = 30;
            Projectile projectile = new Projectile();
            getWorld().addObject(projectile, getX(), getY());
            projectile.turnTowards(18000,0);
            projectile.move(110.1800);
            if(animationCounter % 1 == 0)
             animatemouth();
        }
    }
    public void animatemouth()
    {
        if (frame == 1)
        {
            setImage(run13);
        }
        else if(frame ==2)
        {
            setImage(run14);
        }
        else if(frame ==3)
        {
            setImage(run15);
        }
        else if(frame ==4)
        {
            setImage(run16);
            frame = 1;
            return;
        }
        
        frame ++;
    }
}
 
Code "Projectile":
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Projectile here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Projectile extends Mover
{
    /**
     * Act - do whatever the Projectile wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    private int speed = 10;
    private int vSpeed = 0;
    private int accel = 1;
    private int jumpHeight= -20;
    private boolean jumping = false;
    
    private GreenfootImage run1 = new GreenfootImage("fireball1.png");
    private GreenfootImage run2 = new GreenfootImage("fireball2.png");
    private GreenfootImage run3 = new GreenfootImage("fireball3.png");
    private GreenfootImage run4 = new GreenfootImage("fireball4.png");
    private GreenfootImage run5 = new GreenfootImage("fireball5.png");
    private GreenfootImage run6 = new GreenfootImage("fireball6.png");
    private GreenfootImage run7 = new GreenfootImage("fireball7.png");
    private GreenfootImage run8 = new GreenfootImage("fireball8.png");
    private GreenfootImage run9 = new GreenfootImage("fireball9.png");
    private int frame = 1;
    private int animationCounter = 0;
    public Projectile()
    {
        
    }
    public void act() 
    {
        move(10.0);
        moveAround();
        animationCounter ++;
    }    
    public void moveAround()
    {
        if(animationCounter % 8 == 0)
          animatefireball();
    }
    public void turnToMouse()
    {
        turnTowards(15000,0);
    }
    public void animatefireball()
    {
        if (frame == 1)
        {
            setImage(run1);
        }
        else if(frame ==2)
        {
            setImage(run2);
        }
        else if(frame ==3)
        {
            setImage(run3);
        } 
        else if(frame ==4)
        {
            setImage(run4);
        } 
        else if(frame ==5)
        {
            setImage(run5);
        } 
        else if(frame ==6)
        {
            setImage(run6);
        } 
        else if(frame ==7)
        {
            setImage(run7);
        } 
        else if(frame ==8)
        {
            setImage(run8);
        } 
        else if(frame ==9)
        {
            setImage(run9);
            frame = 1;
            return; 
        }
        
        frame ++;
    }
}
danpost danpost

2021/2/20

#
If you would rather not compare ALL the possible images to the current image, I would suggest you add a boolean to track the currently facing direction. Update it when a movement key is pressed and use it to determine shooting direction.
You need to login to post a reply.