I have a "shock trap" that when touched, makes an electric explosion. How would i code it so when an animal touches it, it stays in place for a few seconds and takes a bit of damage?
Electric explosion code:
Shock Trap code:
Animal code:
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Explosion here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Electric extends Effects
{
GreenfootImage[] currAnim;
int animTimer;
private static final int frameDuration = 3;
public Electric()
{
GreenfootImage[] electricAni = new GreenfootImage[10];
for (int i=0; i<10; i++)
{
electricAni[i] = new GreenfootImage("etile" + i + ".png");
electricAni[i].scale (100, 100);
}
setAnimation(electricAni);
}
public void act()
{
//this.getImage().setTransparency(getImage().getTransparency() - 3); //slowly becomes transparent
removeTouching(Animal.class);
setImage();
/*if (hunter != null)
{
getWorld().removeObject(hunter);
}
if (this.getImage().getTransparency() < 5)
{
getWorld().removeObject(this);
}*/
}
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)
import java.util.ArrayList;
/**
* Write a description of class elandmine here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Elandmine extends Traps
{
/**
* Act - do whatever the elandmine wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public Elandmine()
{
GreenfootImage image = getImage();
image.scale(image.getWidth() - 200, image.getHeight() - 145);
setImage(image);
}
public void checkHitAnimal()
{
ArrayList<Animal> h = (ArrayList<Animal>)getObjectsAtOffset(0,0, Animal.class);
if (h.size() > 0)
{
getWorld().addObject (new Electric(), 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.
*/
public int health = 10;
public Animal()
{
GreenfootImage image = getImage();
image.scale(image.getWidth() - 260, image.getHeight() - 250);
setImage(image);
}
public void act()
{
move(-2);
}
}
