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

2013/7/27

Score won't reset when scenario is reset

redninja21 redninja21

2013/7/27

#
In my scenario : http://www.greenfoot.org/scenarios/9071 when I click reset, the score is not resetting to 0 after every game. below is the code i used for the projectile: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Projectile * * @redninja21 * @1.0 */ public class Projectile extends Actor { /** * Act - do whatever the Projectile wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { move(10); checkForWorldEdge(); } public void checkForWorldEdge(){ if(atWorldEdge()){ getWorld().removeObject(this); } else{ if(canSee(Asteroid.class)){ getWorld().removeObject(getOneIntersectingObject(Asteroid.class)); getWorld().removeObject(this); SpaceWorld world = (SpaceWorld) getWorld(); world.asteroids--; world.asteroidcount++; //whenever an asteroid is destroyed it adds one to the score } } } /** * Test if we are close to one of the edges of the world. Return true is we are. */ public boolean atWorldEdge() { if(getX() < 10 || getX() > getWorld().getWidth() - 10) return true; if(getY() < 10 || getY() > getWorld().getHeight() - 10) return true; else return false; } /** * Return true if we can see an object of class 'clss' right where we are. * False if there is no such object here. */ public boolean canSee(Class clss) { Actor actor = getOneObjectAtOffset(0, 0, clss); return actor != null; } /** * Try to eat an object of class 'clss'. This is only successful if there * is such an object where we currently are. Otherwise this method does * nothing. */ public void eat(Class clss) { Actor actor = getOneObjectAtOffset(0, 0, clss); if(actor != null) { getWorld().removeObject(actor); } } } and here is the code i used to display the score: import greenfoot.*; import java.awt.Color; public class GameOver extends Actor { Color colors = { Color.ORANGE, Color.RED }; int colorNum = 0; int counter = 1; public void act() { if (--counter == 0) { colorNum = ++colorNum % 2; updateImage(); counter = 50; } } public void updateImage() { SpaceWorld world = (SpaceWorld) getWorld(); String s1 = new String(); s1 = "" + world.asteroidcount; setImage(new GreenfootImage("GAME\nOVER\nYOUR SCORE : " + s1, 96, colors, new Color(0, 0, 0, 0))); } } someone please help
danpost danpost

2013/7/27

#
In your world constructor, add a line to set asteroidcount to zero (or remove 'static' from the field declaration).
danpost danpost

2013/7/27

#
Also, you do not need the 'asteroids' field. You can use 'getObjects(Asteroid.class).size()' to determine how many asteroids are currently in the world.
redninja21 redninja21

2013/7/27

#
hmmm I'm getting a nullPointerException when I remove "static" from the field of declaration
danpost danpost

2013/7/27

#
You just need to change the order of some of the statements in the 'checkForWorldEdge' method in the Projectile class:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
public void checkForWorldEdge() {
    if(atWorldEdge()) {
        getWorld().removeObject(this);
    }
    else {
        if(canSee(Asteroid.class)) {
            SpaceWorld world = (SpaceWorld) getWorld();
            world.removeObject(getOneIntersectingObject(Asteroid.class));
            world.removeObject(this);
            world.asteroids--;
            world.asteroidcount++; //whenever an asteroid is destroyed it adds one to the score
        }
    }
}
redninja21 redninja21

2013/7/27

#
oh thanks a lot it works now!
kira_25@live.com kira_25@live.com

2013/9/8

#
How do I change the movement of the Asteroid from Top to bottom instead of Right to left? and the Rockets as well? A reply kindly appreciated
SPower SPower

2013/9/8

#
@kira, please create a new discussion
You need to login to post a reply.