I am trying to make my character throw knives as a weapon. At the beginning of the game, I want it to be where you pick up the knife, while it is stationary, and once you pick it up then you can throw them. I thought this would work.
First, I have a boolean, hasKnife, that is false;
Second, I make a method that removes the knife when you touch it, and sets the boolean to true:
After that I have the method to actually throw the knives:
To make sure that you couldn't do throwKnife() without picking up the knife first, I did this in the act method:
So why doesn't it work? The stationary knife doesn't disappear, and it spawns knife when ever you press space.
public void pickUpKnife()
{
Actor tKnife;
tKnife = getOneObjectAtOffset(0, 0, ThrowingKnife.class);
if(tKnife != null)
{
World world = getWorld();
getWorld().removeObject(tKnife);
}
hasKnife = true;
}
public void throwKnife()
{
if(Greenfoot.isKeyDown("space"))
{
getWorld().addObject(new ThrowingKnife(), getX(), getY());
}
}
public void act()
{
hitEnemy();
movement();
collision();
if(hasKnife = false)
{
pickUpKnife();
}
if(hasKnife = true)
{
throwKnife();
}
}
