Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (536)
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  
  Cannot get this to work properly.  (Read 2418 times)
0 Members and 1 Guest are viewing this topic.
Offline 106498

Senior Newbie





« Posted 2007-04-12 06:28:25 »

This is the code for my first game. So far Its supposed to be a circle that moves to wherever you click the mouse. The code compiles fine but when I run the applet the circle only moves along the y-axis, and not across. Could someone please tell me what's wrong with it?

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  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class App extends Applet implements MouseListener, Runnable
{
 Image offscreen;
 Graphics buffer;
 int posX, posY, clickX, clickY;
 Thread th;
 
 
 

 public void init()
 {
  offscreen = this.createImage(100,100);
  buffer = offscreen.getGraphics();

  addMouseListener(this);
 
 }


 public void paint(Graphics g)
 {
  buffer.setColor(Color.white);
  buffer.fillRect(0,0,100,100);
  buffer.setColor(Color.red);
  buffer.fillOval(posX - 5,posY - 5,10,10);
  g.drawImage(offscreen,1,1,this);
 }

 public void start()
 {
  if(th == null)
  {
   th = new Thread(this);
  }  
 }

 public void stop()
 {
 
 }
 

 public void mouseClicked (MouseEvent me)
 {
  clickX = me.getX();
  clickY = me.getY();
 
  th.start();
 }

 public void run()
 {
  while(isActive())
  {
   try
   {
    //CLICK IS TO THE RIGHT...
   if(clickX > posX)
    {
      //...and lower than ball
     if(clickY > posY)
      {
        posX ++;
        posY ++;
        repaint();
      }
      //...and higher than ball
     if(clickY < posY)
      {
        posX ++;
        posY --;
        repaint();
      }
      //...and equal height to the ball
     if(clickY == posY)
      {
        posX++;
        repaint();
      }
    }
   
    //CLICK IS TO THE LEFT...
   if(clickX > posX)
    {
      //...and lower than ball
     if(clickY > posY)
      {
        posX --;
        posY ++;
        repaint();
      }
      //...and higher than ball
     if(clickY < posY)
      {
        posX --;
        posY --;
        repaint();
      }
      //...and equal height to the ball
     if(clickY == posY)
      {
        posX--;
        repaint();
      }
    }
   
    //CLICK IS SAME X-CO AS BALL...
   if(clickX == posX)
    {
      //...and lower than ball
     if(clickY > posY)
      {
       
        posY ++;
        repaint();
      }
      //...and higher than ball
     if(clickY < posY)
      {
       
        posY --;
        repaint();
      }
      //...and equal height to the ball
     if(clickY == posY)
      {
        repaint();
        th.interrupt();
      }
    }
     
   }
   catch(Exception e)
   {
   }
  }
 }
  public void mouseEntered (MouseEvent me) {}
  public void mousePressed (MouseEvent me) {}
  public void mouseReleased (MouseEvent me) {}  
  public void mouseExited (MouseEvent me) {}



}
Offline fletchergames

Senior Member





« Reply #1 - Posted 2007-04-12 17:55:34 »

I'm not sure exactly what you're doing wrong, but you're code is way more complicated than it has to be.

First of all, the Thread th is useless and might even be screwing things up because it gets restarted every time you click the mouse.  Just include the code in the run method in the start method instead.

Second, the run method's code is way too complicated.  It should just be:

1  
2  
3  
4  
5  
6  
if(clickX > posX)
   posX ++;
else if(clickX < posX)
   posX--;

//similar code for y - completely seperate from code for x


You don't need to call repaint because the Applet will get repainted anyways unless you've shut off passive rendering (which can be done with "setIgnoreRepaint(true)", though there's a little more to it than that).  If you did shut off passive rendering, repaint won't do anything anyways.

Also, the mouseClicked method only gets called when you press and release the mouse button without moving it off the Component.  I don't think that's an issue here though.
Offline Mr_Light

Senior Member




shiny.


« Reply #2 - Posted 2007-04-12 20:25:30 »

he's using Applet not JApplet.

It's harder to read code than to write it. - it's even harder to write readable code.

The gospel of brother Riven: "The guarantee that all bugs are in *your* code is worth gold." Amen brother a-m-e-n.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline kaosnkorruption

Senior Newbie





« Reply #3 - Posted 2007-04-13 00:48:46 »

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
//CLICK IS TO THE LEFT...
   if(clickX > posX)
    {
      //...and lower than ball
     if(clickY > posY)
      {
        posX --;
        posY ++;
        repaint();
      }
      //...and higher than ball
     if(clickY < posY)
      {
        posX --;
        posY --;
        repaint();
      }
      //...and equal height to the ball
     if(clickY == posY)
      {
        posX--;
        repaint();
      }
    }


not sure the issue of it not moving on the x-axis, but i do have these observations.

1. That should be if clickX < posX

2. use else if statements when you check your y position after already checking if the click is lower than the position.  This prevents unnecessary checks from every having to execute.

3.  why execute posX-- or posX++ inside every if statement.  Since that is the one constant of the larger if statement, call it outside of your y-value checks, and only execute posY-- or posY++ inside your y-value checks.

4.  there are certainly better ways to do this. 

Gotta head to class now, but If no one else has provided a better solution, I'll reply again later tonight.
Offline 106498

Senior Newbie





« Reply #4 - Posted 2007-04-13 02:34:20 »

Thanks for the replies!

I think this is the reason for not moving along x

1. That should be if clickX < posX
the first if clickX > posX moves the ball to the right, and the second if clickX >posX moves it right back!! Silly me! Embarrassed
I changed the code and added in sleep() so the ball moves slowly.
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  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class App extends Applet implements MouseListener, Runnable
{
 Image offscreen;
 Graphics buffer;
 int posX, posY, clickX, clickY;
 Thread th;
 
 
 

 public void init()
 {
  offscreen = this.createImage(100,100);
  buffer = offscreen.getGraphics();

  addMouseListener(this);
 
 }


 public void paint(Graphics g)
 {
  buffer.setColor(Color.white);
  buffer.fillRect(0,0,100,100);
  buffer.setColor(Color.red);
  buffer.fillOval(posX - 5,posY - 5,10,10);
  g.drawImage(offscreen,1,1,this);
 }

 public void start()
 {
  if(th == null)
  {
   th = new Thread(this);
  }  
 }

 public void stop()
 {
 
 }
 

 public void mouseClicked (MouseEvent me)
 {
   clickX = me.getX();
   clickY = me.getY();
   
   th.start();
   
 }

 public void run()
 {
  while(isActive())
  {
   try
   {
     if(clickX > posX)
     {
       posX ++;
     }
     else if(clickX < posX)
     {
       posX --;
     }
     
     if(clickY > posY)
     {
       posY ++;
     }
     else if(clickY < posY)
     {
       posY --;
     }
     
     if(posX != clickX || posY !=clickY)
     {
       repaint();
       th.sleep(100);
     }
   }
   catch(Exception e)
   {
   }
  }
 }
  public void mouseEntered (MouseEvent me) {}
  public void mousePressed (MouseEvent me) {}
  public void mouseReleased (MouseEvent me) {}  
  public void mouseExited (MouseEvent me) {}



}


It all works but the second time (and following times) MouseClicked is called I get like 8 error messages in command. I think its because I call start when the thread is already running but I cant see any other way of doing it. Looks like I need some help once again!
Offline fletchergames

Senior Member





« Reply #5 - Posted 2007-04-13 19:28:05 »

Just take the Thread out.  You shouldn't need it at all.  You would only need a Thread there if the program were doing alot of computation in mouseClicked, which it isn't.

Or actually end the Thread when it's done - all you have to do is take out the while loop.  That wouldn't be perfect though because mouseClicked might get called again before the Thread is done.  You would have to create a new Thread each time (or use some kind of ThreadPool).  It's just not worth it here.

Or set a boolean saying rather the Thread is running.  This would require some synchronization.  I don't want to spend the time to test it to make sure it works (or post code that doesn't work here), so you'll just have to learn about Threads if you want to do it that way.

Frankly, multithreading is complicated.  I don't do it except where necessary.  After I move, I plan to buy a book about multithreading and learn more about it.  You can't just toss in Threads at random - you'll have to learn about multithreading before you can use them effectively.
Offline SimonH
« Reply #6 - Posted 2007-04-13 20:20:15 »

Or try this;

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
public void start()
 {
  if(th == null)
  {
   th = new Thread(this);
   th.start();
  }  
 }

 public void mouseClicked (MouseEvent me)
 {
   clickX = me.getX();
   clickY = me.getY();
 }

People make games and games make people
Offline 106498

Senior Newbie





« Reply #7 - Posted 2007-04-14 00:18:17 »

My java book shows how to do this but it uses deprecated methods. Fletchergames, when you say take out the thread alltogether, how would you control the speed of the animation? Or use doublebuffering?
When I followed SimonH,s advice the ball wouldn't move at all. Is there a way of slowing down a loop, timers or something?
Offline Jeff

JGO Coder




Got any cats?


« Reply #8 - Posted 2007-04-14 00:28:36 »

My java book shows how to do this but it uses deprecated methods. Fletchergames, when you say take out the thread alltogether, how would you control the speed of the animation?

Note that using passive rendering the animation is never under your control.  Things wil render when AWT/Swing think its appropriate.  AWT will consolidate render calls for efficiency so just because you call update 10 times in a  second doesn't mean it will render 10 time in that second, or 10 times at all.

This is one of a number of reasons most serious games don't use passive rendering and generally avoid Swing altogether.




Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!

http://wiki.java.net/bin/view/Games/JeffFAQ
Offline Jeff

JGO Coder




Got any cats?


« Reply #9 - Posted 2007-04-14 00:30:15 »

My java book shows how to do this but it uses deprecated methods.

You might want to try starting with the tutorials at cokeandcode.com

Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!

http://wiki.java.net/bin/view/Games/JeffFAQ
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline SimonH
« Reply #10 - Posted 2007-04-14 02:39:23 »

When I followed SimonH,s advice the ball wouldn't move at all. Is there a way of slowing down a loop, timers or something?

Oops! I just compiled and ran it & you're right! I fixed it though;

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
 public void run()
 {
  int moveSpeed=4;
  while(true)
  {
   try
   {
     if(clickX > posX)
     {
       posX+=moveSpeed;
     }
     else if(clickX < posX)
     {
       posX-=moveSpeed;
     }
&c...


You can change moveSpeed & the sleep() time to adjust the speed.

People make games and games make people
Offline fletchergames

Senior Member





« Reply #11 - Posted 2007-04-14 18:08:04 »

Since you're using mouseClicked, not mousePressed, the circle should only move one pixel (or however far you want it to move) each time you click the mouse.  So you should be able to remove the thread.  See the following code:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
public void mouseClicked (MouseEvent me) 
 {
   clickX = me.getX();
   clickY = me.getY();
   
     if(clickX > posX)
     {
       posX ++;
     }
     else if(clickX < posX)
     {
       posX --;
     }
     
     if(clickY > posY)
     {
       posY ++;
     }
     else if(clickY < posY)
     {
       posY --;
     }
 }


It would be different if you had alot of stuff happening in mouseClicked.  If that were the case, the EDT would be busy running that code, and the interface would be unresponsive during that time.  That's when you would need a thread.  Or, at least, that's the way it looks to me.
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.

Riven (20 views)
2014-07-29 18:09:19

Riven (13 views)
2014-07-29 18:08:52

Dwinin (12 views)
2014-07-29 10:59:34

E.R. Fleming (31 views)
2014-07-29 03:07:13

E.R. Fleming (12 views)
2014-07-29 03:06:25

pw (42 views)
2014-07-24 01:59:36

Riven (42 views)
2014-07-23 21:16:32

Riven (28 views)
2014-07-23 21:07:15

Riven (29 views)
2014-07-23 20:56:16

ctomni231 (60 views)
2014-07-18 06:55:21
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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!