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

2013/11/11

Help!! my score.....

payner2013 payner2013

2013/11/11

#
ok i thought it was working... but apparently not, what is wrong... space class:
 public Space()
    {    
        // Create a new world with 800x600 cells with a cell size of 1x1 pixels.
        super(800, 600, 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()
    {
        RestartSun restartsun2 = new RestartSun();
        RestartSun restartsun3 = new RestartSun();
        addObject(restartsun3, 799, 41);
        MenuMoon menumoon2 = new MenuMoon();
        addObject(menumoon2, 21, 8);
        Rocket rocket = new Rocket();
        addObject(rocket, 68, 218);
        Rock rock = new Rock(counter);
        addObject(rock, 567, 374);
        Rock rock2 = new Rock(counter);
        addObject(rock2, 466, 222);
        Rock rock3 = new Rock(counter);
        addObject(rock3, 428, 45);
        Rock rock4 = new Rock(counter);
        addObject(rock4, 80, 338);
        Rock rock5 = new Rock(counter);
        addObject(rock5, 228, 290);
        Rock rock6 = new Rock(counter);
        addObject(rock6, 130, 58);
        Rock rock7 = new Rock(counter);
        addObject(rock7, 388, 337);
        rocket.setLocation(94, 189);
        rock4.setLocation(91, 525);
        rock5.setLocation(301, 488);
        rock7.setLocation(320, 383);
        rock2.setLocation(540, 509);
        rock.setLocation(738, 453);
        rock2.setLocation(573, 527);
        Rock rock8 = new Rock();
        addObject(rock8, 652, 165);
        rock7.setLocation(484, 373);
        Rock rock9 = new Rock();
        addObject(rock9, 205, 355);
        Rock rock10 = new Rock();
        addObject(rock10, 317, 143);
        rock9.setLocation(177, 365);
        rock9.setLocation(157, 348);
        rock10.setLocation(313, 139);
        rocket.setLocation(74, 284);
        rock9.setLocation(182, 397);
        rocket.setLocation(63, 302);
        Bullet bullet = new Bullet();
        addObject(bullet, 137, 301);
        removeObject(bullet);
        Counter counter = new Counter(rock);
        addObject(counter, 62, 592);
        counter.setLocation(56, 584);
    }
}
Rock class:
public class Rock extends Actor
{
    private int points = 0;
    public Rock (Counter pointCounter)
    {
    }
    
    public void act()
    {
        moveAndTurn();
        crash();
            }
            
           public void moveAndTurn()
    {
      move(1);
 {
     if (Greenfoot.getRandomNumber(100) <10)
{
    turn(Greenfoot.getRandomNumber(90) - 45);
}
if (getX() <= 5 || getY() >= getWorld().getWidth() - 5)
{
    turn(180);
}if (getY() <= 5 || getY() >= getWorld().getHeight() - 5)
{
    turn(180);
}
}
}
public void crash()
{
    {
        Actor rocket;
    rocket = getOneObjectAtOffset(0, 0, Rocket.class);
    if (rocket != null)
    {
        World world;
        world = getWorld();
        world.removeObject(rocket);
}
}
    {
        Actor Bullet;
    Bullet = getOneObjectAtOffset(0, 0, Bullet.class);
    if (Bullet != null)
    {
        World world;
        world = getWorld();
        world.removeObject(this);
        world.removeObject(Bullet);
}
}
}
}
when i compile, is says cannot find symbol - variable counter... help me plz thank you
shrucis1 shrucis1

2013/11/11

#
Likely, you don't have a Counter class, it's not built in. Try adding this:
import greenfoot.*;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Font;


public class Counter extends Actor
{
    private static final Color textColor = new Color(0, 0, 0);
    
    private int value = 0;
    private int target = 0;
    private String text;
    private int stringLength;
    private Font textFont = new Font ("Arial", Font.BOLD, 14);

    public Counter()
    {
        this("");
    }

    public Counter(String prefix)
    {
        text = prefix;
        stringLength = (text.length() + 2) * 10;

        setImage(new GreenfootImage(stringLength, 16));
        GreenfootImage image = getImage();
        image.setColor(textColor);
        image.setFont(textFont);
        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;
    }

    private void updateImage()
    {
        GreenfootImage image = getImage();
        image.clear();
        image.drawString(text + value, 1, 12);
    }
}
Also, just a note, but in the Rock class at line 45, you might want to do this instead:
Bullet = getOneIntersectingObject(Bullet.class);
I'm not sure if there's a difference, I just like using that one; it probably doesn't matter.
shrucis1 shrucis1

2013/11/11

#
Then, in line 59 of your space class, when you create the counter do it like this:
Counter counter = new Counter("Score"); //Put whatever you want instead of "Score", just make sure it has quotes around it  
The add, subtract, and getValue methods are already in the Counter Class
payner2013 payner2013

2013/11/11

#
yeah sorry i forgot i alrady have a counter class also... and ok thank you
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;

/**
 * A simple counter with graphical representation as an actor on screen.
 * 
 * @author mik
 * @version 1.0
 */
public class Counter extends Actor
{
    private static final Color transparent = new Color(0,0,0,0);
    private GreenfootImage background;
    private int value;
    private int target;

    /**
     * Create a new counter, initialised to 0.
     */
    public Counter()
    {
        background = getImage();  // get image from class
        value = 0;
        target = 0;
        updateImage();
    }
    
    /**
     * Animate the display to count up (or down) to the current target value.
     */
    public void act() 
    {
        if (value < target) {
            value++;
            updateImage();
        }
        else if (value > target) {
            value--;
            updateImage();
        }
    }

    /**
     * Add a new score to the current counter value.
     */
    public void add(int score)
    {
        target += score;
    }

    /**
     * Return the current counter value.
     */
    public int getValue()
    {
        return value;
    }

    /**
     * Set a new counter value.
     */
    public void setValue(int newValue)
    {
        target = newValue;
        value = newValue;
        updateImage();
    }

    /**
     * Update the image on screen to show the current value.
     */
    private void updateImage()
    {
        GreenfootImage image = new GreenfootImage(background);
        GreenfootImage text = new GreenfootImage("" + value, 22, Color.BLACK, transparent);
        image.drawImage(text, (image.getWidth()-text.getWidth())/2, 
                        (image.getHeight()-text.getHeight())/2);
        setImage(image);
    }
}
shrucis1 shrucis1

2013/11/11

#
Your counter class is slightly different from mine; mine has a constructor that takes a string for a prefix, while yours's constructor gets the image from the original image. You might want to try copying the one I gave you into that Counter class; they have the same methods, the only difference is how they are instantiated.
payner2013 payner2013

2013/11/11

#
i just tryed that haha... comes up with the same error... im so confused :s
danpost danpost

2013/11/11

#
First remove the argument from the Rock class constructor (line 4 in your Rock class code above). Then, change lines 22, 24, 26, 28, 30, 32, and 34 in your Space class to '= new Rock();'. You can adjust your Counter constructor and line 59 to what shrucis1 suggests. Then we can work on getting the Counter object working.
payner2013 payner2013

2013/11/12

#
public class Space extends World
{

    /**
     * Constructor for objects of class Space.
     * 
     */
    public Space()
    {    
        // Create a new world with 800x600 cells with a cell size of 1x1 pixels.
        super(800, 600, 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()
    {              
        RestartSun restartsun2 = new RestartSun();
        RestartSun restartsun3 = new RestartSun();
        addObject(restartsun3, 799, 41);
        MenuMoon menumoon2 = new MenuMoon();
        addObject(menumoon2, 21, 8);
        Rocket rocket = new Rocket();
        addObject(rocket, 68, 218);
        Rock rock = new Rock();
        addObject(rock, 567, 374);
        Rock rock2 = new Rock();
        addObject(rock2, 466, 222);
        Rock rock3 = new Rock();
        addObject(rock3, 428, 45);
        Rock rock4 = new Rock();
        addObject(rock4, 80, 338);
        Rock rock5 = new Rock();
        addObject(rock5, 228, 290);
        Rock rock6 = new Rock();
        addObject(rock6, 130, 58);
        Rock rock7 = new Rock();
        addObject(rock7, 388, 337);
        rocket.setLocation(94, 189);
        rock4.setLocation(91, 525);
        rock5.setLocation(301, 488);
        rock7.setLocation(320, 383);
        rock2.setLocation(540, 509);
        rock.setLocation(738, 453);
        rock2.setLocation(573, 527);
        Rock rock8 = new Rock();
        addObject(rock8, 652, 165);
        rock7.setLocation(484, 373);
        Rock rock9 = new Rock();
        addObject(rock9, 205, 355);
        Rock rock10 = new Rock();
        addObject(rock10, 317, 143);
        rock9.setLocation(177, 365);
        rock9.setLocation(157, 348);
        rock10.setLocation(313, 139);
        rocket.setLocation(74, 284);
        rock9.setLocation(182, 397);
        rocket.setLocation(63, 302);
        Bullet bullet = new Bullet();
        addObject(bullet, 137, 301);
        removeObject(bullet);
        Counter counter7 = new Counter("0");
        addObject(counter7, 65, 582);
        counter7.setLocation(56, 582);
ok so i have doen that, and then did what shrucis1 surgests and it say i have another error... constructor counter in class counter cannot be applied to given types?
danpost danpost

2013/11/12

#
Change line 66 to 'Counter counter7 = new Counter();'.
payner2013 payner2013

2013/11/12

#
ahh yeah... thank you :) so now where do i start on the counter object...?
danpost danpost

2013/11/12

#
OK, since you only have one counter object, just use the following to access it from an actor class:
Counter counter = (Counter)getWorld().getObjects(Counter.class).get(0);
Anytime you "score", access it and then use 'counter.add(/* amount to add */);'. You could combine the two statements into one, using:
((Counter)getWorld().getObjects(Counter.class).get(0)).add(/* amount to add */);
payner2013 payner2013

2013/11/12

#
ok so now this is what ive got
 Actor Bullet;
    Bullet = getOneObjectAtOffset(0, 0, Bullet.class);
    if (Bullet != null)
    {
        World world;
        world = getWorld();
        world.removeObject(this);
        world.removeObject(Bullet);
        Counter counter = (Counter)getWorld().getObjects(Counter.class).get(0);
        counter.add(10);
but now i get this...: java.lang.NullPointerException at Rock.crash(Rock.java:57) at Rock.act(Rock.java:16) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) java.lang.NullPointerException at Rock.crash(Rock.java:57) at Rock.act(Rock.java:16) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205)
danpost danpost

2013/11/12

#
Change 'getWorld()' on line 9 to 'world'.
payner2013 payner2013

2013/11/12

#
Thank you soo much!!!!!!!! :D you have no idea how happy i am. im sorry its taken you this long i have been trying to look it up but nothing was working for me so sorry... but thank you ever so much!!!!!!!! :D and thank you shrucis1. you bothe helped alot :D
You need to login to post a reply.