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

2019/1/17

How do you detect a click in an area on the world

TobyRoby TobyRoby

2019/1/17

#
Hey guys below is my code that i am using to determine if when the program is running a click is within a certain area of the world. I have an object array that needs to have one of the objects .totalPebble set to zero when it is clicked in the area. I used System.out.println("Mouse clicked") to test if it is detecting the click and it doesnt seem to be outputting this could someone please help me, im not sure if it is the null thing or something else. Thanks in advance
int x = 0;
int y = 0;
        
        MouseInfo mouse = Greenfoot.getMouseInfo();
        if (mouse != null)
        {
            x = mouse.getX();
            y = mouse.getY();
        }

if (Greenfoot.mouseClicked(null))
        {
            System.out.println("Mouse clicked");
            System.out.println("X: " + x + "Y: " + y);
            if (((x > 10) && (x < 220)) && ((y > 0) && (y < 270)))//min and max should be the edges of the area;
            {
                System.out.println("Dip 1 clicked");
                for ( int i=0; i<dipArray[0].totalPebble; i++)
                {
                    if (i>dipArray.length)  //the constant that is controlling the object array is set
                    {           //to zero when it is above array length, as there are no more objects
                        i=0;
                    }
                    dipArray[i].totalPebble=dipArray[i].totalPebble+1;      //add one pebble to following dips
                }
                dipArray[0].totalPebble=0;  //set the clicked dip pebble value to 0
            }
            dipArray[0].updatePebble();
        }
danpost danpost

2019/1/17

#
TobyRoby wrote...
Hey guys below is my code that i am using to determine if when the program is running a click is within a certain area of the world. I have an object array that needs to have one of the objects .totalPebble set to zero when it is clicked in the area. I used System.out.println("Mouse clicked") to test if it is detecting the click and it doesnt seem to be outputting this could someone please help me, im not sure if it is the null thing or something else. << Code Omitted >>
First thing that needs to be asked is -- where in your class did you place the given code?
TobyRoby TobyRoby

2019/1/18

#
hey danpost, the code shown is at the bottom of my world-class, below initialising the objects. The only thing in my dip class is 'public int totalPebble = 0;'. I just need to know how to get a mouse click on the world in a certain area, thanks.
danpost danpost

2019/1/18

#
TobyRoby wrote...
hey danpost, the code shown is at the bottom of my world-class, below initialising the objects. The only thing in my dip class is 'public int totalPebble = 0;'. I just need to know how to get a mouse click on the world in a certain area, thanks.
In what method is the code shown placed? Please show entire world class code for context.
TobyRoby TobyRoby

2019/1/18

#
*also I've only been coding on greenfoot for a month or so, so sorry for explaining basics
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class MyWorld extends World
{

    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    public MyWorld()
    {    
        super(700, 400, 1); 
        
        int totalPebble = 0;
        
        mancalaBoard mancalaBoard = new mancalaBoard();
        addObject(mancalaBoard,350,200);
        
        instructionsPage instructionsPage = new instructionsPage();
        addObject(instructionsPage, 650,350);
        
        Dip[] dipArray = new Dip[14];
        
        System.out.println("Making dips");  //this is to check if the code is running
        dipArray[0]=new Dip();  //these make the dips in the array positions and
        addObject(dipArray[0],208,259);     //decide their locations on the board
        dipArray[1]=new Dip();
        addObject(dipArray[1],269,263);
        dipArray[2]=new Dip();
        addObject(dipArray[2],328,259);
        dipArray[3]=new Dip();
        addObject(dipArray[3],388,260);
        dipArray[4]=new Dip();
        addObject(dipArray[4],441,260);
        dipArray[5]=new Dip();
        addObject(dipArray[5],506,262);
        dipArray[6]=new Dip();
        addObject(dipArray[6],554,189);
        dipArray[7]=new Dip();
        addObject(dipArray[7],504,153);
        dipArray[8]=new Dip();
        addObject(dipArray[8],444,157);
        dipArray[9]=new Dip();
        addObject(dipArray[9],381,151);
        dipArray[10]=new Dip();
        addObject(dipArray[10],322,155);
        dipArray[11]=new Dip();
        addObject(dipArray[11],262,153);
        dipArray[12]=new Dip();
        addObject(dipArray[12],204,153);
        dipArray[13]=new Dip();
        addObject(dipArray[13],147,197);
        
        
        
        for ( int i=0; i<dipArray.length; i++)
        {
            dipArray[i].totalPebble=4;  //this for loop makes all of the dips have a pebble value of 4
        }

        dipArray[6].totalPebble=0;  //these two lines change th value of the 'mancalas' so that they start with a value of 0
        dipArray[13].totalPebble=0;
        
        int x = 0;
        int y = 0;
        
        MouseInfo mouse = Greenfoot.getMouseInfo();
        if (mouse != null)
        {
            x = mouse.getX();
            y = mouse.getY();
        }
        
        
        //fix the if statement below to detect when a user has clicked on the screen
        //if this is working then mouse clicked should be printed
        //
        if (Greenfoot.mouseClicked(null))
        {
            System.out.println("Mouse clicked");
            System.out.println("X: " + x + "Y: " + y);
            if (((x > 10) && (x < 220)) && ((y > 0) && (y < 270)))//min and max should be the edges of the area;
            {
                System.out.println("Dip 1 clicked");
                for ( int i=0; i<dipArray[0].totalPebble; i++)
                {
                    if (i>dipArray.length)  //the constant that is controlling the object array is set
                    {           //to zero when it is above array length, as there are no more objects
                        i=0;
                    }
                    dipArray[i].totalPebble=dipArray[i].totalPebble+1;      //add one pebble to following dips
                }
                dipArray[0].totalPebble=0;  //set the clicked dip pebble value to 0
            }
            dipArray[0].updatePebble();
        }
        
        // if (Greenfoot.isKeyDown("m"))
         // {
             // instructionsPage.addedToWorld(Greenfoot.World());
         // } 
    }
}
danpost danpost

2019/1/18

#
Okay, you have all codes previously given in your world constructor. The constructor is only executed once per world created (before that world becomes active). Your world is created during a reset or after compiling of the code before the your scenario goes into a running state. You need the code initially given to be executed repeatedly during run-time. It needs to be in an act method (which the World class does have). Override it in your MyWorld class and move the given code into it.
TobyRoby TobyRoby

2019/1/19

#
wow, thank you so much it worked, i was stuck on that for so long. The only error im getting now is that the 'dipArray' says cannot find symbol, is there something i need to make public?
if (Greenfoot.mouseClicked(null))
        {
            System.out.println("Mouse clicked");
            System.out.println("X: " + x + "Y: " + y);
            if (((x > 10) && (x < 220)) && ((y > 0) && (y < 270)))//min and max should be the edges of the area;
            {
                System.out.println("Dip 1 clicked");
                for ( int i=0; i<dipArray[0].totalPebble; i++)
                {
                    if (i>dipArray.length)  //the constant that is controlling the object array is set
                    {           //to zero when it is above array length, as there are no more objects
                        i=0;
                    }
                    dipArray[i].totalPebble=dipArray[i].totalPebble+1;      //add one pebble to following dips
                }
                dipArray[0].totalPebble=0;  //set the clicked dip pebble value to 0
            }
            dipArray[0].updatePebble();
        }
danpost danpost

2019/1/19

#
Move line 14 up to line 5. The array, when declared in the constructor is lost once the constructor is done executing. By having it outside any method or constructor, it remains until the World object is discarded.
TobyRoby TobyRoby

2019/1/19

#
do you mean like this? because i am still getting the same error
if (Greenfoot.mouseClicked(null))
        {
            System.out.println("Mouse clicked");
            System.out.println("X: " + x + "Y: " + y);
            dipArray[i].totalPebble=dipArray[i].totalPebble+1;      //add one pebble to following dips
            if (((x > 10) && (x < 220)) && ((y > 0) && (y < 270)))//min and max should be the edges of the area;
            {
                System.out.println("Dip 1 clicked");
                for ( int i=0; i<dipArray[0].totalPebble; i++)
                {
                    if (i>dipArray.length)  //the constant that is controlling the object array is set
                    {           //to zero when it is above array length, as there are no more objects
                        i=0;
                    }
                }
                dipArray[0].totalPebble=0;  //set the clicked dip pebble value to 0
            }
            dipArray[0].updatePebble();
        }
danpost danpost

2019/1/19

#
TobyRoby wrote...
do you mean like this? because i am still getting the same error
No, sorry. I guess I needed to be more specific -- in your MyWorld class code above. Dog-nabbit. I also mean line 22, not 14.
TobyRoby TobyRoby

2019/1/21

#
awesome, thank you very much you beautiful greenfoot user <3
You need to login to post a reply.