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/7

#
So I'm new to this and was wondering if someone could help me figure out how to change my actor around? I'm going to try to explain my game as best as I can. I would like to create a tile based game with various tiles and an arrow tile that would eat the other tiles. I would like for the blue arrow to only be able to eat blue tiles and for a red tile to only be able to eat red tiles, etc. I would also like for the tile color to change to another random color when one tile is eaten. I only have a basic code for a random tile map and various arrow actors that can eat all the tiles right now. Can anyone explain to me how I would go about making the arrow change to a different color whenever it eats a a tile and how to limit the arrow to only eating it's color tile?
danpost danpost

2014/5/7

#
The tiles will need an instance field to hold a value that is unique to the color that it currently is. You will need a 'get' method to return that value so you can compare that of the food with that of the hungry. You can add a public method for the tiles to randomly choose a new color and set the appropriate image.
anonymousse anonymousse

2014/5/8

#
Sorry for asking so many questions. Would I just add public int blue, green, red etc to the map class through which all tiles come from or in each individual tile class itself? Thanks for your help!
danpost danpost

2014/5/8

#
Well ... what are all the classes that extend the Map class?
anonymousse anonymousse

2014/5/8

#
I have a blank, blue, red, purple, yellow, and green subclass from the Map class.
danpost danpost

2014/5/8

#
Ok. A few more questions and I think we can work on setting things proper. (1) of the various arrows, is only one to be in the world at a time; (confirm) (2) if (1) is so, then is each arrow just a matching color to the various tile colors; (assumed) (3) nvm this one, answered above; (4) do you have images in your 'images' folder of the project for all the different tiles and arrows Once all of these questions are answered completely, we can begin. Number four is the main question that needs to be answered. Also, could you please post the code for the Map class and one of the subclasses of it. Use the 'code' tag below the 'Post a reply' box for inserting (using copy/paste) your code into the post.
anonymousse anonymousse

2014/5/8

#
1. Yes 2. Yes 4. Yes I took a scenario on here with randomly generated tiles and I'm having trouble understanding the last part with the tilescomp commands. The original map subclasses didn't have any particular code in them so I haven't modified them yet, I've only been able to make a random generated map and a start button in the beginning.
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
public class Map extends Actor
{
    /**
     * Act - do whatever the Map wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        int online = 0;
        int tilescomp = 0;
        boolean done = false;
        int num;
        int x = 12;
        int y = 12;
        while (!done) {
            num = Greenfoot.getRandomNumber(28);
            if (num==0) {
                Red red = new Red();
                getWorld().addObject(red, x, y);
            }
            if (num==1) {
                Purple purple = new Purple();
                getWorld().addObject(purple, x, y);
            }
            if (num==2) {
                Yellow yellow = new Yellow();
                getWorld().addObject(yellow, x, y);
            }
            if (num==3) {
                Green green = new Green();
                getWorld().addObject(green, x, y);
            }
            if (num==4) {
                Blue blue = new Blue();
                getWorld().addObject(blue, x, y);
            }
            if (num>4) {
                Blank1 blank1 = new Blank1();
                getWorld().addObject(blank1, x, y);
            }
       
             
            if (tilescomp==575) {
                done = true;
                getWorld().removeObject(this);
            }
            else {
                if (online==23) {
                    online = 0;
                    x = 12;
                    y = y + 25;
                    tilescomp = tilescomp + 1;
                }
                else {
                    online = online + 1;
                    x = x + 25;
                    tilescomp = tilescomp + 1;
                }
            }
        }
    }   
}
danpost danpost

2014/5/9

#
It appears that the 'tilescomp' field is to count the iterations through the 'while' loop. The 'while' loop is iterating through each possible location for a color object and by random chance, deciding if and which to place there. . The world appears to be 600x600 (24x24 in tiles) in size and there is a 3:14 chance of a tile being placed at any location and a 1:6 chance for any color tile when one is placed. A double 'for' loop would make the coding easier. The following is a Map class that performs exactly the same way (absolutely no difference in what it does). If you created a Random generator object and used the same seed on both classes, the resulting maps should be identical.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import greenfoot.*;
 
public class Map extends Actor
{
    public void act()
    {
        for (int y=12; y<600; y+=25) for (int x=12; x<600; x+=25)
        {
            int num = Greenfoot.getRandomNumber(28);
            Actor actor = null;
            if (num == 0) actor = new Red();
            if (num == 1) actor = new Purple();
            if (num == 2) actor = new Yellow();
            if (num == 3) actor = new Green();
            if (num == 4) actor = new Blue();
            if (num == 5) actor = new Blank1();
            if (actor != null) getWorld().addObject(actor, x, y);
        }
        getWorld().removeObject(this);
    }   
}
With this code, you can more readily see what it does; and you may then realize this: why should a Map object be created to build a map when instead of creating and adding a map into the world to do it, you can just do it. That is
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// instead of (I would presume in the world class)
addObject(new Map(), 0, 0);
// you can just use
for (int y=12; y<600; y+=25) for (int x=12; x<600; x+=25)
{
    int num = Greenfoot.getRandomNumber(28);
    Actor actor = null;
    if (num == 0) actor = new Red();
    if (num == 1) actor = new Purple();
    if (num == 2) actor = new Yellow();
    if (num == 3) actor = new Green();
    if (num == 4) actor = new Blue();
    if (num == 5) actor = new Blank1();
    if (actor != null) addObject(actor, x, y);
}
Or, just create a method with that code (in the world class) and call it instead of creating and adding the Map object just to execute that code.
anonymousse anonymousse

2014/5/9

#
Thank you so much! The tiles are so much simpler now without the map. Now I have all the buttons and am wondering how I can make sure the button can land only on a blank tile in the beginning? Will I make a specific class for the tile and then subclasses that are connected to each color tile?
danpost danpost

2014/5/9

#
Ok, I see I missed something in my code. In the last 15-line code post above, change 'num == 5' in line 13 to 'num > 4'. Next,
anonymousse wrote...
Now I have all the buttons and am wondering how I can make sure the button can land only on a blank tile in the beginning? Will I make a specific class for the tile and then subclasses that are connected to each color tile?
Actually, no. In fact, we are probably going to remove a bunch of stuff before we continue. First, I would like to see the code to a colored tile class, maybe 'Red', and also to the 'Blank1' class. Also, what are the names of the image files for each of the six colored tiles.
anonymousse anonymousse

2014/5/9

#
Already changed thanks. I changed Blank1 to Blank and changed the World name. I haven't changed the codes in each of the classes yet. I am confused about how to add an instance field to them.
anonymousse anonymousse

2014/5/9

#
The names are Egg1.png, egg2.png, egg3.png, egg4.png, egg5.png and also blank.png
danpost danpost

2014/5/9

#
Is it 'Egg1.png' or 'egg1.png' (in lowercase, like the others)? and with that order, what are their colors? I still need to see the classes I asked for (whether changed or not).
anonymousse anonymousse

2014/5/10

#
They're all in lowercase. egg1.png: green egg2.png: yellow egg3.png: red egg4.png: purple egg5.png: blue blank.png: blank Red tile:
1
2
3
4
5
6
7
8
9
10
11
public class Red extends Actor
{
    /**
     * Act - do whatever the Red wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        // Add your action code here.
    }   
}
Blank tile:
1
2
3
4
5
6
7
8
9
10
11
public class Blank extends Actor
{
    /**
     * Act - do whatever the Blank wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        // Add your action code here.
    }   
}
danpost danpost

2014/5/10

#
Ok. First, to make things easy, change the name of the 'blank.png' file to 'egg0.png'. Then, change my tile spawning code above to this:
1
2
3
4
5
6
for (int y=12; y<600; y+=25) for (int x=12; x<600; x+=25)
{
    int num = Greenfoot.getRandomNumber(28);
    if (num > 5) num = 0;
    addObject(new Tile(num), x, y);
}
Now, remove all the classes, Red Green Blue Purple Yellow and Blank and afterwards add the following class:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import greenfoot.*;
 
public class Tile extends Actor
{
    private int color;
 
    public Tile(int colorValue)
    {
        color = colorValue;
        setImage(new GreenfootImage("egg"+color+".png");
    }
 
    public int getColor()
    {
        return color;
    }
}
When this is done and working, post an arrow class, give names of files with colors as before and we will fix it right up.
There are more replies on the next page.
1
2
3
4