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

2012/4/12

Scoring and certain bugs in my game

DHoffman94 DHoffman94

2012/4/12

#
My current project, posted at http://www.greenfoot.org/scenarios/4764, has some problems. Scoring is unreliable, as the scoring system lags considerably. Also, due to the fact that the shield is constantly getting X and Y coordinates from the rocket, i get errors when the rocket is destroyed due to the fact that X and Y cannot be taken from an object not entered into the world. Please, any help with the problems would be appreciated.
SPower SPower

2012/4/12

#
You have to move the method which can remove the rocket to the end of the act() method in the rocket.
DHoffman94 DHoffman94

2012/4/12

#
Sweet, well that one was an easy fix. Thank you. Now i just have to fix the scoring bug.
SPower SPower

2012/4/12

#
Can you describe the bug a little bit more?
DHoffman94 DHoffman94

2012/4/12

#
For every hit, the amount of damage is converted into a score. However, the score lags so that after death the score counter will keep going up to the correct score, whereas the scoreboard will display the incorrect score. I will update the online scenario with the corrected Rocket.class so that you will see.
DHoffman94 DHoffman94

2012/4/12

#
New update has been posted.
nccb nccb

2012/4/12

#
For the scoring problem: the version of the Counter in the beta always animates the score, so if you add big numbers, it will lag. In the final release, we've added a setValue method that sets the value directly. You should be able to add this method to your Counter class by editing the source and copy-pasting it in:
/**
     * Set a new counter value.  This will not animate the counter.
     */
    public void setValue(int newValue)
    {
        target = newValue;
        value = newValue;
        updateImage();
    }
Then use:
scoreCounter.setValue(100 + scoreCounter.getValue());
instead of:
scoreCounter.add(100);
Edit: and the other change we've made is to fix getValue, which should do "return target;" not "return value;", which explains the incorrect score you're seeing. Apologies for the bugs, but that's what happens with beta releases!
DHoffman94 DHoffman94

2012/4/12

#
I have put the specified method in, however when trying to implement i get an error: Non static method setValue(int) cannot be referenced from a static context.
DHoffman94 DHoffman94

2012/4/12

#
In Asteroid.class:
import greenfoot.*;  // (World, Actor, GreenfootImage, and Greenfoot)

/**
 * A rock in space
 * 
 * @author Poul Henriksen
 */
public class Asteroid extends SmoothMover
{
    /** Size of this asteroid */
    private int size;

    /** When the stability reaches 0 the asteroid will explode */
    private int stability;

    
    public Asteroid()
    {
        this(50);
    }
    
    public Asteroid(int size)
    {
        super(new Vector(Greenfoot.getRandomNumber(360), 2));
        setSize(size);
    }
    
    public Asteroid(int size, Vector speed)
    {
        super(speed);
        setSize(size);
    }
    
    public void act()
    {         
        move();
    }

    /**
     * Set the size of this asteroid. Note that stability is directly
     * related to size. Smaller asteroids are less stable.
     */
    public void setSize(int size) 
    {
        stability = size;
        this.size = size;
        GreenfootImage image = getImage();
        image.scale(size, size);
    }

    /**
     * Return the current stability of this asteroid. (If it goes down to 
     * zero, it breaks up.)
     */
    public int getStability() 
    {
        return stability;
    }
    
    /**
     * Hit this asteroid dealing the given amount of damage.
     */
    public void hit(int damage) 
    {
        Counter.add(damage);
        stability = stability - damage;
        if(stability <= 0) 
            breakUp ();         
    }
    
    /**
     * Break up this asteroid. If we are still big enough, this will create two
     * smaller asteroids. If we are small already, just disappear.
     */
    private void breakUp() 
    {
        Greenfoot.playSound("Explosion.wav");
        
        if(size <= 16) 
        {
            getWorld().removeObject(this);
            Space.aNumber--;
        }
        else 
        {
            int r = getMovement().getDirection() + Greenfoot.getRandomNumber(45);
            double l = getMovement().getLength();
            Vector speed1 = new Vector(r + 60, l * 1.2);
            Vector speed2 = new Vector(r - 60, l * 1.2);        
            Asteroid a1 = new Asteroid(size/2, speed1);
            Asteroid a2 = new Asteroid(size/2, speed2);
            getWorld().addObject(a1, getX(), getY());
            getWorld().addObject(a2, getX(), getY());        
            a1.move();
            a2.move();
            Space.aNumber = Space.aNumber + 2;
        
            getWorld().removeObject(this);
            Space.aNumber--;
        }
    }
}
It is in the hit() method that i get the error Edit: the error occurs after changing Counter.add(damage) to Counter.setValue(damage)
danpost danpost

2012/4/12

#
The methods 'add(int)' and 'setValue(int)' are not 'Class' methods, but rather 'object' methods. That is to say, Counter is the name of the class and not what you control a value of. You need to get a reference to a Counter 'object' (scoreCounter) and apply those methods to it. If you only have one counter than
Counter scoreCounter = (Counter) ((Space) getWorld()).getObjects(Counter.class).get(0);
scoreCounter.setValue(damage);
If you already have a reference to the object, ignore the first line and change 'scoreCounter' to the name of the Counter object. BTW: I used 'Space' as the name of the sub-class of World, change that name accordingly.
nccb nccb

2012/4/12

#
@DHoffman94: that's because you've modified the Counter class to turn various bits static. Either finish the job and make setValue method static too (probably the easiest thing), or turn the other bits back and use the class non-statically.
You need to login to post a reply.