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

2018/4/16

Change "this" in Greenfoot.mouseMoved(this) to an object like bee1 from class Bee

Hi I want to make a program that detects a specific object like bee1 or bee2 instead of "this" in general. Can you guys help me? Thanks in advance! This is my code so far:
public class Bee extends Actor
{
    public void act() 
    {
        if (Greenfoot.mouseMoved(this)) {
            Label.selectedBee = 1;
        }
        if (Greenfoot.mouseMoved(this)) {
            Label.selectedBee = 2;
        }  
        if (Greenfoot.mouseMoved(this)) {
            Label.selectedBee = 3;
        }  
    }    
}
public class Label extends Actor
{
    public static int selectedBee = 0;
    public void act() 
    {
        setImage(new GreenfootImage("Selected bee: bee" + selectedBee, 16, Color.WHITE, Color.BLACK));
    }    
}
I tried this already:
        if (Greenfoot.mouseMoved(Bee.bee1)) {
            Label.selectedBee = 1;
        }
        if (Greenfoot.mouseMoved(Bee.bee2)) {
            Label.selectedBee = 2;
        }  
        if (Greenfoot.mouseMoved(Bee.bee3)) {
            Label.selectedBee = 3;
danpost danpost

2018/4/16

#
Try:
if (Greenfoot.mouseMoved(this))
{
    Label.selectedBee = 1+getWorld().getObjects(Bee.class).getIndex(this);
}
This will get a list of Bee objects in the world and find where that particular bee is in the list. As long as no Bee object is removed from the world, the list should be created the same way every time.
You need to login to post a reply.