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

2013/12/5

Avoider Game: method

1
2
w9ndel w9ndel

2013/12/9

#
http://www.greenfoot.org/scenarios/10331
There you go. Hope this works :-)
shrucis1 shrucis1

2013/12/9

#
Alright, so it seems the error is here:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Enemy here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Enemy extends Actor
{
    /**
     * Act - do whatever the Enemy wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        setLocation(getX(), getY() + 1);
        
        Actor avatar;
        avatar = getOneIntersectingObject( Avatar.class );
        {
            AvoiderWorld world = (AvoiderWorld) getWorld();
            world.endGame();
            world.removeObject(avatar);
     
        }
            
    }
    }

You probably want it so that if the enemy is touching the player, the game ends, correct? What you're doing is ending the game as soon as a enemy appears. Try this code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Enemy here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Enemy extends Actor
{
    /**
     * Act - do whatever the Enemy wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        setLocation(getX(), getY() + 1);
        
        Actor avatar;
        avatar = getOneIntersectingObject( Avatar.class );
        if(avatar != null){
            AvoiderWorld world = (AvoiderWorld) getWorld();
            world.endGame();
            world.removeObject(avatar);
     
        }
            
    }
}

shrucis1 shrucis1

2013/12/9

#
After testing out the game with that code on my computer, it works; however, I noticed that when the enemies hit the bottom of the screen, they don't go away. If you want to change this so they disappear when they hit the bottom, you can include this code at the end of the act method:
if(getY() > getWorld().getHeight()-5) {
    getWorld().removeObject(this);
}
w9ndel w9ndel

2013/12/9

#
Thank you so much shrucis! The game now works perfect :-)
You need to login to post a reply.
1
2