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

2018/3/16

How can I determine which actor to change color

Brantgarron Brantgarron

2018/3/16

#
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));
       
    }
    
}
danpost danpost

2018/3/16

#
Sounds like you will need some static content -- a couple of class fields that all Paw object can make use of. One can hold a reference to the Paw with the lowest y-coordinate value and the other can hold that y-coordinate value. Then after moving, a paw can check to see if its y value is less than the last lowest. If so, replace it with its own and set the other field to itself. A paw should also check to see if the Paw reference field is null or, if not, check to see if the paw referenced is still in the world. If either is true, then it should assume itself to have the lowest y-coordinate value. So additions to the Paw class would be:
// fields
protected static Paw activePaw;
private static int activePawY;

// in act, after moving
if (activePaw == null || activePaw.getWorld() == null || getY() < activePawY)
{
    activePaw = this;
    activePawY = getY();
}
Now, you can use:
if (activePaw == this)
as an initial condition before checking for a key to match the paw.
Brantgarron Brantgarron

2018/3/16

#
Where would those two instance data lines go at the top of your response? I get errors when I tried to put them in my Paw (Parent class).
Brantgarron Brantgarron

2018/3/16

#
I figured out my problem to the above reply...but the colors are still changing for both paws rather than the closer one.
Brantgarron Brantgarron

2018/3/16

#
Now the colors are not changing at all for the UpPaw that I initially put the code
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 (activePaw == this)    
        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 (activePaw == this)
        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 (activePaw == this)
        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 (activePaw == this)
        if(Greenfoot.isKeyDown("Up"))
        setImage(upPawRed);
    }
}
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; protected static Paw activePaw; private static int activePawY; /** * 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); if (activePaw == null || activePaw.getWorld() == null || getY() < activePawY) { activePaw = this; activePawY = getY(); } } public void remove() { if(this.getY() < 10) getWorld().removeObjects(getWorld().getObjects(Paw.class)); } }
danpost danpost

2018/3/16

#
Brantgarron wrote...
I figured out my problem to the above reply...but the colors are still changing for both paws rather than the closer one.
danpost wrote...
Now, you can use:
if (activePaw == this)
as an initial condition before checking for a key to match the paw.
Insert the following line in all subclasses of Paw immediately after "super.act();":
if (activePaw != this) return;
and remove the similar lines from the other methods.
danpost danpost

2018/3/16

#
There are a lot of things I would change in your codes. Unfortunately, I currently do not have time to delve what they are and how I would change them. I hope maybe to make a "coding tips tutorial" at some point.
doglover555 doglover555

2018/3/16

#
c'mon Brantgarron
You need to login to post a reply.