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

#
heres my coding import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot) import java.awt.Font; /** * Counter that displays a number. * * @author Michael Kolling * @version 1.0.1 */ public class Counter extends Actor { private int value = 0; private int target = 0; private String text; private int stringLength; public Counter() { this(""); } public Counter(String prefix) { text = prefix; stringLength = (text.length() + 2) * 16; setImage(new GreenfootImage(stringLength, 24)); GreenfootImage image = getImage(); Font font = image.getFont(); image.setFont(font.deriveFont(24.0F)); // use larger font updateImage(); } public void act() { if(value < target) { value++; updateImage(); } else if(value > target) { value--; updateImage(); } } public void add(int score) { target += score; } public void subtract(int score) { target -= score; } public int getValue() { return value; } /** * Make the image */ private void updateImage() { GreenfootImage image = getImage(); image.clear(); image.drawString(text + value, 1, 18); } now i want to add 5 for each time My crab eats a worm any help??
danpost danpost

2011/12/22

#
Immediately after removing the worm that the crab eats, add the line 'counter.add(5);' (assuming 'counter' is the name of the reference to the counter).
programmer22 programmer22

2011/12/22

#
sorry that was confusing >.> just started but would i fix it in this coding or the crab file and if its in this one will u show ?
danpost danpost

2011/12/22

#
The crab is the one that eats the worm, so, yes, the Crab file is where you would be removing the worm from and adding to the counter from.
programmer22 programmer22

2011/12/22

#
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); 'counter.add(5);' Greenfoot.playSound("eating.wav"); } } } as u can see i put it under world remove object worm and it says unclosed character literal? sorry im wasting so much of your time on such a petty matter >.>
danpost danpost

2011/12/22

#
No problem, the single quotes around itI added to show what was to be added. Remove the single quotes around that statement. Since it is in your crab class add the word 'world.' (without the quotes, but with the dot) before counter.add(5); (Your world class has a public reference to the counter? and you named it counter?)
programmer22 programmer22

2011/12/22

#
no counter is in actors and its name is Counter
danpost danpost

2011/12/22

#
Yes, you are talking about the class Counter -- that is well and good. What I am asking is: when you (in your world class) created the counter, did you save a reference to it (the key word is reference)?
programmer22 programmer22

2011/12/22

#
i didnt really save anything >.> just went to a website copied it and made new subclass made a pic for counter then put the copied code into it and thats where im at >.> sorry if this doesnt make sense im really new >.>
danpost danpost

2011/12/22

#
Let's see the code in your world -- anything related to the counter.
programmer22 programmer22

2011/12/22

#
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(560, 560, 1); prepare(); } /** * Prepare the world for the start of the program. That is: create the initial * objects and add them to the world. */ private void prepare() { Crab crab = new Crab(); addObject(crab, 257, 300); Worm worm = new Worm(); addObject(worm, 392, 50); Worm worm2 = new Worm(); addObject(worm2, 462, 264); Worm worm3 = new Worm(); addObject(worm3, 425, 501); Worm worm4 = new Worm(); addObject(worm4, 413, 227); Worm worm5 = new Worm(); addObject(worm5, 427, 460); Worm worm6 = new Worm(); addObject(worm6, 179, 488); Worm worm7 = new Worm(); addObject(worm7, 246, 495); Worm worm8 = new Worm(); addObject(worm8, 54, 443); Worm worm9 = new Worm(); addObject(worm9, 88, 269); Worm worm10 = new Worm(); addObject(worm10, 105, 193); Worm worm11 = new Worm(); addObject(worm11, 103, 52); Worm worm12 = new Worm(); addObject(worm12, 223, 109); Worm worm13 = new Worm(); addObject(worm13, 280, 71); Worm worm14 = new Worm(); addObject(worm14, 339, 195); Worm worm15 = new Worm(); addObject(worm15, 525, 105); Worm worm16 = new Worm(); addObject(worm16, 480, 400); Worm worm17 = new Worm(); addObject(worm17, 130, 390); Lobster lobster = new Lobster(); addObject(lobster, 112, 474); Lobster lobster2 = new Lobster(); addObject(lobster2, 94, 112); Counter counter = new Counter(); addObject(counter, 208, 266); counter.setLocation(30, 26); } } that is my code for the world
danpost danpost

2011/12/22

#
OK, there is no reference for the counter in the world class. So, instead of
world.counter.add(5);
put (in the same place)
Counter counter = (getWorld().getObjects(Counter.class)).get(0);
counter.add(5);
You may have to add at the top of the class, right after import Greenfoot.*; the line import java.util.List;
programmer22 programmer22

2011/12/22

#
you mean in crab right? right at the top after greenfoot*; btw not going to respond because going to sleep but will look at your answer in the morning
danpost danpost

2011/12/22

#
Yes.
programmer22 programmer22

2011/12/22

#
here i put it after and it says class,interface and enum see i put it where u said import greenfoot.*;Counter counter = (getWorld().getObjects(Counter.class)).get(0); counter.add(5); // (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); Greenfoot.playSound("eating.wav"); } } }
There are more replies on the next page.
1
2