I am trying to add a bullet to my character where he can only shoot left and right and I am unclear on how to make any of the code. I was just wondering if anybody has any source code on how to add bullets to my game thanks.
if (<< conditions to fire >>)
{
Bullet b = new Bullet();
b.setRotation(180*Greenfoot.getRandomNumber(2));
addObject(b, getX(), getY());
}import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
public class Bullet extends Actor
{
private int direction,speed;
public Bullet(int rotation)
{
direction = rotation; //passing value
speed = 15;
}
public void act()
{
if(IsOutOfRange())
getWorld().removeObject(this);
setRotation(direction);
move(speed);
}
private boolean IsOutOfRange()
{
if(this.getX() == 999) //World Edge
return true;
return false;
}
}
public class Character extends Actor
{
private int reloadtime = 10,
multiplier = 5;
magSize = 10,
currentAmmo = magSize,
charWidth = getImage().getWidth()-45;
public void act()
{
if(wait > 0)
wait--;
if(Greenfoot.isKeyDown("space"))
if(wait == 0)
Shoot();
}
private void Shoot()
{
if(currentAmmo > 0)
currentAmmo--;
getWorld().addObject(new Bullet(getRotation()), getX()+charWidth, getY());
Greenfoot.playSound("/SFX/shoot.wav");
wait += (currentAmmo > 0) ? reloadtime : reloadtime*multiplier;
}
}