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

2014/5/7

Get an actor to change color

1
2
3
4
anonymousse anonymousse

2014/5/11

#
That worked great. Now if I add a public void eat, do I add a getColor and return color and then add the eat function?
danpost danpost

2014/5/11

#
anonymousse wrote...
That worked great. Now if I add a public void eat, do I add a getColor and return color and then add the eat function?
The Arrow class will not need a 'getColor' method as it is what is moving and it is it that will be checking for the color of the Tile; and the Tile class has a 'getColor' method for that. Ok, now, we have established that the arrow can move between tiles. Now answer this one: can the arrow also move onto any tile regardless of color, or only onto tiles of the color it currently shows?
danpost danpost

2014/5/11

#
By the way, in my version (I programmatically created images for my actors), I reduced the size of the world to (33, 33, 20) and the images of the tiles to 60x60 and the image of the arrow to 18x18 (just under 20x20). That way the arrow will be able to move around the tiles along the edges and in between without touching them. I placed the tiles using this:
1
for (int y=2; y<33; y+=4) for (int x=2; x<33; x+=4)
To try it out, you can scale your actor images in their constructors using 'getImage().scale(n, n);'.
anonymousse anonymousse

2014/5/11

#
If an arrow is red, it can move onto only red tiles and blank tiles. Same thing with green, blue, etc. Once the tile is eaten, it would be replaced by a blank tile. I tried the following code:
1
2
3
4
5
6
7
8
9
10
11
public void eat()
    {
        Actor tile;
        tile = getOneObjectAtOffset(0, 0, Tile.class);
        if (tile !=null)
        {
            World world;
            world = getWorld();
            world.removeObject(tile);
        }
    }
But the arrow still eats everything in its path. Would I restrict the movement instead? Also, whenever they are eaten, I notice they only get replaced by a white image with nothing on it. I have this code in the tile class:
1
2
3
4
5
  public void getEaten() 
    color = 0
    setImage("egg0.png"); 
}
And thought that would mean once it is eaten, the tile would just be replaced by the blank tile image. Just saw your post, trying it out
danpost danpost

2014/5/11

#
Your 'eat' method does not check for the color of the tile (yet). Also, you do not want to remove the tile; just change its image/color to blank. Actually, there is no need for an eat method. Since you need to control better where the arrow can go, you will already be finding out if a tile is present. You can either retract the move if the tile is not the correct color or eat the tile if it is, right there in the 'move' method.
anonymousse anonymousse

2014/5/11

#
How can I set limits for movement in the move function? Is there an if statement for that or a getImage?
danpost danpost

2014/5/11

#
Add the following to the end of the 'move' method:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Tile tile = (Tile)getOneObjectAtOffset(0, 0, Tile.class);
if (tile == null || tile.getColor() == 0)
{
    return;
}
if (tile.getColor() != color)
{
    setLocation(getX()-dx, getY()-dy);
}
else
{
    tile.getEaten();
    mutate();
}
anonymousse anonymousse

2014/5/11

#
That worked great. How can we put null and 0 on the same booleon though and still get the arrow to move onto the blank colored tiles? I thought return meant the action would not be performed and is null referring to if the colors do not match and 0 to the blank tile?
danpost danpost

2014/5/11

#
anonymousse wrote...
That worked great. How can we put null and 0 on the same booleon though and still get the arrow to move onto the blank colored tiles? I thought return meant the action would not be performed and is null referring to if the colors do not match and 0 to the blank tile?
Line 2 checks for two conditions: first, 'tile == null' checks if no tile is present and 'tile.getColor() == 0' checks to see if the tile present is a blank tile. These are two condition which if either is true, then the move is legal and no more action needs to be taken (remember, the move has already been made). The next if condition, 'tile.getColor() != color' checks to see if the color of tile does not match the current color of the arrow. If not, the move is taken back (or no move is made -- visually). 'else', of course, means it was the same color and the tile would then be eaten and the arrow would choose a new random color (sometimes it ends up to be the same color).
anonymousse anonymousse

2014/5/13

#
I'm trying to get tiles to show up every few seconds so I added a counter actor and followed a tutorial on timing events. However I keep getting illegal start of expression. It's probably a really simple problem but I'm not understanding what I'm doing wrong. I added it to the arrow class.
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
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class Arrow here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Arrow extends Actor
 
{
 
private Counter counter;
private int tileDelayCount;
 
public Arrow(Counter pointCounter)
{
    counter = pointCounter;
    tileDelayCount = 0;
}
 
 
 
    /**
     * Act - do whatever the Arrow wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
      private int color; 
   
    public Arrow() 
    
        color = Greenfoot.getRandomNumber(5)+1
        setImage(new GreenfootImage("arrow"+color+".png")); 
    
     
   
  
   
    public int getColor() 
    
        return color; 
    
     
    public void addedToWorld(World world) 
    
    while (((Tile)getOneObjectAtOffset(0, 0, Tile.class)).getColor() != 0
    
        int x = Greenfoot.getRandomNumber(world.getWidth());
        int y = Greenfoot.getRandomNumber(world.getHeight());
        setLocation(x, y); 
    
private void mutate() 
    color = Greenfoot.getRandomNumber(5)+1
    setImage("arrow"+color+".png"); 
 
 public void act() {
         
        move();
        checkNewTile();
        newTile();
    }
 
     
public void move() 
 
    String key = Greenfoot.getKey();
    int dx = 0, dy = 0; // the offsets from current location 
    if ("up".equals(key)) dy--; 
    if ("down".equals(key)) dy++; 
    if ("left".equals(key)) dx--; 
    if ("right".equals(key)) dx++; 
    setLocation(getX()+dx, getY()+dy); 
     
   Tile tile = (Tile)getOneObjectAtOffset(0, 0, Tile.class); 
if (tile == null || tile.getColor() == 0
    return
if (tile.getColor() != color) 
    setLocation(getX()-dx, getY()-dy); 
else 
    tile.getEaten(); 
    mutate(); 
    newTile();
     
 
private void newTile()
{
    tileDelayCount = 100;// check for illegal movement here and reset location adding negative offsets 
 
private void checkNewTile()
{
    if (tileDelayCount > 0) {
        tileDelayCount--;
        if (tileDelayCount == 0) {
            createNewTile();
        }
    }
}
 
private void createNewTile()
{
    int num = Greenfoot.getRandomNumber(15); 
    if (num > 5) num = 0
    addObject(new Tile(num), x, y); 
     
}
}
danpost danpost

2014/5/13

#
If you recall, tiles are not being removed when eaten; they just turn to a blank tile. So, adding tiles would be the wrong thing to do here. You could have the blank tiles count down a timer that is given a value within a specific range and, when its timer reaches zero, change to a random color.
anonymousse anonymousse

2014/5/13

#
Do I add a counter to the tile function? Since I don't have a blank tile class I'm sort of confused how to do that. I know I could refer it back to the mutate function but I'm confused on how I can call up the blank tile
anonymousse anonymousse

2014/5/13

#
Ok is there a way to add a double if statement for a single action? For instance, I have
1
2
3
4
5
6
if (tileDelayCount > 0) { 
      tileDelayCount--; 
      if (tileDelayCount == 0)  { 
         mutate(); 
      
  }
And I want to add to the tileDelayCount == 0 a function for (tile.getColor() == 0) )
danpost danpost

2014/5/13

#
Please refer to the middle section, The condtional operators, of this page of the Java tutorials.
You need to login to post a reply.
1
2
3
4