• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

MousePressed after KeyPressed

 
Bart Boersma
Ranch Hand
Posts: 79
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

I am currently developing a game. With the KeyPressed function I would like to move my sprite. With the MousePressed function I would like to shoot. However, as soon as I press a key (say 'A') I move left but cant shoot (use MousePressed) function anymore. The other way around (first MousePressed then KeyPressed) does work. How do I fix is so that I can shoot bullets (use MousePressed) while I am moving (using KeyPressed)?

 
Campbell Ritchie
Marshal
Posts: 79402
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't know, but why are you still writing == true?
 
Bart Boersma
Ranch Hand
Posts: 79
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just changed it, thank you.

So it is not possible to do a certain action (move sprite) when KeyPressed and while the sprite is moving invoke the MousePressed method (to, for example, shoot a bullet)?
 
Knute Snortum
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Be sure to include the imports of the program when posting code.

Don't write:

...but instead write:

Prefer the interface as a declared type, and the generic type can be inferred in the initialization since Java 7.
 
Knute Snortum
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Two things wrong with the following method (may be that you didn't copy and paste correctly).  See // -->  comments

 
Knute Snortum
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you post a SSCCE (that's a link)?  
 
Bart Boersma
Ranch Hand
Posts: 79
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes I didnt post everything.

This is the full code:
Main class


JumpMan class


Bullet Class


Renderworld Class
 
Bart Boersma
Ranch Hand
Posts: 79
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is a simplified version but touches the same problem. While I type any key on the keyboard it returns "keypressed". However, if I press a mousebutton while having any key on the keyboard pressed it does not return "mousepressed".



 
Bart Boersma
Ranch Hand
Posts: 79
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The weird thing I just found is:

If I press a key on my keyboard it prints "keypressed" continously. If I press left mouse button while pressing a key on my keyboard it does not print "mousepressed. However, if I press right mouse button while pressing a key on my keyboard it does print "mousepressed".

I am lost why right mouse button works but left mouse button does not. Anyone got an idea?

This is both with my selfmade game and the simplified example.
 
Rob Camick
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

This is a simplified version but touches the same problem.



That is what the SSCCE is, a simplified version (not the full code you posted). We are not interested in all the other code you posted. The point of the SSCCE is to demonstrate the problem (not your application) and should be included with all questions.

Anyway, I tested the SSCCE and I don't have a problem. The mousePressed displays whether I use the right or left mouse button.

Maybe this is a version/platform issue? I'm using JDK8_45 on Windows 7.
 
Bart Boersma
Ranch Hand
Posts: 79
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for checking for me.

The JDK version I am using is:

java version "1.8.0_111"
Java(TM) SE Runtime Environment (build 1.8.0_111-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.111-b14, mixed mode)

So, if I am correct,  JDK version: 1.8.0_111.
 
Bart Boersma
Ranch Hand
Posts: 79
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And on windows 10
 
Knute Snortum
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The SSCCE worked for me.  Windows 10 and Java 1.8.0_121
 
Bart Boersma
Ranch Hand
Posts: 79
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Quick update:

I have sent the code to by brother as well, which works on his computer. Together we figured that it might had to do with the fact that I was using a mousepad (on a laptop). So after plugin in a regular mouse the code worked.

Concluding that it has something to do with the Button1 (left mouse click) on my laptop mousepad. I have not been able to figure out why my mousepad is not working the same as a regular mouse, probably its something software related. Anyway, the code does work with a regular mouse.

Thanks guys for all the feedback and explanation. If anyone knows why the above happens with a mousepad please let me (us) know. For now, I will put this thread to solved.

Thanks again.
 
Dave Tolls
Rancher
Posts: 4801
50
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have noticed with mousepads that some of them (the ones I've used anyway) do seem to have issues like that.
They're rubbish at playing some games.
Minecraft, for example, has exactly the same issue you mention here.  If you're moving (with the keys) you can't (at least with 3 laptops I've tried) hackety hack at the same time (left mouse click).  Which makes it a little...challenging.
 
Paweł Baczyński
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Consider using KeyAdapter instead of KeyListener and MouseAdapter instead of MouseListener.
That way, you wouldn't need so many empty methods.
I can see you have imports but you don't use them.
 
reply
    Bookmark Topic Watch Topic
  • New Topic