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

2013/1/1

Do not understand what is wrong with the code

1
2
ctgreenfoot ctgreenfoot

2013/1/1

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class CrabWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class CrabWorld extends World
{



    /**
     * Constructor for objects of class CrabWorld.
     * 
     */
    public CrabWorld()
    {    
            super(1000, 800, 1); 
        
       
        prepareWorld();
        backgroundMusic();
     
    }
    
    
    private void backgroundMusic()
    {
       Greenfoot.playSound("background.mp3");
    }
       
    private void prepareWorld()
    {
        Counter counter = new Counter();
        addObject(counter, 100, 70);
        
        Crab crab = new Crab(counter);
        addObject(crab, 500, 400);
        
        CountdownClock countdownClock = new CountdownClock();
        addObject(countdownClock, 100, 40);
       
        
for (int item=0; item<20; item++)
      addObject (new Worm(), (Greenfoot.getRandomNumber(getWidth())), (Greenfoot.getRandomNumber(getHeight())));
            

for (int item=0; item<2; item++)
       addObject (new Lobster(), (Greenfoot.getRandomNumber(getWidth())), (Greenfoot.getRandomNumber(getHeight())));
         
       if (counter.getValue() = 20)
       {
           RedWorm redWorm = new RedWormd();
           addObject(redWorm, (Greenfoot.getRandomNumber(getWidth())), (Greenfoot.getRandomNumber(getHeight())));
        }
    }
}

when i try to compile that, this part highlights and the error message is "unexpected type, required:variable, found:value"
    
       if (counter.getValue() = 20)
       {
           RedWorm redWorm = new RedWormd();
           addObject(redWorm, (Greenfoot.getRandomNumber(getWidth())), (Greenfoot.getRandomNumber(getHeight())));
        }
ctgreenfoot ctgreenfoot

2013/1/1

#
** 

if (counter.getValue() = 20)
       {
           RedWorm redWorm = new RedWorm();
           addObject(redWorm, (Greenfoot.getRandomNumber(getWidth())), (Greenfoot.getRandomNumber(getHeight())));
        }
danpost danpost

2013/1/1

#
You are using an assignment operator: = , instead of a comparison operator: == in the 'if' statement.
ctgreenfoot ctgreenfoot

2013/1/9

#
it still doesn't work. when the score reaches 20 no new red worms appear
davmac davmac

2013/1/9

#
The checking of the counter value is being done in the prepareWorld() method. The counter value will be 0 when that runs. You need to check the counter value and act on it from somewhere that gets run repeatedly (eg. the act() method) or at least whenever the counter gets increased.
ctgreenfoot ctgreenfoot

2013/1/10

#
im not quite sure which act method you are referring to. is it the act method of the counter itself? and if so then how will I tie that to the world prepare method
davmac davmac

2013/1/10

#
The logical place would probably be the act method of the world (but the act method of the counter could also work, yes). Make 'counter' an instance variable instead of a local variable, so that you can access it from the world's act method. You wouldn't "tie" it to the world prepare method, that's the point. The prepare method is the wrong place for this check and action.
ctgreenfoot ctgreenfoot

2013/1/10

#
oh ok i understand. so i think that im going to make the counter in the act method of the world. but how you make it an instance variable?
davmac davmac

2013/1/10

#
Move the declaration outside the method (and usually put it at the top of the class).
public class CrabWorld extends World  
{  
  private Counter counter = new Counter();
i.e. you move line 36 from your code outside the method (and optionally put 'private' before it).
davmac davmac

2013/1/10

#
But, you don't make the counter in the act method. You just check its value. You would still add the counter to the world as part of the prepare() method.
ctgreenfoot ctgreenfoot

2013/1/11

#
did i do it correctly?
public class CrabWorld extends World
{

    private Counter counter = new Counter();
    if (counter.getValue() = 20)  
    {  
        RedWorm redWorm = new RedWorm();  
        addObject(redWorm, (Greenfoot.getRandomNumber(getWidth())), (Greenfoot.getRandomNumber(getHeight())));  
    }  
it will not compile and says: illegal start of type in front of the if
danpost danpost

2013/1/11

#
Your code starting at line 5 should be within a method (probably the 'eatWorm' method) after the line where you increase the value of the counter (counter.add(5)).
davmac davmac

2013/1/11

#
Your code at line 4 declares the variable - makes it exist. That line is now in the right place. You need to access the variable (lines 5 - 9), like I said earlier, from within the act method, or otherwise from everywhere that you increase the counter value. These lines are currently in the wrong place. Finally, as danpost said earlier, you need to use '==' and not just '=' to compare values. Line 5 is wrong.
ctgreenfoot ctgreenfoot

2013/1/11

#
i've done that so i've written this:
if (counter.getValue() == 20)  
        {
        }
but im wondering what to put in between the brackets. basically what i want is for one red worm to appear in a random place on screen.
ctgreenfoot ctgreenfoot

2013/1/11

#
i wrote that in the eatWorm method as Danpost suggested
There are more replies on the next page.
1
2