SO: I have this game thing that a friend and I are making for a class and we want our actor to shoot poison. The poison is appearing on the screen when the down key is pressed, which is what we want, except it just sort of sits where it was created. It doesn't move across the screen. Can anyone help with this?
Poison class code:
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
public class Poison extends Mover
{
/** A bullet loses one life each act, and will disappear when life = 0 */
public int life = 30;
/** The damage this bullet will deal */
public int damage = 16;
/**
* Act - do whatever the Poison wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
private int direction, speed;
private Vector movement = new Vector();
public void act()
{
if(life <= 0) {
getWorld().removeObject(this);
return;
}
else if(getX() <= 0) {
move();
CatZombie yeet = (CatZombie) getOneIntersectingObject(CatZombie.class);
if (yeet != null) {
getWorld().removeObject(this);
yeet.hit(damage);
}
else {
life--;
}
setRotation(direction);
setLocation(getX() + speed, getY());
move(50);
checkBoundaries();
}
}
public Poison()
{
//make image smaller
GreenfootImage image = getImage();
image.scale(image.getWidth()- 10, image.getHeight()-10);
setImage(image);
speed = 100;
}
public void checkBoundaries()
{
//make poison disappear if it gets off screen
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);
}
}
Actor code:
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
public class DogDoctor extends Mover
{
public boolean isDown = false;
private int ySpeed;
private int speed = 7;
public int health = 20;
public GreenfootImage image = getImage();
//sizes doctor to be smaller
public DogDoctor()
{
//public GreenfootImage image = getImage();
image.scale(image.getWidth() - 100, image.getHeight()- 150);
setImage(image);
}
public void act()
{
checkKeys();
checkFire();
move(1);
//Jumping
int groundLevel = 500;
boolean onGround = (getY() == groundLevel);
if (!onGround) // in middle of jump
{
ySpeed++; // adds gravity effect
setLocation(getX(), getY()+ySpeed); // fall (rising slower or falling faster)
if (getY()>=groundLevel) // has landed (reached ground level)
{
setLocation(getX(), groundLevel); // set on ground
Greenfoot.getKey(); // clears any key pressed during jump
}
}
else // on ground
{
if ("space".equals(Greenfoot.getKey())) // jump key detected
{
ySpeed = -25; // add jump speed
setLocation(getX(), getY()+ySpeed); // leave ground
}
}
//Cat kills dog
if (isTouching(CatZombie.class)) {
getWorld().removeObject(this);
}
}
public void checkFire()
{
if(Greenfoot.isKeyDown("down")) {
getWorld().addObject(new Poison(), getX(), getY());
}
}
private boolean checkKeys()
{
//move left
if(Greenfoot.isKeyDown("left"))
{
setImage("TheDoctor-Left.png");
GreenfootImage doctorLeft = getImage();
doctorLeft.scale(image.getWidth(), doctorLeft.getHeight() - 150);
setImage(doctorLeft);
moveLeft();
}
//move right
if(Greenfoot.isKeyDown("right"))
{
setImage("TheDoctor-Right.png");
GreenfootImage doctorRight = getImage();
doctorRight.scale(image.getWidth(), doctorRight.getHeight() - 150);
setImage(doctorRight);
moveRight();
}
return(isDown);
}
public void moveRight()
{
setLocation( getX() + speed, getY() );
}
public void moveLeft()
{
setLocation( getX() - speed, getY() );
}
public void hit(int damage)
{
life = life - damage;
}
}
