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

2013/11/23

Mouse click over certain area?

1
2
Yedya Yedya

2013/11/23

#
So I want an action to occur when my mouse is over a certain location ,the on button. http://i40.tinypic.com/2ugnexk.jpg
bourne bourne

2013/11/23

#
Something like this: (where the bounds are ints defining the area of interest)
MouseInfo mouse = Greenfoot.getMouseInfo();
if (Greenfoot.mouseClicked(null))
{
    if (mouse.getX() >= leftBound &&
        mouse.getX() <= rightBound &&
        mouse.getY() >= topBound &&
        mouse.getY() <= bottomBound)
    {
        // Do something
    }
}
bourne bourne

2013/11/23

#
Of course, if the area of interest is defined via an Actor's image, then you can simply just use something like this: (If the context is right considering the draw order of Actors)
if (Greenfoot.mouseClicked(anActor))
{
    // Do something
}
Yedya Yedya

2013/11/26

#
thanks,but now the image is appearing in a different location.It's because Im changing the image from a different class.
danpost danpost

2013/11/26

#
Please show the method in which you are changing the image of that actor from a different class.
Yedya Yedya

2013/11/26

#
   public void act() 
    {
       if(Greenfoot.mouseClicked(this))
       {
        play();  
        change();
         
       } 
    }    
    public void play()
    {
        Greenfoot.playSound("G#2.mp3");
        
    }  
    public void change ()
        {
    setImage("OFFFFS.png");
    {
     setImage("RLargeHall.png");
    }   
         }
}
danpost danpost

2013/11/27

#
I do not see any place in the code given where you are referring to any other object. I presume from what you previously stated that you are trying to change the image of another object in the 'change' method, but it does not refer to any other object. You need to be more clear as to what you want (a detailed explanation of what you are trying to accomplish).
Yedya Yedya

2013/11/27

#
 public void change ()  
     {  
 setImage("OFFFFS.png");  
 {  
  setImage("RLargeHall.png");  
 }     
      }  
When an picture is clicked,I want to change another picture.But when I do it using the code above the image appears in different location.I want the image to stay in the same place.
danpost danpost

2013/11/27

#
The only image you are going to change with the code above is the image of the actor that was clicked on. You need a reference to the other actor whose image you wish to change before you can change that one.
Yedya Yedya

2013/11/27

#
So how do a reference an actor instead of an image?
danpost danpost

2013/11/27

#
One would need to know the class of that actor and which object of that class you wish to change the image of.
Yedya Yedya

2013/11/28

#
Ok so instead of changing the image,i need to change the actor? Can you give me an example of this code?
danpost danpost

2013/11/29

#
No. You need the actor to get the image of that actor so you can change its image ('actorReferenceName.setImage("imageFileName.png")').
Yedya Yedya

2013/12/4

#
Was away,thanks will try now
Yedya Yedya

2013/12/4

#
Throwing out an error,not a statment
There are more replies on the next page.
1
2