Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (120)
games submitted by our members
Games in WIP (577)
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  
  Objects reacting to mouse clicks  (Read 1001 times)
0 Members and 1 Guest are viewing this topic.
Offline MindTwisteR

Junior Newbie





« Posted 2007-11-19 05:35:11 »

Hello there guys,


so i am trying to code a simple game to try to "exercise" my skills in java since i started university 1 month ago.


the objective of the game is simple:

the program promts the user for 2 values: "minimum bounce" and "maximum Bounce".

then a circle and a cross displays on the screen and the user clicks on the aplet with the mouse near a aplet limit in order to do the ball bounce in the wall to hit the cross.



the thing is how to i make the ball "bounce" were i click with the mouse???

i am using ACM library.

thanks in advance.
Offline alexxz4

Junior Duke


Projects: 1



« Reply #1 - Posted 2007-11-19 14:28:35 »

You mean u click somewhere and the ball starts moving towards the mouse, when it reaches the edge of the applet, it bounces off of it?

U need this function:
1  
public boolean mouseDown (Event e, int x, int y){}

which responds to mouse clicks. x and y are mouse coordinates.

[ Motherload Unlimited looking for programmers ] [ Solitaire ]
Offline MindTwisteR

Junior Newbie





« Reply #2 - Posted 2007-11-19 16:34:08 »

here is a example in flash of what i am trying to do Smiley


http://www.estig.ipbeja.pt/~jpb/p1/tp2/tp2.htm


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

JGO Kernel


Medals: 56
Projects: 11


Monkey for a head


« Reply #3 - Posted 2007-11-19 16:58:41 »

so i am trying to code a simple game to try to "exercise" my skills in java

You mean the same "game" you posted as a homework question here: http://www.gamedev.net/community/forums/topic.asp?topic_id=472640  Roll Eyes

Generally:
- Detect coords of mouse click
- Construct vector from ball to mouse click (subtract one from the other)
- Move your ball in the direction of the vector each animation frame.
- Detect when the ball touches an edge (based on x/y position)
- Flip direction vector based on which edge was hit

[ TriangularPixels.com - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
Offline alexxz4

Junior Duke


Projects: 1



« Reply #4 - Posted 2007-11-19 19:42:59 »

This could do the trick
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
...
double room_width,room_height;        // aplet dimensions
double radius=5;
double ballx=200,bally=200,xspeed=0,yspeed=0,bounces=0,crossx=500,crossy=100;
...
public boolean mouseDown (Event e, int x, int y){
if(xspeed==0 && yspeed==0){
double dist=Math.sqrt((ballx-x)*(ballx-x)+(bally-y)*(bally-y));
xspeed=5*(x-ballx)/dist;
yspeed=5*(y-bally)/dist;
}
}
public void run(){
while(true){
ballx+=xspeed;
bally+=yspeed;
if(ballx<radius || ballx>room_width-radius){
xspeed=-xspeed;bounces++;
}
if(bally<radius || bally>room_height-radius){
yspeed=-yspeed;bounces++;
}
if(bounces>2){
bounces=0;xspeed=0;yspeed=0;
ballx=room_width*Math.random();
bally=room_height*Math.random();
crossx=room_width*Math.random();
crossy=room_height*Math.random();
// pop up the message "missed"
}
if(ballx>crossx-10 && ballx<crossx+10 && bally>crossy-10 && bally<crossy+10 && bounces>0){
bounces=0;xspeed=0;yspeed=0;
ballx=room_width*Math.random();
bally=room_height*Math.random();
crossx=room_width*Math.random();
crossy=room_height*Math.random();
// pop up the message "got it!"
}
}
...
}
public void paint(Graphics g){
g.drawImage(ball,(int)ballx,(int)bally,null,this);
g.drawImage(cross,(int)crossx,(int)crossy,null,this);
...
}

[ Motherload Unlimited looking for programmers ] [ Solitaire ]
Offline MindTwisteR

Junior Newbie





« Reply #5 - Posted 2007-11-19 21:07:39 »

You mean the same "game" you posted as a homework question here: http://www.gamedev.net/community/forums/topic.asp?topic_id=472640  Roll Eyes

Generally:
- Detect coords of mouse click
- Construct vector from ball to mouse click (subtract one from the other)
- Move your ball in the direction of the vector each animation frame.
- Detect when the ball touches an edge (based on x/y position)
- Flip direction vector based on which edge was hit



thats not me. maybe its some1 from my class Tongue but thanks for the link anyway Cheesy

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 (52 views)
2014-10-17 03:59:02

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

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

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

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

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

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

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

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

BurntPizza (86 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!