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

2018/4/19

Clicking on an actor from another actor

FarEalm FarEalm

2018/4/19

#
I am trying to click on an actor from another one so it will change a variable for it My code is currently
public void click(){
        MyWorld world = (MyWorld) getWorld();
        if(Greenfoot.mouseClicked(one.class)){
            lastAmount = 1;
            System.out.println(lastAmount);
        }else if(Greenfoot.mouseClicked(two.class)){
            lastAmount = 2;
        }else  if(Greenfoot.mouseClicked(three.class)){
            lastAmount = 3;
        }else  if(Greenfoot.mouseClicked(four.class)){
            lastAmount = 4;
        }else  if(Greenfoot.mouseClicked(five.class)){
            lastAmount = 5;
        }else  if(Greenfoot.mouseClicked(six.class)){
            lastAmount = 6;
        }else  if(Greenfoot.mouseClicked(seven.class)){
            lastAmount = 7;
        }else  if(Greenfoot.mouseClicked(eight.class)){
            lastAmount = 8;
        }else  if(Greenfoot.mouseClicked(nine.class)){
            lastAmount = 9;
        }else  if(Greenfoot.mouseClicked(zero.class)){
            lastAmount = 0;
            System.out.println("works");
        } 
    }
Please help me im on a time limit thank you
FarEalm FarEalm

2018/4/19

#
I forgot to mention i have a public double named last amount just so you know.
danpost danpost

2018/4/19

#
You click on an Actor object -- not on an Actor (or World) class. If not null, the parameter must refer to a specific object, not a class. You could use null for the parameter and then determine if it was an Actor object clicked on and what type object it was:
if (Greenfoot.mouseClicked(null))
{
    Actor actor = Greenfoot.getMouseInfo().getActor();
    if (actor != null)
    {
        if (actor instanceof one) lastAmount = 1;
        else if (actor instanceof two) lastAmount = 2;
        else if // etc.
FarEalm FarEalm

2018/4/20

#
Thank you very much my code works as intended now. Thank you or all of the help.
You need to login to post a reply.