Currently, I am trying to make my player pick up a weapon. Here is the code:
I have tried to use the getKey() method and have been unsuccessful, I do not know how to properly implement it. Can anyone help?
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Rifle here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Rifle extends Weapon
{
private Killable k;
public boolean canFire;
public int weaponCheck;
public String g;
public Rifle(Killable k_)
{
k = k_;
canFire = false;
weaponCheck = 1;
g = new String("g");
}
/**
* Act - do whatever the Rifle wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
// Add your action code here.
Actor a = (Actor)k;
if(a.getWorld() == null)
{
getWorld().removeObject(this);
}
if(Greenfoot.mousePressed(null))
{
canFire = true;
}
if(Greenfoot.mouseClicked(null))
{
canFire = false;
}
if(findPlayer(40))
{
if(Greenfoot.isKeyDown("g"))
{
weaponCheck = 2;
if((weaponCheck/2) == 1)
{
setRotation(a.getRotation());
setLocation(a.getX(), a.getY());
setWeaponLocation(this, 25, -12);
}
}
shoot();
}
}
public void createBullet()
{
Bullet b = new Bullet(this);
triangulateBulletLocation(b, getImage().getWidth()/2+1, 0);
}
public void shoot()
{
if(Greenfoot.getMouseInfo() != null)
{
if(canFire)
createBullet();
}
}
}
