I have a bear trap class thats supposed to activate and play a closing animation , holding the animal in place for a few seconds and dealing a bit of damage . The problem is that the animation is playing as soon as the animal touches the edge of the trap, so it holds the animal in place and does damage, but the animal isnt in the bear trap, its to the right or left of it. How would i make it activate only when it touches the centre of the image.
Bear trap code:
Animal code:
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.util.ArrayList; /** * Write a description of class Beartrap here. * * @author (your name) * @version (a version number or a date) */ public class Beartrap extends Traps { /** * Act - do whatever the Beartrap wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ GreenfootImage[] currAnim; int animTimer; private static final int frameDuration = 10; public Beartrap() { GreenfootImage[] bearAni = new GreenfootImage[3]; for (int i=0; i<3; i++) { bearAni[i] = new GreenfootImage("beartrapAni/bearTrap" + i + ".png"); bearAni[i].scale (60, 15); } setAnimation(bearAni); } public void checkHitAnimal() { Animal a = (Animal)getOneIntersectingObject(Animal.class); if (a != null) { setImage(); } } public void act() { checkHitAnimal(); } private void setAnimation(GreenfootImage[] anim) { currAnim = anim; animTimer = -1; setImage(); } private void setImage() { animTimer = (animTimer+1)%(frameDuration*currAnim.length); if (animTimer%frameDuration == 0) { if (currAnim[currAnim.length-1] == getImage()) getWorld().removeObject(this); setImage(currAnim[animTimer/frameDuration]); } } }
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; 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(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 }