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

2021/12/22

Lava/Spikes Issue

KaiTheLesbian KaiTheLesbian

2021/12/22

#
I'm having trouble with getting my character to respawn when it touches my lava class *I'd also like to do this when my character touches my spike class* I want my character to go back to its original set location after coming in contact with either lava or spikes.
roonie01 roonie01

2021/12/22

#
do a command something like this
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class enemy1 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class enemy1 extends Actor
{
    /**
     * Act - do whatever the enemy1 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */

    private int x0, y0; // original coordinates
    private int vX, vY; // horizontal and vertical speeds

    protected void addedToWorld(World world)
    {
        x0 = getX();
        y0 = getY();
        int speed = 3; // adjust as required
        if (y0 <= 30)  vY = speed; else vX = speed;
    }

    public void act()
    {
        setLocation(getX()+vX, getY()+vY);
        if (isAtEdge() || isTouching(Rocket.class))
        {

            setLocation(x0, y0);
        }
        else if(isTouching(Laser.class))
        {
            setLocation(x0,y0);
        }
        }
    }
You need to login to post a reply.