the player can shoot bullets fine until the zombie is touching the player which i don't know why this happens.
this is player
this is bullet:
this is zombie:
i want it so that the player can shoot while being attacked by zombies
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 { private int delay = 0; private int delay2 = 0; /** * Act - do whatever the Player wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public Player() { setRotation(270); GreenfootImage image = getImage(); image.scale(55,29); setImage(image); } public void act() { setImage(); chomp(); movement(); shoot(); } public void setImage() { GreenfootImage image = getImage(); image.scale(55,29); setImage(image); } public void movement() { int angle = getRotation(); if (Greenfoot.isKeyDown("w")) { move(3); } if (Greenfoot.isKeyDown("s")) { move(-3); } setRotation(angle); if(Greenfoot.mouseMoved(null)) { MouseInfo mouse = Greenfoot.getMouseInfo(); turnTowards(mouse.getX(), mouse.getY()); } } public void shoot() { if (delay > 0) { delay = delay-1; } else { if (Greenfoot.isKeyDown("space")) { Bullet b = new Bullet(); getWorld().addObject(b, getX(), getY()); b.setRotation(getRotation()); delay = 10; } } } public void chomp() { if (delay > 0) { delay2 = delay2-1; } else { Actor Zombie = getOneIntersectingObject(Zombie.class); if(Zombie != null) { waveZWorld waveWorld = (waveZWorld)getWorld(); waveWorld.loseLife(); delay = 25; } } } }
public void act() { //constant movement of bullet move(10); //sets image scale GreenfootImage image = getImage(); image.scale(20,20); setImage(image); //checks if bullet is touching edge of world or Zombie and removes the sprite Actor Zombie = getOneIntersectingObject(Zombie.class); Actor Medkit = getOneIntersectingObject(Medkit.class); if (isAtEdge()) { getWorld().removeObject(this); } else if(Zombie != null) { waveZWorld waveWorld = (waveZWorld)getWorld(); waveWorld.removeObject(Zombie); waveWorld.removeObject(this); waveWorld.increaseScore(); } }
public class Zombie extends Actor { /** * Act - do whatever the Zombie wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public Zombie() { setRotation(270); GreenfootImage image = getImage(); image.scale(30,37); setImage(image); } public void act() { setImage(); moveAround(); hit(); } public void setImage() { GreenfootImage image = getImage(); image.scale(30,37); setImage(image); } public void moveAround() { move(1); Actor player = getWorld().getObjects(Player.class).get(0); turnTowards(player.getX(), player.getY()); } public void hit() { Actor Bullet = getOneIntersectingObject(Bullet.class); if(Bullet != null) { waveZWorld waveWorld = (waveZWorld)getWorld(); waveWorld.removeObject(this); waveWorld.removeObject(Bullet); waveWorld.increaseScore(); } } }