This site requires JavaScript, please enable it in your browser!
Greenfoot back
KCee
KCee wrote ...

2023/4/18

I wanna make an electric effect that stuns animals that touch it for a few seconds

KCee KCee

2023/4/18

#
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:
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]);
        }
    }
}
Shock Trap code:
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();        
    }
}
Animal code:
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);
    }
}
danpost danpost

2023/4/19

#
KCee wrote...
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? << Codes Omitted >>
I guess it would be done similar to how the poison effect works. In Animal class:
protected boolean shocked;
protected int shockTimer;

// in act (before movement code)
    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--;
Since you want the animal to freeze in place, where it still touches the Electric object, there is a need to give some time for the animal to get off the object before getting another shock. Hence, shock occurs only when timer is zero and timer steps back down to zero once shock itself ends.
KCee KCee

2023/4/20

#
how would i make the animal stay in place? would i put (speed = 0) under health--;?
danpost danpost

2023/4/20

#
KCee wrote...
how would i make the animal stay in place? would i put (speed = 0) under health--;?
I thought I took care of that with line 12. However, it seems to be misplaced. Move line 12 two lines down to have lines 6 through 15 look like this:
if (shocked) {
    if (++shockTimer == 150) {
        shocked = false;
        health--;
        if (health <= 0) {
            getWorld().removeObject(this);
        }
    }
    return;
}
As long as the provided code is placed in the act method before the movement code, It should now work as required.
You need to login to post a reply.