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

2023/4/21

I wanna make a cage fall from the sky

KCee KCee

2023/4/21

#
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:
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
    }
}
Rope code:
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();        
    }
}
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.
     */
    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
}
danpost danpost

2023/4/21

#
KCee wrote...
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? << Code Omitted >>
In line 27 of Rope class, you probably want the y-coordinate of the addObject line to be zero. Then, in the act method of the Cage class, you need the falling code:
speed++;
setLocation(getX(), getY()+speed);
if (/** on ground conditions */) {
    // adjust position to be on top of ground (must be precise here)
    if (speed > 1) {
        // inform animal it is trapped (animal then continuously checks for cage, freezes, and loses health when released)
        speed = 0;
    }
}
Line 3 could be something like:
if (getY() > getWorld().getHeight()-getImage().getHeight()/2-1) }
where the positional adjustment would be:
setLocation(getX(), getWorld().getHeight()-getImage().getHeight()/2-1);
Or, it could be something like:
if (isTouching(Ground.class)) {
where the positional adjustment would be something like this:
Actor ground = (Actor)getOneIntersectingObject(Ground.class);
setLocation(getX(), ground.getY()-ground.getImage().getHeight()/2-getImage().getHeight()/2-1);
Or, it could be a combination of the two.
You need to login to post a reply.