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

2012/5/6

Mouse click

Moritz Moritz

2012/5/6

#
Hey, I have a short question: I would like to click on an object with the mouse, after which something happens - for example, I click on an box and then it opens the next level in a game. Can someone help me? Thanks already in advance =)
erdelf erdelf

2012/5/6

#
Use the following in the box.
public void act()
{
     MouseInfo mouse =  Greenfoot.getMouseInfo();  
            if(mouse!=null){  
                button = mouse.getButton();  
                if(button == 1 && Greenfoot.mouseClicked(null)) 
                {
                    if(mouse.getY() = getY() && mouse.getX() = getX())
                    {
                           World nextWorld = new World2();
                           Greenfoot.setWorld(nextWorld); 
                    }
                }
            }
}
Moritz Moritz

2012/5/6

#
thank you - but there´s still a problem - I wrote:
import greenfoot.*;  


public class Menue extends World
{

   
    public Menue()
    {    
        
        super(1000, 600, 1); 
        
            
       button e = new button();
       addObject(e, 700, 500);
    }
    
    public void act() {
              test();
        
    }
    
      
    public void test(){
       
        MouseInfo mouse =  Greenfoot.getMouseInfo(); 
         if(mouse!=null){ 
            button = mouse.getButton();   
            if(a == 1 && Greenfoot.mouseClicked(null))  
            {  
                if(mouse.getY() = getY() && mouse.getX() = getX())  
                { 
                    World nextWorld = new World2();
                    Greenfoot.setWorld(nextWorld);   
                }  
            }  
        }  
    }
}
so the program don´t know "button" - can someone tell me what´s wrong?
erdelf erdelf

2012/5/6

#
sry make this
import greenfoot.*;  


public class Menue extends World
{

public int button;
   
    public Menue()
    {    
        
        super(1000, 600, 1); 
        
            
       button e = new button();
       addObject(e, 700, 500);
    }
    
    public void act() {
              test();
        
    }
    
      
    public void test(){
       
        MouseInfo mouse =  Greenfoot.getMouseInfo(); 
         if(mouse!=null){ 
            button = mouse.getButton();   
            if(a == 1 && Greenfoot.mouseClicked(null))  
            {  
                if(mouse.getY() = getY() && mouse.getX() = getX())  
                { 
                    World nextWorld = new World2();
                    Greenfoot.setWorld(nextWorld);   
                }  
            }  
        }  
    }
}
davmac davmac

2012/5/6

#
erdelf, did you try compiling your suggested code? line 29, there is no variable called 'button', you need to declare the variable with a type (you can do this on the same line). line 30, there is no variable called 'a', perhaps you meant to use 'button'. Moritz, on line 14, it seems to refer to a class 'button' in your scenario - do you have such a class? (if you do, Java convention would be to name it with a capital, ie. Button vs button).
erdelf erdelf

2012/5/7

#
davmac, look in line 7 I used his code, didn't look at it anymore
davmac davmac

2012/5/7

#
erdelf wrote...
davmac, look in line 7 I used his code, didn't look at it anymore
Right, I missed that (it's confusing that there seems to be both a class and a variable called 'button'). Line 30 is wrong though, as I said.
Moritz Moritz

2012/5/7

#
Yes I have a class called "button" - but when I compile, the program mark:
if(mouse.getY() = getY() && mouse.getX() = getX())  
the () behind getY is marked - it is called "unexpected type - required:variablefound : value" I don´t know what´s wrong... I wrote "button" inside the () - but then the getY is marked. so at this time I wrote:
import greenfoot.*;  


public class Menue extends World
{
    public int button;
   
    public Menue()
    {    
        
        super(1000, 600, 1); 
        
             
       button e = new button();
       addObject(e, 700, 500);
    }
    
    public void act() {
       
       test();
        
    }
    
   
    
    public void test(){
       
        MouseInfo mouse =  Greenfoot.getMouseInfo(); 
         if(mouse!=null){ 
           button = mouse.getButton();   
            if(button == 1 && Greenfoot.mouseClicked(null))  
            {  
                if(mouse.getY() = getY() && mouse.getX() = getX())  
                { 
                    World nextWorld = new World2();
                    Greenfoot.setWorld(nextWorld);   
                }  
            }  
        }  
    }
}
erdelf erdelf

2012/5/7

#
sry, didn't saw that you wrote it into a world, the code needs to be in the Class Button.
Moritz Moritz

2012/5/7

#
ok - so I have to do it below the Actor? Do you think it works this way?
davmac davmac

2012/5/7

#
Also, this code is wrong: if(mouse.getY() = getY() && mouse.getX() = getX()) You need to use '==' to compare values, not '='.
erdelf erdelf

2012/5/7

#
yeah right davmac, I forget this sometimes. In an actor this should work
Moritz Moritz

2012/5/7

#
Thank you =)) - there is no problem - so there is no mistake, but it doesn´t work... when I click on the block nothing happens
You need to login to post a reply.