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

2022/4/10

Need help with a "Greenfoot.MouseClicked" code

Puffshroom Puffshroom

2022/4/10

#
I am a newbie at programming/Greenfoot, and wanted to create a simple strategy game, where you get to choose where you place your base first. I wanted to create a code which detects where the mouse is and if the mouse is clicked, the base and the character will be placed on that location. I came up with the idea to create a seperate invisible actor which sits at the corner of the map. The seethrough model/texture is so tiny that it is very unlikely that one would accidentaly clicked on it. The code that the actor has is as following:
  int limit;
  int mx;
  int my;
    public void act()
    {
        MouseInfo mouse = 
        Greenfoot.getMouseInfo();
        if(mouse!=null){
         mx = mouse.getX();
         my = mouse.getY();
       }
        
      if(Greenfoot.mouseClicked(MyWorld.class)) 
            {  
             if(limit<1) {
              getWorld().addObject(new Player(), mx, my);
              getWorld().addObject(new Base(), mx, my);
              limit++;
           }
        }
        
  }
When I click the mouse, nothing happens, can someone tell me what is wrong and how to fix it? (Sorry for my english I am not a native speaker)
Puffshroom Puffshroom

2022/4/11

#
Nevermind, I fixed it by putting the code in the world, not create a seperate actor for it.
You need to login to post a reply.