0

Basicly I am creating a game that you click on falling objects, E.G cookies, and I need to know how to check and see if a certain cookie has been pressed so it can disappear but the problem is that its in an array.

Here is a bit of my code: Input class...

public class Input implements MouseListener, MouseMotionListener{

@Override
public void mousePressed(MouseEvent e) {
    if(e.getSource().equals(MainGame.CG)){
        if(MainGame.MG.inGame){
            //There is actually something else here but its classified (haha sorry about that)      
            if(e.getPoint().x > /*I NEED SOMETHING HERE*/){
                                    //tells you if the object has been pressed
                MainGame.CG.cookieClicked = true; //CG = ClickerGame
            }

        }
    }
}
}

class with array...

public class ClickerGame extends JPanel{
    public int amount;
    public FallingObject[] fo = new FallingObject[120]; //THE ARRAY I'M HAVING TROUBLES WITH


     /*THE REST IS A SECRET (SORRY ABOUT THAT)*/
}

If you don't understand here is a picture to demonstrate what I need... And sorry about the terrible drawing

  • 1
    Ok, the answer is ** A SECRET, SORRY ABOUT THAT **. In fact the missing code is the one related to your question, so post it (check how to do a http://sscce.org) or delete the question. – SJuan76 Apr 11 '14 at 07:27
  • i reccomend a list. it's more dynamic than an array – Philipp Sander Apr 11 '14 at 07:36

2 Answers2

2

In order to avoid having to check the coordinates of 120 different items on each click, make every element inside FallingObject[] aware of three things:

  1. Its own area of influence (see sn00fy's answer)
  2. The containing class (in this case probably ClickerGame
  3. Its location in the array (an int)

To do this, you would need to change your FallingObject constructor to look something like this:

public void FallingObject(ClickerGame master, int index); //add whatever else is needed for Falling Object.

Then you could instantiate the array as follows.

for(int i = 0; i < 120; i++) {
     fo[i] = new FallingObject(this, i ); //add anything else needed for the constructor
}

Then each FallingObject is responsible for its own state, and when clicked it is able to report back to the ClickerGame instance. All you need now is a method in ClickerGame which each FallingObject can call.

public void clickedObj(int index) {
FallingObject temp = null;
     if(index >= 0 && index < 120) {
          temp = fo[index];
          //Do stuff with temp :)
     }
}

To call this method from within FallingObject just reference the 'master' variable (which you should probably save as a global variable within the class.

1

You have to check every element in your FallingObject[] array if it intersects with the mouse pointer coordinates at the moment of the click.

You can implement a simple rectangle test or use a circle for each cookie as explained here:

Equation for testing if a point is inside a circle

Community
  • 1
  • 1
snOOfy
  • 33
  • 6