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

2016/4/13

Make both players shoot

rasm214c rasm214c

2016/4/13

#
My problem is that only one player can shoot at a time. If both players a alive, only player 2 can shoot, and if player 2 is gone, player one can shoot. Code for player 1.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
/**
 * Write a description of class car here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class car extends vehicle
{
    Bullet bullet = new Bullet();
    /**
     * Act - do whatever the car wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        hitObject(); 
        Move();
        if ("space".equals(Greenfoot.getKey()))
        {
            fire();
        }

    }

    public void Move()
    {
        if(Greenfoot.isKeyDown("left"))
        {
            setRotation(getRotation()-5);
        }
        if(Greenfoot.isKeyDown("right"))
        {
            setRotation(getRotation()+5);
        }
        if(Greenfoot.isKeyDown("up"))
        {
            move(7);
        }
        if(Greenfoot.isKeyDown("down"))
        {
            move(-7);
        }  

    }

    public String hitTheEdge()
    {
        int x = getX();
        int y = getY();
        World myWorld = getWorld();
        int rightSide = myWorld.getWidth() -1;
        int bottomSide = myWorld.getHeight() -1;
        if(y == 0)
        {
            return "top";
        } else if(x == 0)
        {
            return "left";
        } else if (x == rightSide)
        {
            return "right";
        } else if(y == bottomSide)
        {
            return "bottom";
        } else {
            return null;
        }
    }

    public void hitObject()
    {
        Actor object = getOneIntersectingObject(object.class);
        if(object != null)
        {
            World myWorld = getWorld();
            GameOver gameover = new GameOver();
            myWorld.addObject(gameover, myWorld.getWidth()/2, myWorld.getHeight()/2);
            myWorld.removeObject(this);
        }
    }

    private void fire()
    {
        Bullet bullet = new Bullet();
        getWorld().addObject(bullet, getX(), getY());
        bullet.setRotation(getRotation());
    }
}
code for player 2
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
/**
 * Write a description of class car here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class car2 extends Actor
{
    Bullet bullet = new Bullet();
    /**
     * Act - do whatever the car wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        hitObject(); 
        Move();
        if ("q".equals(Greenfoot.getKey()))
        {
            fire1();
        }

    }

    public void Move()
    {
        if(Greenfoot.isKeyDown("a"))
        {
            setRotation(getRotation()-5);
        }
        if(Greenfoot.isKeyDown("d"))
        {
            setRotation(getRotation()+5);
        }
        if(Greenfoot.isKeyDown("w"))
        {
            move(7);
        }
        if(Greenfoot.isKeyDown("s"))
        {
            move(-7);
        }  

    }

    public String hitTheEdge()
    {
        int x = getX();
        int y = getY();
        World myWorld = getWorld();
        int rightSide = myWorld.getWidth() -1;
        int bottomSide = myWorld.getHeight() -1;
        if(y == 0)
        {
            return "top";
        } else if(x == 0)
        {
            return "left";
        } else if (x == rightSide)
        {
            return "right";
        } else if(y == bottomSide)
        {
            return "bottom";
        } else {
            return null;
        }
    }

    public void hitObject()
    {
        Actor object = getOneIntersectingObject(object.class);
        if(object != null)
        {
            World myWorld = getWorld();
            GameOver gameover = new GameOver();
            myWorld.addObject(gameover, myWorld.getWidth()/2, myWorld.getHeight()/2);
            myWorld.removeObject(this);
        }
    }

    private void fire1()
    {
        Bullet1 bullet1 = new Bullet1();
        getWorld().addObject(bullet1, getX(), getY());
        bullet1.setRotation(getRotation());
    }
}
danpost danpost

2016/4/13

#
You are using 'Greenfoot.getKey()' in two different places. It will not continually return the same value. Once a value is returned, the next key, or null, will be returned. Use 'Greenfoot.isKeyDown("?")' with a timer or a boolean that tracks the state of the key.
rasm214c rasm214c

2016/4/14

#
but when i use "Greenfoot.isKeyDown" then i will shoot many times a second instead of only once på click? how to solve?
danpost danpost

2016/4/14

#
rasm214c wrote...
but when i use "Greenfoot.isKeyDown" then i will shoot many times a second instead of only once på click? how to solve?
danpost wrote...
Use 'Greenfoot.isKeyDown("?")' with a timer or a boolean that tracks the state of the key.
The last phrase says it all. Use it with an int timer or a boolean that tracks the state of the key. A timer will allow continuous regulated shooting while the key is held down. A boolean that tracks the state of the field can be used to only shoot when the key changes state (from up to down or from down to up).
You need to login to post a reply.