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

2022/1/26

when the player is attacked by Zombie they can't shoot.

Soop__ Soop__

2022/1/26

#
the player can shoot bullets fine until the zombie is touching the player which i don't know why this happens. this is 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
{
    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;
            }
        }
    }
}
this is bullet:
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();
        }
    }
this is zombie:
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();
            
        }
    }
}
i want it so that the player can shoot while being attacked by zombies
danpost danpost

2022/1/26

#
Soop__ wrote...
i want it so that the player can shoot while being attacked by zombies
I would think line 90 in the Player class is suspect. Remove that line as it keeps the delay in effect while touching a zombie.
Spock47 Spock47

2022/1/26

#
You are using two variables called "delay" and "delay2" and I think you are confusing them in your source code. One place to see this is the one mentioned by danpost. Another one is lines 79,81:
        if (delay > 0)
        {
            delay2 = delay2-1;
        }
You are checking for delay, but dealing with delay2. That's quite suspicious. Also, delay2 is never set in any place. My guess is, that you actually wanted to use delay2 in line 79 and line 90 (which danpost mentioned). But I can't be totally sure since the names "delay" and "delay2" don't tell me really what these variables are for. A way to prevent this kind of problems is to give the variables expressive names, e.g. "bulletDelay" and "zombieDelay" (or similar). If the variables would have these names, one would immediately see "ah, bulletDelay is used in the zombie method... problem found and solved". Live long and prosper, Spock47
Soop__ Soop__

2022/1/27

#
yes i think i was a bit blind with my code ive just seen it, ill change it to more a appropriate wording , thank you :) ill come back if it still isnt working.
You need to login to post a reply.