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

#
Worked great here's an arrow class Green arrow:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class Greenarrow here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Greenarrow extends Actor
{
    /**
     * Act - do whatever the Greenarrow 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.
    }   
}
arrow1.png: green arrow2.png: yellow arrow3.png: red arrow4.png: purple arrow5.png: blue
danpost danpost

2014/5/10

#
Ok. Remove all your arrow classes and then 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 Arrow extends Actor
{
    private int color;
 
    public Arrow()
    {
        color = Greenfoot.getRandomNumber(5)+1;
        setImage(new GreenfootImage("arrow"+color+".png");
    }
 
    public int getColor()
    {
        return color;
    }
}
This is just the beginning of this class. We will be adding movement, eating and color changing methods.
anonymousse anonymousse

2014/5/10

#
Ok I added the class. no errors
danpost danpost

2014/5/10

#
To aid in properly adding the Arrow object into the world, add the following method to the Arrow class:
1
2
3
4
5
6
7
8
9
public void addedToWorld(World world)
{
    while (((Tile)getOneObjectAtOffset(0, 0, Tile.class)).getColor() != 0)
    {
        int x = 12+25*(Greenfoot.getRandomNumber(world.getWidth()/25);
        int y = 12+25*(Greenfoot.getRandomNumber(world.getHeight()/25);
        setLocation(x, y);
    }
}
This will land the arrow on a blank tile; but the world will still have to randomly add it into the world.
anonymousse anonymousse

2014/5/10

#
Ok done
danpost danpost

2014/5/10

#
You will also need this method in the Arrow class:
1
2
3
4
5
private void mutate()
{
    color = Greenfoot.getRandomNumber(5)+1;
    setImage("arrow"+color+".png");
}
That reminds me, you will need a somewhat similar method in the Tile class:
1
2
3
4
5
public void getEaten()
{
    color = 0;
    setImage("egg0.png");
}
BTW, what are the movement rules for the arrow? if there are any restrictions within the 24x24 grid, what are they?
anonymousse anonymousse

2014/5/10

#
For movement, there are little borders of white between tiles. I would like for the arrow to not be between tiles but only on top of tiles which are then eaten.
anonymousse anonymousse

2014/5/10

#
Also how can I connect the getEaten and mutate commands?
danpost danpost

2014/5/10

#
anonymousse wrote...
Also how can I connect the getEaten and mutate commands?
When the arrow eats (in the Arrow class), you will use the following together:
1
2
((Tile)getOneObjectAtOffset(0, 0, Tile.class)).getEaten(); // eat tile
mutate(); // select new random color
Line 1 may be written differently as you may already at that point in the method have a reference to the tile object that is to be eaten.
danpost danpost

2014/5/10

#
Are you going to have any other actors in your world besides tiles and arrows? if so, what? Also, is your Arrow object to 'jump' from tile to tile when moved?
anonymousse anonymousse

2014/5/10

#
No only the tiles and the arrows are actors. Yes, it would basically jump to the next tile. I added the codes but how can I get the arrow to be placed in the world in connection with addedtoworld function? I tried adding just the arrow class as a prepare function with a location on the world but I guess that won't work.
danpost danpost

2014/5/10

#
anonymousse wrote...
No only the tiles and the arrows are actors. Yes, it would basically jump to the next tile. I added the codes but how can I get the arrow to be placed in the world in connection with addedtoworld function?
Given what you just wrote, this can be made a whole lot easier. (1) Change your 'super(600, 600, 1);' call in the world constructor to 'super(24, 24, 25);' (2) Change lines 5 and 6 of the 'addedToWorld' method above to
1
2
int x = Greenfoot.getRandomNumber(world.getWidth());
int y = Greenfoot.getRandomNumber(world.getHeight());
(3) Change the double 'for' loop spawning code in your world class to this:
1
2
3
4
5
6
for (int y=0; y<24; y++) for (int x=0; x<24; x++)
{
    int num = Greenfoot.getRandomNumber(28);
    if (num > 5) num = 0;
    addObject(new Tile(num), x, y);
}
Having the world take care of the gridwork will make thing less complicated for you. Now, the two lines in (2) above can be used in the world class (minus the 'world.' prefix for getting the width and height of the world) to choose the first possible random location for the arrow; just 'addObject(new Arrow(), x, y);' afterwards (this would be done in the world constructor or a method it calls).
anonymousse anonymousse

2014/5/10

#
I have a few questions: Here is what I have of the world called Blank right now:
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
public class Blank extends World
{
 
    /**
     * Constructor for objects of class Seven.
     *
     */
    public Blank()
    {   
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(200, 200, 5);
        for (int y=12; y<200; y+=25) for (int x=12; x<200; x+=25
    int num = Greenfoot.getRandomNumber(20); 
    if (num > 5) num = 0
    addObject(new Tile(num), x, y); 
 
int x = Greenfoot.getRandomNumber(getWidth()); 
int y = Greenfoot.getRandomNumber(getHeight());
addObject(new Arrow(), x, y);
    }
     
     
}
I changed the codes in (2) without any issues. For (1), if I change them to those dimensions my world and my tiles are messed up so I was just wondering if I would be able to keep the original values with the new setup. Also, for part 3, similarly I run into problems if I change both the x+=25 and y+=25 into x++ and y++. The world will no longer show up. Also, I added the last part for the arrow but I run into this error in the greenfoot terminal window: java.lang.IllegalStateException: Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed. at greenfoot.Actor.failIfNotInWorld(Actor.java:663) at greenfoot.Actor.getOneObjectAtOffset(Actor.java:867) at Arrow.<init>(Arrow.java:45) at Blank.<init>(Blank.java:29) Here is my code for Arrow:
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
public class Arrow extends Actor
{
    /**
     * 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"); 
 
 
{
((Tile)getOneObjectAtOffset(0, 0, Tile.class)).getEaten(); // eat tile 
mutate(); // select new random color
}
I am wondering if I messed up something on the Arrow class that's giving me this error
danpost danpost

2014/5/10

#
The error is due to lines 36 through 39 in the Arrow class, which should not be in your code yet (I said that we will use it, but I did not mean for you to add it yet. You may have to leave line 39 in the code to balance your brackets. I did not realize that your world was already gridded in cells of dimension 5. I will have to look over what we have and see if I can come up with something that will work.
anonymousse anonymousse

2014/5/10

#
That worked thank you. If I would change the dimensions to 'super(24, 24, 25), would that mean that my cells are in a dimension of 25 then?
There are more replies on the next page.
1
2
3
4