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

2021/3/18

Wombat movement with left or right arrow

Mutex Mutex

2021/3/18

#
Hello! I've this situation: modify so that if I click on a wombat, it will be selected (idea: in the WombatWorld class add an instance variable that is null if no wombat is selected or a reference to the selected one. If it is selected and I also click on it If I click on another, it will become the selected one). Once a wombat is selected and the water the "<-" key rotates to the left
import greenfoot.*;  // imports Actor, World, Greenfoot, GreenfootImage

import java.util.Random;

/**
 * A world where wombats live.
 * 
 * @author Michael Kolling
 * @version 1.0.1
 */
public class WombatWorld extends World
{
    /**
     * Create a new world with 8x8 cells and
     * with a cell size of 60x60 pixels
     */
    public WombatWorld() 
    {
        super(8, 8, 60);        
        setBackground("cell.jpg");
    }

    /**
     * Populate the world with a fixed scenario of wombats and leaves.
     */    
    public void populate()
    {
        Wombat w1 = new Wombat();
        addObject(w1, 3, 3);
        
        Wombat w2 = new Wombat();
        addObject(w2, 1, 7);

        Leaf l1 = new Leaf();
        addObject(l1, 5, 3);

        Leaf l2 = new Leaf();
        addObject(l2, 0, 2);

        Leaf l3 = new Leaf();
        addObject(l3, 7, 5);

        Leaf l4 = new Leaf();
        addObject(l4, 2, 6);

        Leaf l5 = new Leaf();
        addObject(l5, 5, 0);
        
        Leaf l6 = new Leaf();
        addObject(l6, 4, 7);
    }
    
    /**
     * Place a number of leaves into the world at random places.
     * The number of leaves can be specified.
     */
    public void randomLeaves(int howMany)
    {
        for(int i=0; i<howMany; i++) {
            Leaf leaf = new Leaf();
            int x = Greenfoot.getRandomNumber(getWidth());
            int y = Greenfoot.getRandomNumber(getHeight());
            addObject(leaf, x, y);
        }
    }
}
Thanks!
danpost danpost

2021/3/18

#
Better might be a Wombat class variable:
public static Wombat selected = null;
checking for clicks in act method:
if (Greenfoot.mouseClicked(this)) selected = this;
Then, for special treatment of selected wombat, you would start with:
if (this == selected)
or from another class:
Wombat selectedWombat = Wombat.selected;
if (selectedWombat != null)
Mutex Mutex

2021/3/18

#
And how can i use like if { (selectedWombat && Greenfoot.isKeyDown("left")) { turn left; } to turn left via left arrow? But i need to turn left just the selected wombat.
danpost danpost

2021/3/18

#
Mutex wrote...
And how can i use like if { (selectedWombat && Greenfoot.isKeyDown("left")) { turn left; } to turn left via left arrow? But i need to turn left just the selected wombat.
You wouldn't need to. In Wombat act method (starting as shown above):
if (this == selected)
{
    if (Greenfoot.isKeyDown("left")) turnLeft();
}
Mutex Mutex

2021/3/18

#
Hmm i can see it doesnt work. Should i edited another?
danpost danpost

2021/3/18

#
Mutex wrote...
Hmm i can see it doesnt work. Should i edited another?
Show Wombat class codes.
Mutex Mutex

2021/3/18

#
import greenfoot.*;  // (World, Actor, GreenfootImage, and Greenfoot)

import java.util.List;
import java.util.ArrayList;

/**
 * Wombat. A Wombat moves forward until it can't do so anymore, at
 * which point it turns left. If a wombat finds a leaf, it eats it.
 * 
 * @author Michael Kolling
 * @version 1.0.1
 */
public class Wombat extends Actor
{
    private static final int EAST = 0;
    private static final int WEST = 1;
    private static final int NORTH = 2;
    private static final int SOUTH = 3;
    public static Wombat selected = null;
    private int direction;
    private int leavesEaten;

    public Wombat()
    {
        setDirection(EAST);
        leavesEaten = 0;
    }

    /**
     * Do whatever the wombat likes to to just now.
     */
    public void act()
    {
        move(4);
	if (this == selected)
{
    if (Greenfoot.isKeyDown("left")) turnLeft();
}
    }

    /**
     * Check whether there is a leaf in the same cell as we are.
     */
    public boolean foundLeaf()
    {
        Actor leaf = getOneObjectAtOffset(0, 0, Leaf.class);
        if(leaf != null) {
            return true;
        }
        else {
            return false;
        }
    }
    
    /**
     * Eat a leaf.
     */
    public void eatLeaf()
    {
        Actor leaf = getOneObjectAtOffset(0, 0, Leaf.class);
        if(leaf != null) {
            // eat the leaf...
            getWorld().removeObject(leaf);
            leavesEaten = leavesEaten + 1; 
        }
    }
    
    /**
     * Move one cell forward in the current direction.
     */
    public void move()
    {
        if (!canMove()) {
            return;
        }
        switch(direction) {
            case SOUTH :
                setLocation(getX(), getY() + 1);
                break;
            case EAST :
                setLocation(getX() + 1, getY());
                break;
            case NORTH :
                setLocation(getX(), getY() - 1);
                break;
            case WEST :
                setLocation(getX() - 1, getY());
                break;
        }
    }

    /**
     * Test if we can move forward. Return true if we can, false otherwise.
     */
    public boolean canMove()
    {
        World myWorld = getWorld();
        int x = getX();
        int y = getY();
        switch(direction) {
            case SOUTH :
                y++;
                break;
            case EAST :
                x++;
                break;
            case NORTH :
                y--;
                break;
            case WEST :
                x--;
                break;
        }
        // test for outside border
        if (x >= myWorld.getWidth() || y >= myWorld.getHeight()) {
            return false;
        }
        else if (x < 0 || y < 0) {
            return false;
        }
        return true;
    }

    /**
     * Turns towards the left.
     */
    public void turnLeft()
    {
        switch(direction) {
            case SOUTH :
                setDirection(EAST);
                break;
            case EAST :
                setDirection(NORTH);
                break;
            case NORTH :
                setDirection(WEST);
                break;
            case WEST :
                setDirection(SOUTH);
                break;
        }
    }

    /**
     * Sets the direction we're facing.
     */
    public void setDirection(int direction)
    {
        this.direction = direction;
        switch(direction) {
            case SOUTH :
                setRotation(90);
                break;
            case EAST :
                setRotation(0);
                break;
            case NORTH :
                setRotation(270);
                break;
            case WEST :
                setRotation(180);
                break;
            default :
                break;
        }
    }

    /**
     * Tell how many leaves we have eaten.
     */
    public int getLeavesEaten()
    {
        return leavesEaten;
    }
}
danpost danpost

2021/3/18

#
danpost wrote...
checking for clicks in act method:
if (Greenfoot.mouseClicked(this)) selected = this;
You are missing this.
Mutex Mutex

2021/3/18

#
Thanks
You need to login to post a reply.