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

2013/1/11

#
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.
What you had before looks fine. Why do you think it's wrong?
danpost danpost

2013/1/11

#
What you had between the brackets before looked good.
ctgreenfoot ctgreenfoot

2013/1/12

#
yeah i thought so too but greenfoot highlights the words 'addObject' in red and says cannot find symbol - method addObject(RedWorm,int,int)
ctgreenfoot ctgreenfoot

2013/1/12

#
i changed the code and now it works!
 if (counter.getValue() == 20)  
        {  
        getWorld().addObject(new RedWorm(), Greenfoot.getRandomNumber(1000), Greenfoot.getRandomNumber(800));
        } 
however, each time quite a few worms appear however what i need is for only one to appear. how do i change this?
danpost danpost

2013/1/12

#
It is probably because of where you placed that code; however, there is no way to tell unless you show your code (probably need the whole class).
ctgreenfoot ctgreenfoot

2013/1/12

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

/**
 * Write a description of class Crab here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Crab extends Animals
{
    /**
     * Act - do whatever the Crab wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */

    private Counter counter;

    public Crab(Counter pointCounter)
    {
        counter = pointCounter;
    }

    public void act() 
    {
        moveAndTurn(); 
        eatWorm();

    }  

    private void moveAndTurn()
    {  
        if (Greenfoot.isKeyDown ("space"))
            move (4);
        if (Greenfoot.isKeyDown ("right"))
            turn (3);

        if (Greenfoot.isKeyDown ("left"))
            turn (-3);
    }   

    private void eatWorm()
    {
        Actor Worm;
        Worm = getOneObjectAtOffset (0,0,Worm.class);
        if(Worm != null)
        {
            int x = Greenfoot.getRandomNumber(getWorld().getWidth());  
            int y = Greenfoot.getRandomNumber(getWorld().getHeight());  
            Worm.setLocation(x, y); 
            counter.add(1);
            Greenfoot.playSound ("eating.wav");
        }

        Actor RedWorm;
        RedWorm = getOneObjectAtOffset (0,0,RedWorm.class);
        if (RedWorm != null)
        {
            World world;
            world = getWorld ();
            world.removeObject (RedWorm);
            counter.add(5);
            Greenfoot.playSound ("eating.wav");
        }

        if (counter.getValue() == 20)  
        {  
        getWorld().addObject(new RedWorm(), Greenfoot.getRandomNumber(1000), Greenfoot.getRandomNumber(800));
        } 
        
        if (counter.getValue () == 50)
        {
            Greenfoot.stop();
            Greenfoot.playSound ("fanfare.wav");
            getWorld().addObject(new GameWon(), getWorld().getWidth()/2, getWorld().getHeight()/2);
        }

    } 
}
	
danpost danpost

2013/1/12

#
Move line 63 to 76 (the code from lines 65 to 75, you count value checks, should be placed after where you count value changes, line 61; within the same block of code).
ctgreenfoot ctgreenfoot

2013/1/12

#
it doesn't work, no red worms appear. help!
danpost danpost

2013/1/12

#
Not even one, when the counter hits 20? BTW, the condition should probably be:
if (counter.getValue()>=20)
(with '>=' instead of '==').
ctgreenfoot ctgreenfoot

2013/1/13

#
i changed that and still not even one appears
danpost danpost

2013/1/13

#
I realized that you have two places where the score is increased. Try the following for your 'eatWorm' method:
private void eatWorm()
{
    int addScore = 0; // to track any scoring
    Actor worm = getOneObjectAtOffset (0,0,Worm.class);
    if(worm != null)
    {
        int x = Greenfoot.getRandomNumber(getWorld().getWidth());  
        int y = Greenfoot.getRandomNumber(getWorld().getHeight());  
        worm.setLocation(x, y); 
        Greenfoot.playSound ("eating.wav");
        addScore += 1;
    }
    worm = getOneObjectAtOffset (0,0,RedWorm.class);
    if (worm != null)
    {
        getWorld().removeObject (RedWorm);
        Greenfoot.playSound ("eating.wav");
        addScore += 5;
    }
    if (addScore == 0) return; // exit method if no score
    int previousScore = counter.getValue();
    counter.add(addScore);
    if (previousScore+addScore >= 20)  
    {
        int x = Greenfoot.getRandomNumber(1000);
        int y = Greenfoot.getRandomNumber(800);
        getWorld().addObject(new RedWorm(), x , y);
    } 
    if (previousScore+addScore >= 50)
    {
        Greenfoot.stop();
        Greenfoot.playSound ("fanfare.wav");
        getWorld().addObject(new GameWon(), getWorld().getWidth()/2, getWorld().getHeight()/2);
    }
}
vonmeth vonmeth

2013/1/13

#
Line 16 of the code danpost posted will need to be:
getWorld().removeObject (worm);
danpost danpost

2013/1/13

#
Thanks, vonmeth. I must have missed that one during simplification.
ctgreenfoot ctgreenfoot

2013/1/13

#
thanks! it works now. however a random number of red worms appear. what i was requested to do was to: When score reaches 10, red worms appear, one by one, at random locations, 2% of the time, with max of 20 at a time how would i change it to that?
You need to login to post a reply.
1
2