I have a rope trap that upon contact with the animal, I want to trigger a cage above the animal to fall down, and trap the animal under it for a couple of seconds. how would i make the cage fall down?
Cage code:
Rope code:
Animal code:
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class BearTrap here. * * @author (your name) * @version (a version number or a date) */ public class Cage extends Effects { private int trapState; // 0 for closed, 1 for open private int speed; public Cage() { GreenfootImage Cage = new GreenfootImage("cage.png"); setImage(Cage); } public void act() { // check for collision with bear } }
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.util.ArrayList; /** * Write a description of class Rope here. * * @author (your name) * @version (a version number or a date) */ public class Rope extends Traps { /** * Act - do whatever the Rope wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public Rope() { GreenfootImage image = getImage(); image.scale(image.getWidth() - 160, image.getHeight() - 210); setImage(image); } public void checkHitAnimal() { ArrayList<Animal> h = (ArrayList<Animal>)getObjectsAtOffset(0,0, Animal.class); if (h.size() > 0) { getWorld().addObject (new Cage(), this.getX(), this.getY()); getWorld().removeObject(this); } } public void act() { checkHitAnimal(); } }
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Hunter here. * * @author (your name) * @version (a version number or a date) */ public class Animal extends Actor { /** * Act - do whatever the Hunter wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ protected boolean poisoned; protected int poisonTimer; public int health = 60; private int timer = 5; private int speed = -2; protected boolean shocked; protected int shockTimer; protected boolean grabbed; protected int grabTimer; protected boolean caged; protected int cagedTimer; public Animal() { GreenfootImage image = getImage(); image.scale(image.getWidth() - 260, image.getHeight() - 250); setImage(image); } public void act() { if (isTouching(Electric.class) && shockTimer == 0) shocked = true; if (shocked) { if (++shockTimer == 150) { shocked = false; health--; if (health <= 0) { getWorld().removeObject(this); } } return; } else if (shockTimer > 0) shockTimer--; if (isTouching(Cage.class) && cagedTimer == 0) caged = true; if (caged) { if (++cagedTimer == 150) { shocked = false; health--; if (health <= 0) { getWorld().removeObject(this); } } return; } else if (cagedTimer > 0) cagedTimer--; if (isTouching(Beartrap.class) && grabTimer == 0) grabbed = true; if (grabbed) { if (++grabTimer == 150) { grabbed = false; health--; if (health <= 0) { getWorld().removeObject(this); } } return; } else if (grabTimer > 0) grabTimer--; move(speed); if (health == 0) { getWorld().removeObject(this); } if (isTouching(Poison.class)) poisoned = true; poisonEffect(); } protected void poisonEffect() { if ( ! poisoned ) return; poisonTimer = (poisonTimer+1)%20; if (poisonTimer != 0) return; health--; if (health == 0) getWorld().removeObject(this); } //shocked does not work yet }