Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (497)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
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  
  Movement Issues  (Read 3497 times)
0 Members and 1 Guest are viewing this topic.
Offline Grobinson

Senior Newbie





« Posted 2012-03-26 21:54:09 »

I have been following some tutorials, trying to get into programming in Java and so far all has worked well. The problem I have now stumbled upon is, I am not able to move my image around in my window with the arrow keys after proceeding with the tutorials into threads. I have followed the code, in a manner I thought was precise, and yet, my image will not move with the arrow keys being pressed down. Can anyone point out what I'm doing wrong?

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  
package javagame;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.ImageIcon;
import javax.swing.JFrame;

public class JavaGame extends JFrame implements Runnable{

   int x, y, xDirection, yDirection;
   private Image dbImage;
   private Graphics dbg;
   Image face;
   
   public void run(){
      try{
         while(true){
            move();
            Thread.sleep(5);
         }
      }
      catch(Exception e){
         System.out.println("error...");
      }
   }
   
   // handle movement thread.
  public void move() {
      x += xDirection;
      y += yDirection;
     
      if (x <= 0);
           x = 0;
      if (x >= 50);
           x = 50;
      if (y <= 0);
           y = 0;
      if (y >= 50);
           y = 50;
   }

   // set up the directional movement variables of the player.
  public void setXDirection(int xdir) {
      xDirection = xdir;
   }
   public void setYDirection(int ydir) {
      yDirection = ydir;
   }

   // handle key input.
  public class AL extends KeyAdapter {
      public void keyPressed(KeyEvent e) {
         int keyCode = e.getKeyCode();
         // handle arrow key movement.
        if (keyCode == e.VK_LEFT) {
            setXDirection(-1);
         }
         if (keyCode == e.VK_RIGHT) {
            setXDirection(+1);
         }
         if (keyCode == e.VK_UP) {
            setYDirection(-1);
         }
         if (keyCode == e.VK_DOWN) {
            setYDirection(+1);
         }

      }

      public void keyReleased(KeyEvent e) {
         int keyCode = e.getKeyCode();
         if (keyCode == e.VK_LEFT) {
            setXDirection(0);
         }
         if (keyCode == e.VK_RIGHT) {
            setXDirection(0);
         }
         if (keyCode == e.VK_UP) {
            setYDirection(0);
         }
         if (keyCode == e.VK_DOWN) {
            setYDirection(0);
         }
      }
   }

   // handle the window class.
  public JavaGame() {
      // load images.
     ImageIcon i = new ImageIcon(
            "C:/Users/Me/workspace/JavaGame/src/javagame/face.png");
      face = i.getImage();
     
      // game properties.
     addKeyListener(new AL());
      setLocation(400, 300);
      setTitle("JavaGame");
      setSize(256, 256);
      setResizable(false);
      setVisible(true);
      setBackground(Color.RED);
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      x = 50;
      y = 50;
   }

   // handle graphics input.
  public void paint(Graphics g) {
      dbImage = createImage(getWidth(), getHeight());
      dbg = dbImage.getGraphics();
      paintComponent(dbg);
      g.drawImage(dbImage, 0, 0, this);
   }

   public void paintComponent(Graphics g) {
      // draw and refresh the image.
     g.setColor(Color.RED);
      g.drawImage(face, x, y, this);
      repaint();
   }

   public static void main(String[] args) {
      JavaGame jg = new JavaGame();
      // Threads
     Thread t1 = new Thread(jg);
      t1.start();
   }

}


I was able to move the object earlier using the following code format.
1  
2  
3  
4  
if (x >= 200);
    x = 200;
else
    x += 5;

This, however was not best for the style I wanted to do. Where did I go wrong?
Offline ra4king

JGO Kernel


Medals: 345
Projects: 3
Exp: 5 years


I'm the King!


« Reply #1 - Posted 2012-03-26 23:04:50 »

You put semi-colons after if statements! You shouldn't do that, as that means that the if statement body is empty Grin

This way, your move method only causes X and Y to be permanently at 50,50 Smiley

Offline sproingie

JGO Kernel


Medals: 202



« Reply #2 - Posted 2012-03-26 23:22:19 »

I'm trying to figure out how that would even compile, since the 'else' should be unexpected at that point.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Grobinson

Senior Newbie





« Reply #3 - Posted 2012-03-26 23:42:27 »

You put semi-colons after if statements! You shouldn't do that, as that means that the if statement body is empty Grin

This way, your move method only causes X and Y to be permanently at 50,50 Smiley
Fantastic. That's exactly what it was. After reviewing the tutorial he was doing I saw that he did in fact place the semi-colons there himself, only to remove them later on while his video was paused to correct other code. Thank you for your help. Now I just need to figure out how to make the controls a little less glitchy. Cheesy
Offline ReBirth
« Reply #4 - Posted 2012-03-27 13:07:42 »

I'm trying to figure out how that would even compile, since the 'else' should be unexpected at that point.

no 'else' in his full code.

maybe we should publish more about pastebin feature.

Offline ra4king

JGO Kernel


Medals: 345
Projects: 3
Exp: 5 years


I'm the King!


« Reply #5 - Posted 2012-03-27 14:29:14 »

I'm trying to figure out how that would even compile, since the 'else' should be unexpected at that point.

no 'else' in his full code.

maybe we should publish more about pastebin feature.
There's an "else" in that second code snippet Cranky

Offline Grobinson

Senior Newbie





« Reply #6 - Posted 2012-03-28 02:30:43 »

Yeah I'm still fairly new to the Java world so I make simple mistakes sometimes. Cheesy Especially when I'm trying to follow someone who talks and types fairly quickly. The only thing I'm not liking about this so far is the fact that it's fairly glitchy in the movement. Basically when you move in the four general directions of up, down, left, and right, it moves smooth and crisply. When you move in a diagonal direction, it catches for a moment before continuing it's motion. Is there a simple fix to this that anyone can guide me towards or any suggestions to fix it?
Offline GabrielBailey74
« Reply #7 - Posted 2012-03-28 04:10:56 »

Simply add brackets:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
// handle movement thread.
  public void move() {
      x += xDirection;
      y += yDirection;
      if (x <= 0) {
         x = 0;
      }
      if (x >= 50) {
         x = 50;
      }
      if (y <= 0) {
         y = 0;
      }
      if (y >= 50) {
         y = 50;
      }
   }

Offline Grobinson

Senior Newbie





« Reply #8 - Posted 2012-03-28 10:42:00 »

Simply add brackets:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
// handle movement thread.
  public void move() {
      x += xDirection;
      y += yDirection;
      if (x <= 0) {
         x = 0;
      }
      if (x >= 50) {
         x = 50;
      }
      if (y <= 0) {
         y = 0;
      }
      if (y >= 50) {
         y = 50;
      }
   }

that's the thing. I have added brackets already and it had no effect in making the movement less glitchy.
Offline ReBirth
« Reply #9 - Posted 2012-03-28 11:30:05 »

I'm trying to figure out how that would even compile, since the 'else' should be unexpected at that point.

no 'else' in his full code.

maybe we should publish more about pastebin feature.
There's an "else" in that second code snippet Cranky
That's not his main code part. Guess he typed it while posting.

@OP: how about your framerate?

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

Senior Newbie





« Reply #10 - Posted 2012-03-28 20:44:39 »

I'm not sure about my frame-rate. I have no code implemented to tell me what it is, nor do I know of any built in function that may exist.
Offline ReBirth
« Reply #11 - Posted 2012-03-29 12:25:56 »

Sometimes fps can be the root. Too much or less frame in a second can cause bad move. However it's easily fixed by change your speed value.

Offline StonePickaxes

JGO Coder


Medals: 4
Projects: 2


Nathan Kramber


« Reply #12 - Posted 2012-03-29 13:17:41 »

You are saying if x is less than or equal to zero, set it to 0, which means it is going to get caught in a loop and you won't be able to move in the x direction.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
   public void move() {
      x += xDirection;
      y += yDirection;
      if (x <= 0) {   //change to if (x < 0)
        x = 0;
      }
      if (x >= 50) { //change to if (x > 50)
        x = 50;
      }
      if (y <= 0) { // change to if (y < 0)
        y = 0;
      }
      if (y >= 50) { // change to if (y > 50)
        y = 50;
      }
   }

Check out my website!
Offline Grobinson

Senior Newbie





« Reply #13 - Posted 2012-03-29 13:53:17 »

You are saying if x is less than or equal to zero, set it to 0, which means it is going to get caught in a loop and you won't be able to move in the x direction.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
   public void move() {
      x += xDirection;
      y += yDirection;
      if (x <= 0) {   //change to if (x < 0)
        x = 0;
      }
      if (x >= 50) { //change to if (x > 50)
        x = 50;
      }
      if (y <= 0) { // change to if (y < 0)
        y = 0;
      }
      if (y >= 50) { // change to if (y > 50)
        y = 50;
      }
   }


That has it fixed. Thank you very much.
Offline ra4king

JGO Kernel


Medals: 345
Projects: 3
Exp: 5 years


I'm the King!


« Reply #14 - Posted 2012-03-29 14:07:20 »

Actually it should never be stuck in a loop, because you are adding xDirection and yDirection before your if checks.

Offline StonePickaxes

JGO Coder


Medals: 4
Projects: 2


Nathan Kramber


« Reply #15 - Posted 2012-03-29 19:44:41 »

Ahh didn't notice that.

Check out my website!
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.

UprightPath (20 views)
2014-09-20 20:14:06

BurntPizza (26 views)
2014-09-19 03:14:18

Dwinin (40 views)
2014-09-12 09:08:26

Norakomi (70 views)
2014-09-10 13:57:51

TehJavaDev (95 views)
2014-09-10 06:39:09

Tekkerue (49 views)
2014-09-09 02:24:56

mitcheeb (70 views)
2014-09-08 06:06:29

BurntPizza (52 views)
2014-09-07 01:13:42

Longarmx (39 views)
2014-09-07 01:12:14

Longarmx (45 views)
2014-09-07 01:11:22
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

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!