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

2019/5/31

Shooting Direction

Verglas Verglas

2019/5/31

#
Hi! I'm having trouble with my coding for a shooter game and I would like to have some advice and help. Currently, I'm having two problems. First, I can't shoot according to where my player character is facing. It will always shoot downwards. Second, The checkBoundaries and destroyEnemies methods are always clashing. If I put the checkBoundaries ABOVE destroyEnemies in the act method, it will pop out an error when I destroy an enemy but not when the bullet hits the edge of the board. Vice versa, if I put the checkBoundaries BELOW destroyEnemies in the act method, it will pop out an error when the bullet hits the edge but not when the bullet destroys an enemy. May I have some help please? Here is the code for my Player class.
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 GreenfootImage left = new GreenfootImage("LeftMC.png");
    private GreenfootImage right= new GreenfootImage("FrontMC.png");
    private GreenfootImage up= new GreenfootImage("BackMC.png");
    private GreenfootImage down= new GreenfootImage("FrontMC.png");
    
    public void act() 
    {
        turnImage();
        fireBullet();
        movePlayer();
        playerHit();
    }
    
    public void turnImage()
    {
        if(Greenfoot.isKeyDown("s"))
        {
            setImage("FrontMC.png");
        }
        if(Greenfoot.isKeyDown("w"))
        {
            setImage("BackMC.png");
        }
        if(Greenfoot.isKeyDown("a"))
        {
            setImage("LeftMC.png");
        }
        if(Greenfoot.isKeyDown("d"))
        {
            setImage("RightMC.png");
        }
    }
    
    public void movePlayer()
    {
        if(Greenfoot.isKeyDown("s"))
        {
            setLocation(getX(), getY() + 3);
        }
        if(Greenfoot.isKeyDown("w"))
        {
            setLocation(getX(), getY() - 3);
        }
        if(Greenfoot.isKeyDown("a"))
        {
            setLocation(getX() - 3, getY());
        }
        if(Greenfoot.isKeyDown("d"))
        {
            setLocation(getX() + 3, getY());
        }
    }
    
    private boolean spaceDown;
    public void fireBullet()
    {
      Bullet bullet = new Bullet();
      if(!spaceDown && Greenfoot.isKeyDown("space")) {
           spaceDown = true;
           getWorld().addObject(bullet, getX(), getY());
           if (getImage().equals("left")) bullet.setRotation(270);
           else if (getImage().equals("down")) bullet.setRotation(180);
           else if (getImage().equals("up")) bullet.setRotation(0);
           else bullet.setRotation(90);
      }
      if(spaceDown && !Greenfoot.isKeyDown("space")){
           spaceDown = false;
      }
        
    }
  
    
   //public void fireBullet()
    {
        if(!spaceDown && Greenfoot.isKeyDown("space")) {
            spaceDown = true;
            getWorld().addObject(new Bullet(), getX(), getY());
        }
        if(spaceDown && !Greenfoot.isKeyDown("space")){
            spaceDown = false;
        }
   }
    
   public void playerHit()
    {
        Actor ghost = getOneIntersectingObject(ghost.class);
        if(ghost != null)
        {
            World myWorld = getWorld();
            myWorld.removeObject(this);
            
            //Game Over Screen
        }
    }
    
}
Here is the code for my Bullet class.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Bullet here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Bullet extends Actor
{
    /**
     * Act - do whatever the Bullet wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    int maxDistanceTravelled = 100;
    //private int speed = 5;
    public void act()
   {
       //bulletMove();
       move(4);
       destroyEnemies();
       checkBoundaries();
       bulletRange();
   }  
   
   public void bulletMove()
   {
       if(this.getRotation() == 90)
       {
          move(4);
       }
       if(this.getRotation() == 270)
       {
           move(-4);
       }
       if(this.getRotation() == 0)
       {
           setLocation(getX(), getY() - 4);
       }
       if(this.getRotation() == 90)
       {
           setLocation(getX(), getY() + 4);
       }
   }
   //we add a method "checkBoundaries()" that destroys bullets that are off screen.
   public void checkBoundaries()
   {
       World myWorld = getWorld();
       if(this != null){
       if(getX() > myWorld.getWidth() - 1) 
            myWorld.removeObject(this);
       else if(getX() < 1) 
            myWorld.removeObject(this);
       if(getY() > myWorld.getHeight() - 1) 
            myWorld.removeObject(this);
       else if(getY() < 1) 
            myWorld.removeObject(this);
       }     
   }
   //"destroyEnemies()" destroys enemies.
   public void destroyEnemies()
   {
       //"Enemy" can be any class that you want the bullet to destroy. 
       if(this != null){
       Actor enemy = getOneIntersectingObject(ghost.class);
       if(enemy != null) {
            getWorld().removeObject(enemy);
            getWorld().removeObject(this);
       }
    }
   }
   
   public void bulletRange(){
       maxDistanceTravelled -= 1;
       if(maxDistanceTravelled == 0)
       {
           getWorld().removeObject(this);
       }
    }
}

danpost danpost

2019/5/31

#
For the second issue, place the following line as the first line in all 3 methods called from act in the Bullet class:
if (getWorld() == null) return;
For the first one, remove the quotation marks in lines 76 thru 78 of the Player class.
Verglas Verglas

2019/5/31

#
Thank you very much :D I will try it out.
Verglas Verglas

2019/5/31

#
The second issue of getting error messages is solved. But I still can't fire anywhere else other than downwards. Here is the current code for the Player class.
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 GreenfootImage left = new GreenfootImage("LeftMC.png");
    private GreenfootImage right= new GreenfootImage("FrontMC.png");
    private GreenfootImage up= new GreenfootImage("BackMC.png");
    private GreenfootImage down= new GreenfootImage("FrontMC.png");
    
    public void act() 
    {
        turnImage();
        fireBullet();
        movePlayer();
        playerHit();
    }
    
    public void turnImage()
    {
        if(Greenfoot.isKeyDown("s"))
        {
            setImage("FrontMC.png");
        }
        if(Greenfoot.isKeyDown("w"))
        {
            setImage("BackMC.png");
        }
        if(Greenfoot.isKeyDown("a"))
        {
            setImage("LeftMC.png");
        }
        if(Greenfoot.isKeyDown("d"))
        {
            setImage("RightMC.png");
        }
    }
    
    public void movePlayer()
    {
        if(Greenfoot.isKeyDown("s"))
        {
            setLocation(getX(), getY() + 3);
        }
        if(Greenfoot.isKeyDown("w"))
        {
            setLocation(getX(), getY() - 3);
        }
        if(Greenfoot.isKeyDown("a"))
        {
            setLocation(getX() - 3, getY());
        }
        if(Greenfoot.isKeyDown("d"))
        {
            setLocation(getX() + 3, getY());
        }
    }
    
    private boolean spaceDown;
    public void fireBullet()
    {
      Bullet bullet = new Bullet();
      if(!spaceDown && Greenfoot.isKeyDown("space")) {
           spaceDown = true;
           getWorld().addObject(bullet, getX(), getY());
           if (getImage().equals(left)) bullet.setRotation(270);
           else if (getImage().equals(down)) bullet.setRotation(180);
           else if (getImage().equals(up)) bullet.setRotation(0);
           else bullet.setRotation(90);
      }
      if(spaceDown && !Greenfoot.isKeyDown("space")){
           spaceDown = false;
      }
        
    }
  
    
   //public void fireBullet()
    {
        if(!spaceDown && Greenfoot.isKeyDown("space")) {
            spaceDown = true;
            getWorld().addObject(new Bullet(), getX(), getY());
        }
        if(spaceDown && !Greenfoot.isKeyDown("space")){
            spaceDown = false;
        }
   }
    
   public void playerHit()
    {
        Actor ghost = getOneIntersectingObject(ghost.class);
        if(ghost != null)
        {
            World myWorld = getWorld();
            myWorld.removeObject(this);
            
            //Game Over Screen
        }
    }
    
}
Here is the code for Bullet class.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Bullet here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Bullet extends Actor
{
    /**
     * Act - do whatever the Bullet wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    int maxDistanceTravelled = 100;
    //private int speed = 5;
    public void act()
   {
       //bulletMove();
       move(4);
       destroyEnemies();
       checkBoundaries();
       bulletRange();
   }  
   
   public void bulletMove()
   {
       if (getWorld() == null) return;
       if(this.getRotation() == 90)
       {
          move(4);
       }
       if(this.getRotation() == 270)
       {
           move(-4);
       }
       if(this.getRotation() == 0)
       {
           setLocation(getX(), getY() - 4);
       }
       if(this.getRotation() == 90)
       {
           setLocation(getX(), getY() + 4);
       }
   }
   //we add a method "checkBoundaries()" that destroys bullets that are off screen.
   public void checkBoundaries()
   {
       if (getWorld() == null) return;
       World myWorld = getWorld();
       if(this != null){
       if(getX() > myWorld.getWidth() - 1) 
            myWorld.removeObject(this);
       else if(getX() < 1) 
            myWorld.removeObject(this);
       if(getY() > myWorld.getHeight() - 1) 
            myWorld.removeObject(this);
       else if(getY() < 1) 
            myWorld.removeObject(this);
       }     
   }
   //"destroyEnemies()" destroys enemies.
   public void destroyEnemies()
   {
       //"Enemy" can be any class that you want the bullet to destroy.
       if (getWorld() == null) return;
       if(this != null){
       Actor enemy = getOneIntersectingObject(ghost.class);
       if(enemy != null) {
            getWorld().removeObject(enemy);
            getWorld().removeObject(this);
       }
    }
   }
   
   public void bulletRange(){
       maxDistanceTravelled -= 1;
       if(maxDistanceTravelled == 0)
       {
           getWorld().removeObject(this);
       }
    }
}

danpost danpost

2019/5/31

#
Replace the string literals with the appropriate variable names in your turnImage method.
Verglas Verglas

2019/5/31

#
Thank you very much! Works perfectly after having to change some of the rotation angles. I don't know why but it seems that the bullet is facing right when the program starts; right is 0, down is 90 and so on. Took me a few moment to realize XD.
You need to login to post a reply.