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

2014/3/12

Shoot Score

pr2alede pr2alede

2014/3/12

#
I've created a game where a sprite shoots Ants. If the Ants collide with the Aliens in the game then the Aliens disappear. What I need to do now is access a class score and increase it by one. If the score reaches 2 then the game goes to the next level. Code below. Thanks
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * A bullet to be shot at asteroids.
 * 
 * The Shot moves to the top of the screen and then expires. If it hits an asteroid
 * on the way, it destroys the asteroid, and then expires immediately.
 */
public class Ant  extends Animal
{
    private Hero myHero;
    public Score score;

    /**
     * Constructor for a Shot. You must specify the ship the shot comes from.
     */
    public Ant(Hero myHero)
    {
      this.myHero = myHero;
    }

    /**
     * Act - do whatever the Shot wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        int ypos = getY();
        if (ypos > 0) {
            ypos = ypos - 10;
            setLocation(getX(), ypos);
            Actor rock = getOneIntersectingObject(Alien1.class);
            Actor rock2 = getOneIntersectingObject(Alien2.class);
            if (rock != null) {
                // We've hit an asteroid!

                getWorld().removeObject(rock);
                getWorld().removeObject(this);
            }
            if (rock2 != null) {
                // We've hit an asteroid!

                getWorld().removeObject(rock2);
                getWorld().removeObject(this);
            }
        }
        else {
            // I reached the top of the screen
            getWorld().removeObject(this);
        }
    }
}
danpost danpost

2014/3/12

#
Do you not think it might be important to include the code for your Score class also (since that is where the field is located)?
You need to login to post a reply.