| ★ wanayoo — archive 1999 http://javaboutique.webdeveloper.com/tutorials/Java_Game_Programming/MausereignisseEng.html | Nouvelle recherche | Portail wanayoo |
![]() | |||||||||||||||||||||||||||
Tutorials : The Java Game Development Tutorial :
Handle mouse eventsI think there is no game that doesn't "interact" with the player. In our case, as a programmer, you have two possibilities to allow the player to interact with the computer: the player has to use the mouse or the keyboard. In the next two chapters I want to tell you how to handle mouse and keyboard events in an applet. Again this is much easier to do in an applet than in a real application programmed for example using Java Swing. Also you won't be able to use the things you will learn here about event handling in an application, because event handling is very different in applications. But every good book about Java tells you how to handle events in applications so I think it will be no problem for you to learn it by yourself if you need to. Now I want to change our "ball movement" applet so that the ball changes the direction if the user clicks on the applet. To do this you have to implement just one method in our Main class called mouseDown. This method should look like this:
public boolean mouseDown (Event e, int x, int y) {
x_speed = - (x_speed); // DON'T FORGET (although not necessary here)!! return true; Of course you can handle other events than the mouseDown event. Java makes it possible to overwrite the following methods the same way as I did above and handle other mouse events that way. Here comes a list of the methods:
With the help of the variables int x and int y you can get the coordinates where the event happened, which is very important for many games. Also you have to take care of the return statement "return true;" that often has no meaning for the functionality of your game but is expected by the method. Sourcecode download Next chapterHandling keyboard events
How to Add Java Applets to Your SiteNew on the Java Boutique:
New Article:
New Tutorial: Elsewhere on internet.com:
WebDeveloper Java
WDVL Java
ScriptSearch Java |
| ||||||||||||||||||||||||||
