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

2015/11/13

game controller with game

TheBrains1001 TheBrains1001

2015/11/13

#
So I want to configure my controller to my game, but one of the buttons i want to use is a left mouse click. How do i use Greenfoot.isKeyDown() to do this?
import greenfoot.*;

/**
 * Write a description of class Ship here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Ship extends Actor
{
    private int canShoot = 0;
    private int lives = 3;
    /**;
     * Act - do whatever the Ship wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        keyCommand();
        hitbox();
        if(lives == 0) 
        {
            getWorld().removeObject(this);
            Greenfoot.stop();
        }
    } 
    public void keyCommand()
    {
        if(Greenfoot.isKeyDown("up"))
        {
            up();
        }
        if(Greenfoot.isKeyDown("down"))
        {
            down();
        }
        if(Greenfoot.isKeyDown("left"))
        {
            left();
        }
        if(Greenfoot.isKeyDown("right"))
        {
            right();
        }
        if(Greenfoot.isKeyDown("space") && (canShoot<=0))
        {
            fire();
            Greenfoot.playSound("pew.mp3");
            canShoot = 25;
        }
        canShoot--;
    }
    public void up()
    {
        setLocation(getX(),getY()-5);
    }
    public void down()
    {
       setLocation(getX(),getY()+5); 
    }
    public void left()
    {
        move(-5);
    }
    public void right()
    {
        move(5);
    }
    public void fire()
    {
        getWorld().addObject(new Missle(), getX(), getY());
    }
        private void respawn()
    {
        setLocation(getWorld().getWidth(), Greenfoot.getRandomNumber(200));
    }
    public void hitbox()
    {
        if(getOneIntersectingObject(Alien.class)!=null)
        {
            getWorld().addObject(new Explosion(), getX(),getY());
            Greenfoot.playSound("explosion.wav");
            Actor alien = getOneIntersectingObject(Alien.class);
            getWorld().addObject(new Ship(), Greenfoot.getRandomNumber(600), Greenfoot.getRandomNumber(400));
            getWorld().removeObject(this);
        }
        
    }
}
danpost danpost

2015/11/13

#
Maybe you should review the Greenfoot class API documentation as the 'isKeyDown' method is for keyboard control, not mouse control. After looking there, you may also need to review the MouseInfo class API documentation to see how to proceed.
TheBrains1001 TheBrains1001

2015/11/13

#
TY
You need to login to post a reply.