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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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:
1
public static Wombat selected = null;
checking for clicks in act method:
1
if (Greenfoot.mouseClicked(this)) selected = this;
Then, for special treatment of selected wombat, you would start with:
1
if (this == selected)
or from another class:
1
2
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):
1
2
3
4
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

#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
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:
1
if (Greenfoot.mouseClicked(this)) selected = this;
You are missing this.
Mutex Mutex

2021/3/18

#
Thanks
You need to login to post a reply.