i don't know how to type a script to shoot the object...
help me please :o
public void act()
{
checkFire();
//leave the rest of your player's act method alone
}
//after the "act()" method, add a new method:
public void checkFire()
{
if(Greenfoot.isKeyDown("space")) {
getWorld().addObject(new Bullet(), getX(), getY());
}
}
public Bullet extends Actor
{
public void act()
{
setLocation(getX() + speed, getY());
checkBoundaries();
destroyEnemies();
}
//we add a method "checkBoundaries()" that destroys bullets that are off screen.
public void checkBoundaries()
{
if(getX() > getWorld().getWidth() - 1)
getWorld().removeObject(this);
else if(getX() < 1)
getWorld().removeObject(this);
if(getY() > getWorld().getHeight() - 1)
getWorld().removeObject(this);
else if(getY() < 1)
getWorld().removeObject(this);
}
//"destroyEnemies()" destroys enemies.
public void destroyEnemies()
{
//"Enemy" can be any class that you want the bullet to destroy.
Actor enemy = getOneIntersectingObject(Enemy.class);
if(enemy != null) {
getWorld().removeObject(enemy);
getWorld().removeObject(this);
}
}
private int speed = 10;
}