I am working on a dance dance revolution type game for a school project and I have randomly spawning objects that you have to press keys in a given area to change the color of the object coming by. For example, if you tap the up key closer to the given area it will turn green and you receive X amount of points. The problem I am having is that when I tap the arrow key to try to match the object with the target area, if there are multiple objects of that same type it will turn both objects a certain color based off where they are in the world. I would like some help to only change the object that is closest to the target area - or in other words that object type that has the smallest Y coordinate.
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class UpPaw here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class UpPaw extends Paw
{
private int yDirection = -2;
private int xPos,yPos;
/**
* Act - do whatever the UpPaw wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public UpPaw()
{
getImage().scale(50,50);
}
public void act()
{
super.act();
turnBlue();
turnGreen();
turnYellow();
turnRed();
}
protected void addedToWorld(World world) //update position
{
xPos = getX();
yPos = getY();
}
public void turnBlue()
{
GreenfootImage upPawBlue = new GreenfootImage("UpPawBlue.png");
upPawBlue.scale(50,50);
if(getX() <= 125 && getY() < 300 && getY() > 200 )
if(Greenfoot.isKeyDown("Up"))
{
setImage(upPawBlue);
}
}
public void turnGreen()
{
GreenfootImage upPawGreen = new GreenfootImage("UpPawGreen.png");
upPawGreen.scale(50,50);
if(getX() == 125 && getY() <= 50 && getY() > 20)
if(Greenfoot.isKeyDown("Up"))
setImage(upPawGreen);
}
public void turnYellow()
{
GreenfootImage upPawYellow = new GreenfootImage("UpPawYellow.png");
upPawYellow.scale(50,50);
if(getX() == 125 && getY() <= 199 && getY() >= 70)
if(Greenfoot.isKeyDown("Up"))
setImage(upPawYellow);
}
public void turnRed()
{
GreenfootImage upPawRed = new GreenfootImage("UpPawRed.png");
upPawRed.scale(50,50);
if(getX() == 125 && getY() > 300 )
if(Greenfoot.isKeyDown("Up"))
setImage(upPawRed);
}
}import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Game here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Game extends World
{
private int leftPawSpawnTimer;
private int rightPawSpawnTimer;
private int upPawSpawnTimer;
private int downPawSpawnTimer;
/**
* Constructor for objects of class Game.
*
*/
public Game()
{
// Create a new world with 600x400 cells with a cell size of 1x1 pixels.
super(600, 400, 1);
//addObject(new Pause(), 530, 1);
//addObject(new LeftPaw(), 45, 500); //those are the correct coordinates for LeftPaw
//addObject(new RightPaw(), 293, 700);
//addObject(new UpPaw(), 125, 500);
//addObject(new DownPaw(), 208, 500);
}
public void act()
{
leftPawSpawnTimer();
rightPawSpawnTimer();
upPawSpawnTimer();
downPawSpawnTimer();
}
public void leftPawSpawnTimer()
{
leftPawSpawnTimer = (leftPawSpawnTimer+1)%100;
if (leftPawSpawnTimer == 0)
leftPawSpawn();
}
public void rightPawSpawnTimer()
{
rightPawSpawnTimer = (rightPawSpawnTimer+1)%350;
if(rightPawSpawnTimer == 0)
rightPawSpawn();
}
public void upPawSpawnTimer()
{
upPawSpawnTimer = (upPawSpawnTimer+1)%275;
if(upPawSpawnTimer == 0)
upPawSpawn();
}
public void downPawSpawnTimer()
{
downPawSpawnTimer = (downPawSpawnTimer+1)%310;
if(downPawSpawnTimer == 0)
downPawSpawn();
}
public void leftPawSpawn()
{
addObject(new LeftPaw(), 45, 500);
}
public void rightPawSpawn()
{
addObject( new RightPaw(), 293, 500);
}
public void upPawSpawn()
{
addObject(new UpPaw(), 125, 500);
}
public void downPawSpawn()
{
addObject(new DownPaw(), 208, 500);
}
}
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Paws here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Paw extends Actor
{
private int yDirection = -2;
/**
* Act - do whatever the Paws wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public Paw()
{
getImage().scale(115,100);
}
public void act()
{
setLocation(getX(), getY()+yDirection);
//remove();
}
public void remove()
{
if(this.getY() < 10)
getWorld().removeObjects(getWorld().getObjects(Paw.class));
}
}

