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

2011/12/22

counter question

1
2
programmer22 programmer22

2011/12/22

#
also does the counter need to be a world or an actor?
programmer22 programmer22

2011/12/22

#
i just switched it to world class so yeah ....
programmer22 programmer22

2011/12/22

#
switched it to world and it put it back in actor
danpost danpost

2011/12/22

#
NOOOOO, you did not read what I wrote. Your first two lines should be
import Greenfoot.*;
import java.util.List; // needed to create a list and use its methods
Then you should have:
public class Crab extends Actor
{
    /**
     * Act - do whatever the Crab wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    { 
// etc.
Finally, the eat method:
 public void eat()
{    
    Actor worm;
    worm = getOneObjectAtOffset (0, 0, Worm.class);
    if (worm != null)
    {
        World world;
        world = getWorld();
        world. removeObject(worm);
        List<Counter> counters = world.getObjects(Counter.class); // this lists all counters in the world (even if just one)
        Counter counter = counters.get(0); // this get the first counter from the list (the only one)
        counter.add(5); // this adds 5 to the counter pulled from the list
        Greenfoot.playSound("eating.wav");
    }
}
danpost danpost

2011/12/22

#
The counter IS an Actor! Anything that is put into the world with addObject IS an actor object.
danpost danpost

2011/12/22

#
The counter IS an Actor! Anything that is put into the world with addObject IS an actor object.
programmer22 programmer22

2011/12/22

#
ok i copied and pasted what you put and it says cannot find symbol -class list sorry if im frustrating you just started so im not sure about all this programming lingimport greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) public class Crab extends Actor { /** * Act - do whatever the Crab wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { moveAndTurn(); eat(); } public void moveAndTurn() { move(4); if (Greenfoot.isKeyDown("right")) { turn(-10); } if (Greenfoot.isKeyDown("left")) { turn (10); } } public void eat() { Actor worm; worm = getOneObjectAtOffset (0, 0, Worm.class); if (worm != null) { World world; world = getWorld(); world. removeObject(worm); List<Counter> counters = world.getObjects (Counter.class); // this lists all counters in the world (even if just one) 11. Counter counter = counters.get(0); // this get the first counter from the list (the only one) 12. counter.add(5); // this adds 5 to the counter pulled from the list Greenfoot.playSound("eating.wav"); } } } o anyways heres what i have now
programmer22 programmer22

2011/12/22

#
oooh should i have taken out some things here lemme mess with it
programmer22 programmer22

2011/12/22

#
dang still says the same thing
danpost danpost

2011/12/22

#
You only copied the eat() method!!! Look closely at the first block of code, of the three I just posted together. You still do not have the second import line in your code.
programmer22 programmer22

2011/12/23

#
ok made sure i did all the coding and only some of it is colored the rest is grayish it says cannot find symbol- class list then it high lights the word list here it is again import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) public class Crab extends Actor { /** * Act - do whatever the Crab wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { moveAndTurn(); eat(); } public void moveAndTurn() { move(4); if (Greenfoot.isKeyDown("right")) { turn(-10); } if (Greenfoot.isKeyDown("left")) { turn (10); } } public void eat() { Actor worm; worm = getOneObjectAtOffset (0, 0, Worm.class); if (worm != null) { World world; world = getWorld(); world. removeObject(worm); List<Counter> counters = world.getObjects (Counter.class); // this lists all counters in the world (even if just one) Counter counter = counters.get (0); // this get the first counter from the list (the only one) counter.add(5); // this adds 5 to the counter pulled from the list Greenfoot.playSound("eating.wav"); } } }
programmer22 programmer22

2011/12/23

#
dang i see where i messed it up
programmer22 programmer22

2011/12/23

#
omg im so stupid for got to copy the top -.-
programmer22 programmer22

2011/12/23

#
it works !!!! thank you ! but gave me a warning about recompiling but it works !! ty !!!
You need to login to post a reply.
1
2