Java-Gaming.org Hi !
Featured games (81)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (119)
games submitted by our members
Games in WIP (576)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  mouse problem  (Read 1480 times)
0 Members and 1 Guest are viewing this topic.
Offline BlindMan

Senior Newbie





« Posted 2006-04-24 20:08:29 »

hi, i have created a game, which works except for the mouse,

basically I want the user to be able to click on a tile or location on my screen so I can add functionallity to my game.

my game world is held inside a 2d array of integers, who's values corrispond to what 20x20 tile image will be printed to the screen.

the following shows a small part of how an image is printed to the screen, if the integer at the current place in the 2d array equals it.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
for (int i = 0; i < 28; i++) {
         for (int j = 0; j < 38; j++) {
           
           
           
            Junctions.updateLights(i, j);
           
            if(gridArray[i][j] == 1) {
                                       // k and l == 20, so the spaces in the area are drawn 20x20 pixels apart.
               g.drawImage(cacheImages.getImage("straight1.gif"), k * j, l * i ,this);

                   }
}
            }



I've added mouse listener, but despite trying, my current progress is only to retrieve the current mouses position:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
public void mouseClicked(MouseEvent e) {   
     
       
      mouseX = e.getX();
      mouseY = e.getY();

   }
   public void mouseEntered(MouseEvent e) {
     
   }

   public void mouseExited(MouseEvent e) {
     
   }

   public void mousePressed(MouseEvent e)
     
   }
   public void mouseReleased(MouseEvent e) {

     
   }


any help on how i could detect whether a part of the screen, preferably a place in the array or a tile has been clicked

thanks
Offline Kova

Senior Duke





« Reply #1 - Posted 2006-04-24 20:21:12 »

what I can think of now is creating a Rectangles as you create images with same coordinates and dimensions. Later you can test if mouse click (x,y) are on specific rectangles by calling some_rect.contains(x,y). contains() actually works for all Shapes
Offline BlindMan

Senior Newbie





« Reply #2 - Posted 2006-04-24 20:32:37 »

so before each image is created create a rectangle like:

Shape rect1 = new Rectangle2D.Float(k * j, l * i, 20, 20);

?

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline BlindMan

Senior Newbie





« Reply #3 - Posted 2006-04-24 20:34:26 »

as i only want certain images for this to be applicable to
Offline Kova

Senior Duke





« Reply #4 - Posted 2006-04-24 22:27:50 »

yes, but the problem is at some point you'll need to loop through all the rectangles and test if mouse click is on them, so I guess you'll need put those rectangles you create in array(list).
Offline rdcarvallo

Senior Duke


Projects: 5
Exp: 15 years


2D Java games forever!


« Reply #5 - Posted 2006-04-25 16:09:18 »

Hi,
  From your code I can say that your grid is homogeneous, and without rectangles overlapping.
  So you can find which rectangle has been clicked without checking all of them
With this code:
1  
2  
3  
4  
5  
int mouseI = mouseX%k;
int mouseJ = mouseY%l;
if(gridArray[mouseI][mouseJ] == 1){
  //Do your work here
}

You can get the exact tile where the mouse was clicked/pressed.

   Rafael.-
Offline Kova

Senior Duke





« Reply #6 - Posted 2006-04-25 16:16:38 »

nice rdcarvallo
sorry blindman I didn't pay attention, I just saw you are drawing pics and need to detect clicks on them Smiley Of course if you know and have stored exactly where pics are drawn then you don't have to check them all, just the one with that coords (and you know how to reach it).
Offline oNyx

JGO Coder


Medals: 2


pixels! :x


« Reply #7 - Posted 2006-04-25 17:15:01 »

Modulo? Smiley

mx=(mousex)/CELL_SIZE;
my=(mousey)/CELL_SIZE;

if(map[mx][my]==whatever){...}

弾幕 ☆ @mahonnaiseblog
Offline rdcarvallo

Senior Duke


Projects: 5
Exp: 15 years


2D Java games forever!


« Reply #8 - Posted 2006-04-25 20:43:28 »

Modulo? Smiley

mx=(mousex)/CELL_SIZE;
my=(mousey)/CELL_SIZE;

if(map[mx][my]==whatever){...}

Right!! It's integer division, not modulo. (Just a typo  Embarrassed)

   Rafael.-

Offline BlindMan

Senior Newbie





« Reply #9 - Posted 2006-04-25 21:40:08 »

right, awsome, thanks

I will try and implement it, and will tell you if im successful.

sorry, even though Im generally quite alright at programming, I have never programmed a gui type program/game, before, so am a 'newless clubies' in this respect
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline BlindMan

Senior Newbie





« Reply #10 - Posted 2006-04-27 23:32:11 »

Modulo? Smiley

mx=(mousex)/CELL_SIZE(or 20 which is my tile size);
my=(mousey)/CELL_SIZE ;

if(map[mx][my]==1){
//do stuff
}

sorry for sounding thick, but I'm having trouble of where to put this, do i put it in the 'public void mouseClicked(MouseEvent e) ' or in the array loop that is showed in the first chunck of code i pasted at the top.


thanks for helping so far though Smiley
Offline Kova

Senior Duke





« Reply #11 - Posted 2006-04-28 10:40:12 »

you're missing some essential knowlage in programming (logic). You should put it where you need it to be. Ask yourself what do you need and then imagine in what should be done to acomplish this, where is the beggining, what should follow and how should it end. I will try to awser to your question now, but you won't learn anything if we do the logic for you.

It seems you need something done with tile when you click on it. You should put it then in mouseClicked or MousePressed where you determine what tile has been clicked on and then do something with it...
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Longarmx (41 views)
2014-10-17 03:59:02

Norakomi (33 views)
2014-10-16 15:22:06

Norakomi (26 views)
2014-10-16 15:20:20

lcass (30 views)
2014-10-15 16:18:58

TehJavaDev (59 views)
2014-10-14 00:39:48

TehJavaDev (60 views)
2014-10-14 00:35:47

TehJavaDev (50 views)
2014-10-14 00:32:37

BurntPizza (66 views)
2014-10-11 23:24:42

BurntPizza (38 views)
2014-10-11 23:10:45

BurntPizza (80 views)
2014-10-11 22:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!