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

2017/8/19

help with score

mole2003 mole2003

2017/8/19

#
my score isn't working it only goes up once but then stops. Everything else works. Any ideas?
/**
     * Act - do whatever the alien wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
      movement(); 
      if (isTouching(bullet.class))
      {
       die();
      }
      if (Greenfoot.getRandomNumber(1000) < 2)
      {
          fire();
      }
      if (deathCount == 18)
      {
         Greenfoot.setWorld( new WIN()); 
      }
    }
    
    public void movement()
    {
       if (Greenfoot.getRandomNumber(100) >= 50)
       {
          setRotation(180);
          move(3);
       }
       else
       {
         setRotation(0);
         move(3);
       }
       
    }
    private int deathCount = 0;
    private int score = 0;
    public void die()
    {
      
          removeTouching(bullet.class);
          deathCount = deathCount + 1;
          score = score + 100;
          getWorld().showText(Integer.toString(score), 500, 26);
          getWorld().removeObject(this);
      
    }
    public void fire()
    {
      getWorld().addObject( new alienbullet(), getX(), getY());  
    }
Super_Hippo Super_Hippo

2017/8/19

#
Each object has its own deathCount and score variables and each one only dies once. It would probably do what you want it to do if you make "score" and "deathCount" to static. However, the better way of doing it is to save the score in the world and not in your enemy class.
mole2003 mole2003

2017/8/19

#
how do you make it into static? I am new to Greenfoot.
Super_Hippo Super_Hippo

2017/8/19

#
Just add the word "static":
private static int ...
All objects from a class share one static variable. It is not created for every object like non-static variables are. If you save the score in your world, it could look like this:
//in the world
private int score = 0;


public void accScore(int amount)
{
    score += amount;

}
public int getScore()
{
    return score;
}
//when adding points
((WorldName) getWorld()).addScore(100);
//when checking if 18 aliens are killed
if (((WorldName) getWorld()).getScore() > 17)
{
    //win
}
mole2003 mole2003

2017/8/20

#
how would i ad it into the world class?
**
 * Write a description of class MyWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class MyWorld extends World
{

    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    public MyWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(1000, 700, 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()
    {
        alien alien = new alien();
        addObject(alien,66,71);
        alien.setLocation(66,70);
        alien.setLocation(75,71);
        alien alien2 = new alien();
        addObject(alien2,223,71);
        alien2.setLocation(221,62);
        alien2.setLocation(214,69);
        alien alien3 = new alien();
        addObject(alien3,376,71);
        alien3.setLocation(379,67);
        alien alien4 = new alien();
        addObject(alien4,558,71);
        alien4.setLocation(558,71);
        alien alien5 = new alien();
        addObject(alien5,709,71);
        alien5.setLocation(707,72);
        alien alien6 = new alien();
        addObject(alien6,888,71);
        alien6.setLocation(894,73);
        alien alien7 = new alien();
        addObject(alien7,77,204);
        alien7.setLocation(77,178);
        alien alien8 = new alien();
        addObject(alien8,219,175);
        alien8.setLocation(216,180);
        alien alien9 = new alien();
        addObject(alien9,373,184);
        alien9.setLocation(379,178);
        alien alien10 = new alien();
        addObject(alien10,559,176);
        alien10.setLocation(557,178);
        alien alien11 = new alien();
        addObject(alien11,699,187);
        alien11.setLocation(699,180);
        alien alien12 = new alien();
        addObject(alien12,887,179);
        alien12.setLocation(893,182);
        alien alien13 = new alien();
        addObject(alien13,86,311);
        alien13.setLocation(80,300);
        alien alien14 = new alien();
        addObject(alien14,213,305);
        alien14.setLocation(219,303);
        alien14.setLocation(219,302);
        alien alien15 = new alien();
        addObject(alien15,383,309);
        alien15.setLocation(372,299);
        alien alien16 = new alien();
        addObject(alien16,546,314);
        alien16.setLocation(548,302);
        alien alien17 = new alien();
        addObject(alien17,706,307);
        alien17.setLocation(695,307);
        alien alien18 = new alien();
        addObject(alien18,881,298);
        alien18.setLocation(897,305);
        rocket rocket = new rocket();
        addObject(rocket,460,616);
        rocket.setLocation(448,614);

        base1 base1 = new base1();
        addObject(base1,174,534);
        base1.setLocation(162,523);
        base1.setLocation(158,522);
        base2 base2 = new base2();
        addObject(base2,380,529);
        base2.setLocation(374,518);
        base3 base3 = new base3();
        addObject(base3,577,517);
        base3.setLocation(590,520);
        base3.setLocation(591,518);
        base4 base4 = new base4();
        addObject(base4,767,522);
        base4.setLocation(772,522);
    }
    
    
}
Super_Hippo Super_Hippo

2017/8/20

#
Just add the lines I gave you, so for example
private int score = 0;
in line 9 and the two methods in line 105.
mole2003 mole2003

2017/8/21

#
how would I put it if to add to the score two objects (alien and rocket) were intersecting?
Super_Hippo Super_Hippo

2017/8/21

#
//in rocket's act method
if (isTouching(Alien.class))
{
    removeTouching(Alien.class);
    ((WorldName) getWorld()).addScore(100);
    getWorld().removeObject(this);
}
mole2003 mole2003

2017/8/22

#
thank you worked perfectly!!!!!!!!!!!!!!!
mole2003 mole2003

2017/8/22

#
however, after a while an error message popped up: java.lang.IllegalStateException: Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed. at greenfoot.Actor.failIfNotInWorld(Actor.java:711) at greenfoot.Actor.isTouching(Actor.java:972) at bullet.act(bullet.java:27) at greenfoot.core.Simulation.actActor(Simulation.java:604) at greenfoot.core.Simulation.runOneLoop(Simulation.java:562) at greenfoot.core.Simulation.runContent(Simulation.java:221) at greenfoot.core.Simulation.run(Simulation.java:211) java.lang.NullPointerException at bullet.act(bullet.java:33) at greenfoot.core.Simulation.actActor(Simulation.java:604) at greenfoot.core.Simulation.runOneLoop(Simulation.java:562) at greenfoot.core.Simulation.runContent(Simulation.java:221) at greenfoot.core.Simulation.run(Simulation.java:211) java.lang.NullPointerException at bullet.act(bullet.java:33) at greenfoot.core.Simulation.actActor(Simulation.java:604) at greenfoot.core.Simulation.runOneLoop(Simulation.java:562) at greenfoot.core.Simulation.runContent(Simulation.java:221) at greenfoot.core.Simulation.run(Simulation.java:211) any ideas?
Super_Hippo Super_Hippo

2017/8/22

#
Make sure you do not execute the code after the object was removed from the world. So if there are more than one possibility that the object is removed, you have to exit the act method so the other one is not executed. (If there is only one possibility, you can just write it at the end of the act method so no other code which requires it to be in the world is executed after that.) In other world, if you remove an object from the world from its act method, the rest of the act method will be executed for the last time if you don't exit it. A method like 'isTouching' uses the world in which the object is to compare its coordinates and size with all other objects in the same world. If it isn't in a world, it gives you this error message.
mole2003 mole2003

2017/9/5

#
So how would I fix this? (forgive the delay I have recently had a 2 week summer holiday in Mexico) Rocket code:
public class rocket extends Actor
{
    /**
     * Act - do whatever the rocket wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
       if (bullettimer > 0)
      {
        bullettimer = bullettimer - 1;  
      }
        if (Greenfoot.isKeyDown("space") && bullettimer == 0)
        {
           fire(); 
        }
      if (isTouching(alienbullet.class))
      {
         hit(); 
      }
       arrowkeys(); 
    }
    private int bullettimer = 0;
    public void fire()
    {
      getWorld().addObject( new bullet(), getX(), getY());
      bullettimer = bullettimer + 10;
    }
    public void arrowkeys()
    {
      if (Greenfoot.isKeyDown("right"))
      {
          setRotation(0);
          move(3);
      }
      if (Greenfoot.isKeyDown("left"))
      {
          setRotation(180);
          move(3);  
      }
       }
    public void hit()
    {
      Greenfoot.setWorld( new lose());  
    }
}
alien code
public class alien extends Actor
{
    /**
     * Act - do whatever the alien wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
      movement(); 
      
      if (Greenfoot.getRandomNumber(1000) < 2)
      {
          fire();
      }
      
    }
    
    public void movement()
    {
       if (Greenfoot.getRandomNumber(100) >= 50)
       {
          setRotation(180);
          move(3);
       }
       else
       {
         setRotation(0);
         move(3);
       }
       
    }
    
    public void fire()
    {
      getWorld().addObject( new alienbullet(), getX(), getY());  
    }
}
world code
public class MyWorld extends World
{

    private int score = 0;
    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    public MyWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(1000, 700, 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()
    {
        alien alien = new alien();
        addObject(alien,66,71);
        alien.setLocation(66,70);
        alien.setLocation(75,71);
        alien alien2 = new alien();
        addObject(alien2,223,71);
        alien2.setLocation(221,62);
        alien2.setLocation(214,69);
        alien alien3 = new alien();
        addObject(alien3,376,71);
        alien3.setLocation(379,67);
        alien alien4 = new alien();
        addObject(alien4,558,71);
        alien4.setLocation(558,71);
        alien alien5 = new alien();
        addObject(alien5,709,71);
        alien5.setLocation(707,72);
        alien alien6 = new alien();
        addObject(alien6,888,71);
        alien6.setLocation(894,73);
        alien alien7 = new alien();
        addObject(alien7,77,204);
        alien7.setLocation(77,178);
        alien alien8 = new alien();
        addObject(alien8,219,175);
        alien8.setLocation(216,180);
        alien alien9 = new alien();
        addObject(alien9,373,184);
        alien9.setLocation(379,178);
        alien alien10 = new alien();
        addObject(alien10,559,176);
        alien10.setLocation(557,178);
        alien alien11 = new alien();
        addObject(alien11,699,187);
        alien11.setLocation(699,180);
        alien alien12 = new alien();
        addObject(alien12,887,179);
        alien12.setLocation(893,182);
        alien alien13 = new alien();
        addObject(alien13,86,311);
        alien13.setLocation(80,300);
        alien alien14 = new alien();
        addObject(alien14,213,305);
        alien14.setLocation(219,303);
        alien14.setLocation(219,302);
        alien alien15 = new alien();
        addObject(alien15,383,309);
        alien15.setLocation(372,299);
        alien alien16 = new alien();
        addObject(alien16,546,314);
        alien16.setLocation(548,302);
        alien alien17 = new alien();
        addObject(alien17,706,307);
        alien17.setLocation(695,307);
        alien alien18 = new alien();
        addObject(alien18,881,298);
        alien18.setLocation(897,305);
        rocket rocket = new rocket();
        addObject(rocket,460,616);
        rocket.setLocation(448,614);

        base1 base1 = new base1();
        addObject(base1,174,534);
        base1.setLocation(162,523);
        base1.setLocation(158,522);
        base2 base2 = new base2();
        addObject(base2,380,529);
        base2.setLocation(374,518);
        base3 base3 = new base3();
        addObject(base3,577,517);
        base3.setLocation(590,520);
        base3.setLocation(591,518);
        base4 base4 = new base4();
        addObject(base4,767,522);
        base4.setLocation(772,522);
    }
    public void addScore(int amount)
    {
       score += amount;
 
    }
    public int getScore()
    {
       return score;
    }
    
}
Super_Hippo Super_Hippo

2017/9/5

#
In the rocket class, move the 'arrowkeys();' to the begging of the act method.
danpost danpost

2017/9/5

#
mole2003 wrote...
however, after a while an error message popped up: java.lang.IllegalStateException: Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed. at greenfoot.Actor.failIfNotInWorld(Actor.java:711) at greenfoot.Actor.isTouching(Actor.java:972) at bullet.act(bullet.java:27) at greenfoot.core.Simulation.actActor(Simulation.java:604) at greenfoot.core.Simulation.runOneLoop(Simulation.java:562) at greenfoot.core.Simulation.runContent(Simulation.java:221) at greenfoot.core.Simulation.run(Simulation.java:211) java.lang.NullPointerException at bullet.act(bullet.java:33) at greenfoot.core.Simulation.actActor(Simulation.java:604) at greenfoot.core.Simulation.runOneLoop(Simulation.java:562) at greenfoot.core.Simulation.runContent(Simulation.java:221) at greenfoot.core.Simulation.run(Simulation.java:211) java.lang.NullPointerException at bullet.act(bullet.java:33) at greenfoot.core.Simulation.actActor(Simulation.java:604) at greenfoot.core.Simulation.runOneLoop(Simulation.java:562) at greenfoot.core.Simulation.runContent(Simulation.java:221) at greenfoot.core.Simulation.run(Simulation.java:211) any ideas?
All indications show the error(s) are located in your bullet class (which is not given).
You need to login to post a reply.