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

2017/2/12

Help with MouseClicked, emergency

1
2
HardToFindAName HardToFindAName

2017/2/12

#
Hello everyone, I'm in a total rush, please help me. I am trying to write an if statement to check if another object was clicked, so that an object could do something.
if (Greenfoot.mouseClicked(the other object)
{
// do something
}
I don't know how to write that "the other object" part. I mean, how could I tell the program to check if that specific object was clicked, that's what I'm saying. I've read something that I should write a reference, if you could help and explain me how to do this, I'd be forever grateful! Thank you in advance.
Super_Hippo Super_Hippo

2017/2/12

#
What you are trying to do? Do you have 2 actors and one wants to know if the other is clicked? You can save a reference to both actors in your world class. Then you need a getter method so the actors can use it. If you need it a lot of times, then the actor could also have a reference to the other actor. Maybe you could also check the mouse click on the other actor in its class code and only if there is a click, you get a reference to the other actor.
HardToFindAName HardToFindAName

2017/2/12

#
Super_Hippo wrote...
What you are trying to do? Do you have 2 actors and one wants to know if the other is clicked? You can save a reference to both actors in your world class. Then you need a getter method so the actors can use it. If you need it a lot of times, then the actor could also have a reference to the other actor. Maybe you could also check the mouse click on the other actor in its class code and only if there is a click, you get a reference to the other actor.
Yes, that's exactly what I'm trying to do. Could you give me an example of how I should write that reference please? I've seen a tutorial but it is rather confusing. Thanks a lot!
Super_Hippo Super_Hippo

2017/2/12

#
Well, I described three ways. I thought you would tell me what exactly you need ;)
HardToFindAName HardToFindAName

2017/2/12

#
Super_Hippo wrote...
Well, I described three ways. I thought you would tell me what exactly you need ;)
Writing a reference in the world class and a function so that the other object could use it.
Super_Hippo Super_Hippo

2017/2/12

#
//instance fields
private Actor actor1=new ClassName1(), actor2=new ClassName2();

//when adding the actors to the world
addObject(actor1, 100, 100);
addObject(actor2, 200, 200);

//getter methods
public Actor getActor1() {return actor1;}
public Actor getActor2() {return actor2;}
Then for actor1 to get a mouse click on actor 2:
Actor actor2 = ((WorldName) getWorld()).getActor2();
if (actor2 != null && Greenfoot.mouseClicked(actor2))
{
    //do something
}
HardToFindAName HardToFindAName

2017/2/12

#
Super_Hippo wrote...
//instance fields
private Actor actor1=new ClassName1(), actor2=new ClassName2();

//when adding the actors to the world
addObject(actor1, 100, 100);
addObject(actor2, 200, 200);

//getter methods
public Actor getActor1() {return actor1;}
public Actor getActor2() {return actor2;}
Then for actor1 to get a mouse click on actor 2:
Actor actor2 = ((WorldName) getWorld()).getActor2();
if (actor2 != null && Greenfoot.mouseClicked(actor2))
{
    //do something
}
Thank you, one more question, where should I wrote private Actor actor1=new ClassName1(), actor2=new ClassName2(); this part, in the constructor of the world or in the prepare method?
Super_Hippo Super_Hippo

2017/2/12

#
Outside of all methods at the beginning of the class code.
HardToFindAName HardToFindAName

2017/2/12

#
Super_Hippo wrote...
Outside of all methods at the beginning of the class code.
I think I've done something wrong, please correct me.
private Actor actor1=new florida();
then in the michigan class code editor
Actor michigan = ((Harta)getWorld()).getActor1();
        if (michigan!=null && Greenfoot.mouseClicked(michigan))
        {
           setImage(new GreenfootImage("nevada.png"));
        }
If I press compile, it somehow freezes. It doesn't show "file saved" neither "class compiled" and when I run it it's not working.
HardToFindAName HardToFindAName

2017/2/12

#
The function is in place, I just copy-pasted it from you. :)
HardToFindAName HardToFindAName

2017/2/12

#
I have restarted the project, it compiles now without any errors but it's not working.
Super_Hippo Super_Hippo

2017/2/12

#
Yeah, I also notice that the auto-compile function is not always working. Restarting helps then. Did you also add actor1 to the world like described above? If yes, insert the following line after the 'Actor michigan...' line. (I hope this is in the act method btw.)
System.out.println(michigan);
It should not print "null".
HardToFindAName HardToFindAName

2017/2/12

#
Ohh, I got it. I did not add the addObject statement because it already was in the prepare statement. It works if I add it
HardToFindAName HardToFindAName

2017/2/12

#
There is another problem, the object is added at a specified place, and it remains there when you click run. If I drag it in the right place, it stays there, but when I click reset it returns. What should I do in this case? By the way, thank you a lot!
HardToFindAName HardToFindAName

2017/2/12

#
And on a side note, what method should I write to get a variable from that class?
There are more replies on the next page.
1
2