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

2014/5/17

#
Ok. I got it.
danpost danpost

2014/5/17

#
One major problem I found was in your Blank world class, in the 'runTileChangeCounter' method, the 'if' condition needs to be changed to '>=' instead of '=='.
anonymousse anonymousse

2014/5/17

#
Ok I changed that. But why? I thought we were running it at 50 until it hit 0? And are there any problems with the gameOver function running?
danpost danpost

2014/5/17

#
If we 'return' in the middle of the method, then the counter is not reset to zero and the next running of it will increase it to 51 and will not be caught by the 'if' condition unless you change it so.
danpost danpost

2014/5/17

#
The best times to have the arrow check for a trapped state is when a Tile object has its color changed from a blank state to a colored state AND when the arrow moves (a tile may have changed to a colored state while the arrow was on top of it). So, the check method should be in the Arrow class and it should be 'public' so the world can call it when a tile changes to a colored state (which your 'checkGame2' method is).
if ((Arrow)getObjects(Arrow.class).get(0)).checkGame2()) gameOver();
Not that you should do anything about the following, now; but, thought you ought to know: Two things that would make things easier are (1) keeping track of how many tiles are blank (then, just checking its value for 25 would be enough to check for game over) and (2) keeping a reference to the Arrow object in the world class (so it does not have to search for it each time a tile changes to a colored state). For the edge/corner issue, you can either just go with being trapped at the locations, as is, or you can have the arrow wrap the edges of the world (leave one side and come in at the opposite side), giving it eight 'neighbors' no matter where it is at (the check will be a bit different for that obviously).
anonymousse anonymousse

2014/5/17

#
So then would that be added after tile.changecolor(); in the tiledelaycount method? When I use it after it, I get illegal start of expression. I also added java.util.List<Arrow> arrow = getObjects(Arrow.class); to keep a reference to the Arrow object in the world class. For number 1 though, what if 4 noncolored tiles just showed up around the arrow and there were not many in other areas? Then would checking the value for 25 still yield the same result?
danpost danpost

2014/5/17

#
The checking for 25 would be for the check that all tiles are blank; not for the trapping of the arrow. What you added to keep a reference to the Arrow object is not the way to do it; and, like I said before, you should not be doing anything about that right now You need to show what you did to come up with the illegal start of expression..
anonymousse anonymousse

2014/5/17

#
Here's what it is:
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();  
        if ((Arrow)getObjects(Arrow.class).get(0)).checkGame2()) gameOver();  

        tileDelayCount = 0;   

    }      
}  
danpost danpost

2014/5/17

#
I think you need another open parenthesis after the 'if' on line 15:
if (((Arrow)getObjects(Arrow.class).get(0)).checkGame2()) gameOver();
anonymousse anonymousse

2014/5/17

#
Ok I fixed that. Now I'm having the problem where if any noncolored tile is around the arrow the game will be over.
public boolean checkGame2() // no semi-color at end of this line  
{  
    for (Object obj : getNeighbours(1, false, Tile.class))  
    {  
        if (((Tile)obj).getColor() == color) return false;  
    }  
     
        return true;  
  
    
}  
}
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();  
        if (((Arrow)getObjects(Arrow.class).get(0)).checkGame2()) gameOver(); 

        tileDelayCount = 0;   

    }      
I removed the gameOver function from the arrow class. Would it be required since I am already calling it up in the world? I tried changing a part of the checkGame2 to
 if (((Tile)obj).getColor() != color) return true;  
    }  
     
        return false;  
  
    
}  
}
And then in the world class to:
if (((Arrow)getObjects(Arrow.class).get(4)).checkGame2()) gameOver(); 
Since I was thinking that would mean that the game only ended when there were 4 tiles surrounding that had different colors. But then my game won't run. Is the get function in the above code returning the number of tiles that satisfy the booleon?
danpost danpost

2014/5/18

#
I think you just need a little more quailifying for game over:
public boolean checkGame2() // no semi-color at end of this line  
{  
    for (Object obj : getNeighbours(1, false, Tile.class))  
    {  
        if (((Tile)obj).getColor() == color || ((Tile)obj).getColor() == 0) return false;  
    }  
        return true;  
    }  
}
If a neighboring tile is blank or is the same color then the game continues.
anonymousse anonymousse

2014/5/19

#
Ok so I'm trying to make easy normal and hard modes. I have one All world and a level 1, 2, and 3 world (for each of the difficulties). There's a choice in the beginning to choose the difficulty. My tile class has been fine so far. However, I am very confused with my arrow class. Each of the modes has another color arrow. Like easy has 2 colors, normal has 3, and hard has 4. I tried doing this with the public arrow class but for some reason then the game can't even find the arrow image and will automatically show a greenfoot image. Am I going to have to make a separate tile and arrow class for each level? Thanks as always for all your help!
  
public arrow{
if (getWorld() instanceof Level1)  {
            
            color = Greenfoot.getRandomNumber(2)+1; 
            setImage("arrow"+color+".png");         
    }
    
      if (getWorld() instanceof Level2)  {
            
            color = Greenfoot.getRandomNumber(3)+1; 
            setImage("arrow"+color+".png");         
    }
    
         if (getWorld() instanceof Level3)  {
            
            color = Greenfoot.getRandomNumber(4)+1; 
            setImage("arrow"+color+".png");         
    }
}
          
danpost danpost

2014/5/19

#
All you needed to do was to add a 'public static int level;' field in your All class and have each subclass of it use 'level = ??;' in its constructor. Then the Arrow could change images with 'setImage("arrow"+(Greenfoot.getRandomNumber(All.level+1)+1)+".png");'.
anonymousse anonymousse

2014/5/20

#
Thank you! I tried doing that earlier but I guess I was confused on how to define the level. It worked perfectly. I'm also trying to add a counter that can add the same color tile that the arrow currently is. I already have the following from my first changecolor and am going to add a changecolor2 method:
public void changecolor()    
{    
    Tile tile = (Tile)getOneObjectAtOffset(0, 0, Tile.class);  
    Actor arrow = getOneObjectAtOffset(0, 0, Arrow.class);     
        if (isTouching(Arrow.class)) return;
    color = Greenfoot.getRandomNumber(All.level+1)+1;    
    setImage("egg"+color+".png");    
}    
Is there a way I can recall the color to only be the current color the arrow is? I tried to do arrow.getColor, but I can't recall it. I can't define an arrow class in my world that is suggested for instance variables as there is more than one world.
danpost danpost

2014/5/20

#
You can always use:
int arrowColor = (Arrow)getWorld().getObjects(Arrow.class).get(0)).getColor();
This works because you will always only have one arrow in your worlds.
There are more replies on the next page.
1
2
3
4