Thursday 15 July 2010

Java Click on a object and output a message -


IM is currently making a game in my spare time to improve my Java but from one of the IMs There is a problem that I want to do. Ive made the game so that I can keep one item in a certain position. In this example it is a tower in a fixed location. When this tower is placed at this place and I click on the tower, I should get this message. Although it tried several ways to do this and I was able to find a solution. I have code for this:

  public static zero click (int x, int y) {System.out.println ("X" + X + "Y" "+ Y"); If (screen.release.blow [x] [y] .airid == Value.tower) {System.out.println ("tower clicked"); }}   

When a tower is placed in a location, I am using it for:

  if (holdsItem) {for (int y = 0; Y & lt; Screen.room.block.length; y ++) {for (int x = 0; x & lt; Screen.room.block [0]; length; x ++) {if (Screen .room.block [y] [x]. (Screen room blocks [or] [x] .groundID! = Value.ground & Screen.room.block [y] [x] .airid == Value Tower ) When I put a tower below:  
 < Code> System.out.println (screen.root.block [y] [x]];   

find me

  block [x = 243 , Y = 260, width = 52, height = 52]   

It works fine and the tower is kept then I wanted to use the place which was placed in the tower and then if that place is pressed The message will be printed in the console though it does not happen that anyone can help me fix this problem.

I use a mouse listener that:

  Public Zero Mousechat (MouseEvent E) {E.T.T.A. E.getY (); Block Click (E.A.T.A.T.X), E.J.A.T.E. ()); }   

When I click on a location, I get:

  System.out.println ("X" + X + " Y "+ Y); X257 wi 298   

If it helps.

You are currently using pixels to get your object's position, Must have to check more than the exact "basic" pixels.

  public static zero click (int x, int y) {System.out.println ("X" + X + "Y" + Y); // To view each block in the matrix (int i = 0; i & lt; screen.block.label; i ++) {for (int j = 0; j and lefthinum; screen.boom.bloc [0]; Length; J ++) {If (Screen.room.block [i] [j] .airID == Value.tower) // // If we press within the block's limit then if (x & Gt; = i & amp; amp; amp; & amp; x & lt; = i + blockwidth & y> = j & amp; y and lt; = j + blockHeight) system.out.println ("tower click Done "); Return; One common practice is to store your objects in some type of catalog or uni-dimensional array, so that you can avoid  
 . For (int i = 0; i   

and just for

  (int i = 0; i & lt; screen.release.blog.lamp; i ++) {  

For each block to be checked and to avoid having such large matrix, you have to check each block.

Another approach: You can place each block in a checker-like matrix and then mouse-clicked event coordinate "block" coordinates with pixels:

  public Zero mouse clicked (mouse) e. {Int x = e.getX () / widthOfABlockInPixels; Int y = e.getY () / heightoffblock infix; Block Click (X, Y); }   

Say your object is 100x100 pixels, then it will give you

  (160, 150) = & gt; To give correct block coordinates. (1, 1) (60, 50) = & gt; (0, 0)   

and so on.

No comments:

Post a Comment