I'm making a game and I can't get my enemy to take lives of me when it hits. I can get the enemy to make me disappear when touched.
Here is my enemy code:
And here is my Main character code:
What do I need to add to both of these, please help.
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Enemy here. * * @author (your name) * @version (a version number or a date) */ public class Enemy extends BadGuys { private int vSpeed = 0; private int acceleration = 2; public int damage = 5; private int spriteHeight = getImage().getHeight(); private int spriteWidth = getImage().getWidth(); private int lookForGroundDistance = (int)spriteHeight/2; private int lookForEdge = (int)spriteWidth/2; private int speed = 1; GreenfootImage run1r = new GreenfootImage("snake2.png"); int frame = 0; private int animationCount; public void act() { checkFall(); move(); Actor a = this.getOneIntersectingObject(Alli.class); if( a != null) { this.getWorld().removeObject(a); } if(speed<0) { animateRight(); } animationCount ++; } public void move() { Actor ground = getOneObjectAtOffset(lookForEdge, lookForGroundDistance, Ground.class); if(ground == null) { speed *= -1; lookForEdge *= -1; } else { move(speed); } } public void fall() { setLocation ( getX(), getY() + vSpeed); if(vSpeed <=9) { vSpeed = vSpeed + acceleration; } } public void moveToGround(Actor ground) { int groundHeight = ground.getImage().getHeight(); int newY = ground.getY() - (groundHeight + getImage().getHeight())/2; setLocation(getX(), newY); } public void checkFall() { if(onGround()) { vSpeed = 0; } else { fall(); } } public boolean onGround() { int spriteHeight = getImage().getHeight(); int yDistance = (int) (spriteHeight / 2 + 5); Actor ground = getOneObjectAtOffset(0, getImage().getHeight()/2, Ground.class); if(ground == null) { return false; } else { moveToGround(ground); return true; } } public void animateRight() { if(frame == 0) { setImage(run1r); } frame ++; } }
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.awt.Color; public class Alli extends Actor { private int speed = 2; private int vSpeed = 0; private int acceleration = 2; private int jumpStrength = 23; private int ySpeed = 1; private boolean jumping; private int direction = 1; private int shootingCounter = 100; private GreenfootImage run1r = new GreenfootImage("Run 1.png"); private GreenfootImage run2r = new GreenfootImage("Run 2.png"); private GreenfootImage run3r = new GreenfootImage("Run 3.png"); private GreenfootImage run4r = new GreenfootImage("Run 4.png"); private GreenfootImage run1l = new GreenfootImage("Run 1L.png"); private GreenfootImage run2l = new GreenfootImage("Run 2L.png"); private GreenfootImage run3l = new GreenfootImage("Run 3L.png"); private GreenfootImage run4l = new GreenfootImage("Run 4L.png"); private int frame = 1; private int animationCounter = 0; private int level; private int lives = 5; private boolean isDead = false; private int timeToRegenerate = 0; private Counter counter; public Alli(Counter pointCounter) { counter = pointCounter; } public Alli() { level = 1; } public void act() { Actor a = this.getOneIntersectingObject(Coins.class); if( a != null) { this.getWorld().removeObject(a); counter.add(1); Greenfoot.playSound("Mario Coin.WAV"); } checkKeys(); checkFall(); platformAbove(); shooting(); shootingCounter --; animationCounter ++; checkNextLevel(); } private void checkNextLevel() { if (getX() == getWorld().getWidth()-1) { if (getWorld() instanceof Desert) Greenfoot.setWorld(new Level3(this)); if (getWorld() instanceof Rocks) Greenfoot.setWorld(new Desert(this)); if (getWorld() instanceof Desert) Greenfoot.setWorld(new Level3(this)); } } private void checkKeys() { if(Greenfoot.isKeyDown("right")) { direction = 1; moveRight(); } if(Greenfoot.isKeyDown("left")) { direction = -1; moveLeft(); } if(Greenfoot.isKeyDown("space") && jumping == false) { jump(); } } public boolean shooting() { if(Greenfoot.isKeyDown("x") && shootingCounter <= 0 && direction ==1) { getWorld().addObject(new ShootRight(), getX(), getY()); shootingCounter = 20; Greenfoot.playSound("Laser.mp3"); return true; } if(Greenfoot.isKeyDown("x") && shootingCounter <= 0 && direction == -1) { getWorld().addObject(new ShootLeft(), getX(), getY()); shootingCounter = 20; Greenfoot.playSound("Laser.mp3"); return true; } return false; } public void jump() { vSpeed = vSpeed - jumpStrength; jumping = true; fall(); } public void fall() { setLocation ( getX(), getY() + vSpeed); if(vSpeed <=9) { vSpeed = vSpeed + acceleration; } jumping = true; } public void moveToGround(Actor ground) { int groundHeight = ground.getImage().getHeight(); int newY = ground.getY() - (groundHeight + getImage().getHeight())/2; setLocation(getX(), newY); jumping = false; } public void checkFall() { if(onGround()) { vSpeed = 0; } else { fall(); } } public boolean onGround() { int spriteHeight = getImage().getHeight(); int yDistance = (int) (spriteHeight / 2 + 5); Actor ground = getOneObjectAtOffset(0, getImage().getHeight()/2, Ground.class); if(ground == null) { jumping = true; return false; } else { moveToGround(ground); return true; } } public boolean platformAbove() { int spriteHeight = getImage().getHeight(); int yDistance = (int) (spriteHeight / -2); Actor ceiling = getOneObjectAtOffset(0, yDistance, Ground.class); if(ceiling != null) { ySpeed =1; bopHead(ceiling); return true; } else { return false; } } public void bopHead(Actor ceiling) { int ceilingHeight = ceiling.getImage().getHeight()/2; int newY = ceiling.getY() + (ceilingHeight + getImage().getHeight())/2; setLocation(getX(), newY); } public void moveRight() { setLocation ( getX() + speed, getY() ); if(animationCounter % 4 == 0) animateRight(); } public void animateRight() { if(frame == 1) { setImage(run1r); } else if(frame == 2) { setImage(run2r); } else if(frame == 3) { setImage(run3r); } else if(frame == 4) { setImage(run4r); frame = 1; return; } frame ++; } public void moveLeft() { setLocation ( getX() - speed, getY() ); if(animationCounter % 4 == 0) animateLeft(); } public void animateLeft() { if(frame == 1) { setImage(run1l); } else if(frame == 2) { setImage(run2l); } else if(frame == 3) { setImage(run3l); } else if(frame == 4) { setImage(run4l); frame = 1; return; } frame ++; } }