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

2014/5/13

Tile Changing Color

1
2
3
4
anonymousse anonymousse

2014/5/13

#
I had a previous thread but felt like it was getting too long. I'm trying to program a game where an arrow tile can eat other egg tiles that are the same color as the arrow but cannot go through egg tiles that are not its color. Thanks to a lot of help from a member, I was able to make the basic scenario. However, I would like new colored tiles to pop up (There are also blank tiles around the board) after a certain amount of time like ten seconds. Instead of new tiles popping up, since they are removed and converted to blank tiles when they are removed, I am trying to get a blank tile to turn into a colored tile after a certain time. I implemented this code, and no errors, but it won't work for some reason. I also forgot to mention my files are based on color, so there's egg1, egg2, etc. That's why this may look a certain way.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Tile here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Tile extends Actor
{
    
    private Counter counter;  
private int tileDelayCount;  
  
public Tile(Counter pointCounter)  
{  
    counter = pointCounter;  
    tileDelayCount = 0;  
}  
    /**
     * Act - do whatever the Tile wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    
 
     private int color;  
  
    public Tile(int colorValue)  
    {  
        color = colorValue;  
        setImage(new GreenfootImage("egg"+color+".png"));
    }  
  
    public int getColor()  
    {  
        return color;  
    }   
    
    public void getEaten()  
{  
    color = 0;  
    setImage("egg0.png");  
}

private void newTile()  
{  
    tileDelayCount = 10;
}    
  
private void checkNewTile()  
{  
       Tile tile = (Tile)getOneObjectAtOffset(0, 0, Tile.class);  
    if (tileDelayCount > 0) {  
        tileDelayCount--;  
        if (tileDelayCount == 0 && tile.getColor() == 0); {  
           changecolor();  
        }  
    }  
}  
  


private void changecolor()  
{  
    Tile tile = (Tile)getOneObjectAtOffset(0, 0, Tile.class);
    color = Greenfoot.getRandomNumber(5)+1;  
    setImage("egg"+color+".png");  
}  
}
danpost danpost

2014/5/13

#
You need to add an 'act' method to run the counter if the tile is blank:
public void act()
{
    if (color != 0) return; // tile is not blank
    if (counter == 0) counter = 500+Greenfoot.getRandomNumber(500); // set timer
    counter--; // run timer
    if (counter == 0) changecolor(); // time elapsed; set new color
}
anonymousse anonymousse

2014/5/13

#
Thank you. Is there a way I can limit the appearance of a colored tile to just one every 500 frames? All of them change one after the other in unison pretty much.
danpost danpost

2014/5/13

#
anonymousse wrote...
Thank you. Is there a way I can limit the appearance of a colored tile to just one every 500 frames? All of them change one after the other in unison pretty much.
To do that, you will have to code it in your subclass of World. Remove the counter and the act method from the Tile class. Also, remove the timeDelayCounter ... wait ... there is so much junk in the class ... ok, from the above code, remove lines 11 through 25 and remove lines 44 through 62; also, remove line 65 and change line 63 to public instead of private. Now, in your world subclass, add the counter and the following method which needs to be called from the act method of that class:
private void runTileChangeCounter()
{
    counter++;
    if (counter == 500)
    {
        java.util.List<Tile> tiles = getObjects(Tile.class);
        boolean foundBlank = false;
        for (int n=0; n<tiles.size(); n++) if (foundBlank = (tile.getColor() == 0) break;
        if (!foundBlank) return;
        Tile tile = null;
        while (tile == null || tile.getColor() != 0) tile = tiles.get(Greenfoot.getRandomNumber(tiles.size()));
        tile.changecolor();
        counter = 0;
    }
}
I had to throw lines 7 through 9 in there to cover the possibility that there could be no blanks; otherwise the 'while' loop would run indefinitely.
anonymousse anonymousse

2014/5/14

#
Ok I keep running into two errors.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Seven here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
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(7, 7, 125);
        for (int y=0; y<7; y++) for (int x=0; x<7; x++)  
{  
    int num = Greenfoot.getRandomNumber(15);  
    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);  
    }  
    
    public void act()  
{  
    if (color != 0) return; // tile is not blank  
    if (tileDelayCount == 0) tileDelayCount = 500+Greenfoot.getRandomNumber(500); // set timer  
    tileDelayCount--; // run timer  
    if (tileDelayCount == 0) changecolor(); // time elapsed; set new color  
}  

    
  private Counter counter;    
private int tileDelayCount;       
private int color;  



 

      
private void runTileChangeCounter()  
{  
    tileDelayCount++;  
    if (tileDelayCount == 500)  
    {    
        
        java.util.List<Tile> tiles = getObjects(Tile.class);  
        boolean foundBlank = false;  
        for (int n=0; n<tiles.size(); n++) if ((foundBlank = (tile.getColor() == 0))); break;  
        if (!foundBlank) return;  
        Tile tile = null;  
        while (tile == null || tile.getColor() != 0) tile = tiles.get(Greenfoot.getRandomNumber(tiles.size()));  
        tile.changecolor();  
        counter = 0;  
    }  
}  
    

 


}
    
    
I changed private into public for changecolor but I'm still running into issues for not being able to find the method changecolor. Also, it can't find the variable tile. I tried adding:
 public class Tile extends Actor  
{  
      
    private Counter counter;    
private int tileDelayCount;    
    
public Tile(Counter pointCounter)    
{    
    counter = pointCounter;    
    tileDelayCount = 0;    
}    
to declare the variable but then I have other problems with my code not working and the addObject(new Tile(num), x, y); no longer works with the num function.
anonymousse anonymousse

2014/5/14

#
Fixed the changecolor issue, now I just am having trouble declaring the tile variable. Edit: I fixed the tile variable too, didn't see that it was tiles. But now I'm getting this error: cannot find symbol method - getColor in the last few lines
danpost danpost

2014/5/15

#
My bad. Change 'tile.getColor()' on line 63 to 'tiles.get(n).getColor()'.
anonymousse anonymousse

2014/5/15

#
Thanks! The tiles.getColor() on line 66 gave me the same error again and I tried using 'tiles.get(n).getColor()' but then it can't read the variable n.
danpost danpost

2014/5/15

#
You should not have gotten the error on 66 and you certainly should not change 'tile.getColor()' on that line as you did on 63. Your tile class has a 'getColor' method and we are making sure that the field is not 'null' before calling 'getColor' on it. So, I cannot say why you are getting that error there. Unless the list is not giving the elements as Tile object (although it is told to hold only Tile objects). Try adding '(Tile)' after the equal sign on that line. If that works, you may have to change the following line to '((Tile)tile).changecolor();'.
anonymousse anonymousse

2014/5/15

#
Ok so I fixed those issues by changing the field to ((Tile)tile).getColor(). Now I only have one issue:
tile = tile.get(Greenfoot.getRandomNumber(Tile.color()));       
I changed size to color because I thought there was no size defined but eitherway I get the error, cannot find symbol method. I tried taking that out and the code compiled but the new tiles wouldn't show up. I thought the getColor and changeColor methods would be enough and thought the code would run without the above. Here is what I have together:
 private Counter counter;    
private int tileDelayCount;       
private int color;  

      
private void runTileChangeCounter()  
{  

    tileDelayCount++;  
    if (tileDelayCount == 50)  
    {   

        java.util.List<Tile> tile = getObjects(Tile.class);  
        boolean foundBlank = false;  
        for (int n=0; n<tile.size(); n++) if ((foundBlank = (tile.get(n).getColor() == 0))); 
        if (!foundBlank) return;    
        while (tile == null|| ((Tile)tile).getColor() != 0) tile = tile.get(Greenfoot.getRandomNumber(Tile.size()));       
        ((Tile)tile).changecolor();   
        tileDelayCount = 0;    
    }    
}    
danpost danpost

2014/5/15

#
I had 'tiles.size()' there. 'tiles' is a List object and the List class has a 'size' method that returns the number of elements within the list. That is the range that we need to use for 'getRandomNumber'. The problem is you are changing things unnecessarily. You changed 'tiles' in line 13 of your last post to 'tile' (without the 's' at the end). Please ask before changing something that I have posted (unless it is an obvious mistake and you know how to fix it).
danpost danpost

2014/5/15

#
I thought I would re-post what you should have considering all the modifications you and I have made. Try this:
private int tileDelayCount; 
      
private void runTileChangeCounter()  
{  

    tileDelayCount++;  
    if (tileDelayCount == 50)  
    {   

        java.util.List<Tile> tiles = getObjects(Tile.class);  
        boolean foundBlank = false;  
        for (int n=0; n<tiles.size(); n++) if ((foundBlank = (tiles.get(n).getColor() == 0))) break; 
        if (!foundBlank) return;
        Tile tile = null;    
        while (tile == null|| tile.getColor() != 0) tile = (Tile)tiles.get(Greenfoot.getRandomNumber(tiles.size()));       
        tile.changecolor();   
        tileDelayCount = 0;    
    }    
}
danpost danpost

2014/5/15

#
Do you even half understand how this method works? Provided the method is called every act cycle (the act method has an unconditional call to 'runTileChangeCounter' in it): Line 6 increments the counter. Line 7 puts a condition on the execution of the rest of the code in the method that 50 act cycles (about one second) have been counted. Line 10 creates a List object called 'tiles' containing all Tile objects in the world. Line 11 declares a local boolean to indicate whether a blank tile has been located or not. There are several parts to line 12. It can be broke down into these steps:
for (int n=0; n<tiles.size(); n++)
{
    if (tiles.get(n).getColor()  == 0) foundBlank = true;
    if (foundBlank) break;
}
The 'for' loop iterates through the list of Tile objects in the world. The first 'if' statement trips the 'foundBlank' flag if the current tile is blank. The second 'if' statement exits the 'for' loop if a blank tile was found. Line 13 exits the method if no blank tiles were found. Line 14 declares a local variable to hold a Tile object. Line 15 also has several parts and can be broke down into this
while (tile == null || tile.getColor() != 0)
{
    int rand = Greenfoot.getRandomNumber(tiles.size());
    tile = (Tile) tiles.get(rand);
}
There are two conditions that must be met to execute the code within the 'while' block. The 'tile' variable cannot be 'null' (it must contain a Tile object) or that Tile object must NOT have a color value of zero (it cannot be a blank tile). If either of these conditions are true then we cannot use that Tile object and must randomly choose another one, which the enclosing code does. Now we have a blank tile held in the 'tile' variable that we can change the color on line 16. Line 17 resets the counter to wait another second before changing another blank tile to a colored tile.
anonymousse anonymousse

2014/5/15

#
My apologies. I added the code and it compiles fine without any tissues. I am confused about the act method though. I realized I forgot to add it in my code.
public void act()
{
    if (color != 0) return; // tile is not blank
    if (tileDelayCount == 0) tileDelayCount = 500+Greenfoot.getRandomNumber(500); // set timer
    tileDelayCount--; // run timer
    if (tileDelayCount == 0) changecolor(); // time elapsed; set new color
}

    
I added it to the tile class and it worked wonderfully but one tile just starts popping up right after the other. Is the act method supposed to be in the World class or the tile class? If I put it in the world class it can't find the changecolor method. And is there a way to edit it so that only one tile changes color at a time?
molsbex molsbex

2014/5/15

#
hi danpost, we're a bit stuck and want to make a game in which a fairy shoots mushrooms at spiders and the spiders disappear when shot. We have already made the fairy able to turn and shoot mushrooms but how do we make the spiders move around and then disappear when shot?
There are more replies on the next page.
1
2
3
4