Hey guys. I'm trying to make it so that when my spaceship eats a green dot, another red dot (which is the enemy) appears. I am doing this so that my game gets gradually harder with more and more red dots as you eat food. However, there is an issue. In the editor for my spaceship subclass (which is called "Player"), I have a method for eating green dots. That works fine. I tried to add "world.addObject(enemy);" into the method. I have already defined "world", so that is not an issue. It still doesn't work. Here is my code for the Player subclass:
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Player here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Player extends Actor
{
/**
* Act - do whatever the Player wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
if (Greenfoot.isKeyDown("up"))
{
move(4);
}
if (Greenfoot.isKeyDown("down"))
{
move(-4);
}
if (Greenfoot.isKeyDown("right"))
{
turn(4);
}
if (Greenfoot.isKeyDown("left"))
{
turn(-4);
}
Actor food;
food = getOneObjectAtOffset(0, 0, Food.class);
if (food != null)
{
World world;
world = getWorld();
world.removeObject(food);
world.addObject(enemy);
Greenfoot.playSound("eatfood.wav");
}
}
}
