Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (487)
Games in Android Showcase (112)
games submitted by our members
Games in WIP (553)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1] 2
  ignore  |  Print  
  Movement Speed  (Read 2895 times)
0 Members and 1 Guest are viewing this topic.
Offline jparril1

Junior Member





« Posted 2011-03-12 02:54:13 »

Hey guys, so I have another question.

Im trying to figure out how to deal with moving my characters faster. Right now my xSpeed, which I add to xPosition in my move() method is set to 1 or 2. So basically my character is moving 1 or 2 pixels per update. This looks really nice and smooth, but its obviously slow. If I want my character to go fast, how can I achieve this?? Setting my speed higher make him move fast, but it makes the animation very choppy, since its kind of jumping over 5 pixels or more each frame. How can I make him move faster but keep the smooth transition in the painting?
Offline ReBirth
« Reply #1 - Posted 2011-03-12 03:35:54 »

First determine how much time you want for that object to move 1pixel. Example you want it takes 2 seconds in every 1pixel move. Then see your delta update time and move your object on how much it need to move based on that time. I use this and move pretty smooth.

1  
2  
3  
public void move(long delta){
    x += delta * speedOnMilisecond;
}


however, if your delta update time goes high, the animation will become choppy again as you said. So, you need to keep that delta small.

Offline jparril1

Junior Member





« Reply #2 - Posted 2011-03-12 04:05:20 »

Sorry but not sure if I get that. Basically my game loop is controlled in a class that extends JPanel and implements runnable. Im that class I have the run method which basically calls update on all game entities (moves, shoot, etc) then calls repaint. This loop runs at about 100fps. since I call the move methods in here, basically whatever my move method does is going to be shown every frame. So If my move method moves by one pixel, each loop I will see a one pixel move. How can I move further per frame without causing a choppy jump? Or should I not actually be moving more per frame?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ra4king

JGO Kernel


Medals: 345
Projects: 2
Exp: 5 years


I'm the King!


« Reply #3 - Posted 2011-03-12 04:20:36 »

At a 100FPS, you can go pretty fast without seeing a lot of choppiness.
You can do 100 pixels per second (1 pixel per update) or 200 pixels per second (2 pixels per update).

Offline ReBirth
« Reply #4 - Posted 2011-03-12 04:24:07 »

If it's 100fps then you call sleep at max 10ms. My previous reply is based on
http://www.cokeandcode.com/spaceinvaderstutorial
hope you can get it right from the source. It's pretty good to update the gameloop. I think It's hard to get it smoother on your current way because you already have high fps (100) and minimum length on screen (1pixel) as ra4king said.

Offline deadly72

Junior Member





« Reply #5 - Posted 2011-03-12 04:25:37 »

First determine how much time you want for that object to move 1pixel. Example you want it takes 2 seconds in every 1pixel move. Then see your delta update time and move your object on how much it need to move based on that time. I use this and move pretty smooth.

1  
2  
3  
public void move(long delta){
    x += delta * speedOnMilisecond;
}


however, if your delta update time goes high, the animation will become choppy again as you said. So, you need to keep that delta small.

This is more or less what you want done.  Your update loop should be receiving the deltaTime it took to complete one loop in your game loop.  For example I'm using

1  
update((int) ((currentUpdateTime - lastUpdateTime)/(1000*1000)));


which I yanked up from http://www.java-gaming.org/topics/basic-game/21919/view.html.  It might be something to use as a reference for you to wrap the idea of using deltaTime to calculate your movement steps.  It did take me a little while to understand how to get consistency between movement but once you understand the concept its actually pretty simple!

Good luck!
Offline jparril1

Junior Member





« Reply #6 - Posted 2011-03-12 04:36:59 »

Ok I think I kinda get it.. heres what my main game class is right now:

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.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Toolkit;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;


import javax.swing.JPanel;



public class GamePanel extends JPanel implements Runnable {

   private Thread animator;

   private Player player;

   private final int DELAY = 20;
   


   public GamePanel() {

      addKeyListener(new KAdapter());
      setFocusable(true);
      setBackground(Color.black);
      setDoubleBuffered(true);

      player = new Player();

   }

   @Override
   public void run() {

      long beforeTime, timeDiff, sleep;

      beforeTime = System.currentTimeMillis();

      while(true) {

         player.update();
         repaint();

         timeDiff = System.currentTimeMillis() - beforeTime;
         sleep = DELAY - timeDiff;

         if (sleep < 0)
            sleep = 2;
         try {
            Thread.sleep(sleep);
         } catch (InterruptedException e) {
            System.out.println("interrupted");
         }

         beforeTime = System.currentTimeMillis();
      }


   }

   public void paint(Graphics g) {
     
      super.paint(g);

      Graphics2D g2 = (Graphics2D)g;

      if(player.isVisible()) {
         g2.drawImage(player.getImage(), player.getXPosition(), player.getYPosition(), this);
      }

      Toolkit.getDefaultToolkit().sync();
      g.dispose();

   }

   private class KAdapter extends KeyAdapter {

      public void keyReleased(KeyEvent e) {
         player.keyReleased(e);
      }

      public void keyPressed(KeyEvent e) {
         player.keyPressed(e);
      }
   }

   public void addNotify() {

      super.addNotify();  

      animator = new Thread(this);
      animator.start();
   }



}


I got the loop idea from a tutorial I read. What should I be doing here???
Offline ra4king

JGO Kernel


Medals: 345
Projects: 2
Exp: 5 years


I'm the King!


« Reply #7 - Posted 2011-03-12 05:17:58 »

No you should give timeDiff to update() and beforeTime = System.currentTimeMillis() should come before the sleep.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
while(true) {
    timeDiff = System.currentTimeMillis()-beforeTime;
    beforeTime = System.currentTimeMillis();
   
    player.update(timeDiff);
    repaint();
   
    sleep = DELAY-timeDiff;
   
    if(sleep < 2) sleep = 2;
   
    try{
        Thread.sleep(sleep);
    } catch(Exception exc) {
        exc.printStackTrace();
    }
}

Offline jparril1

Junior Member





« Reply #8 - Posted 2011-03-12 16:39:01 »

Ok I did that, and now hes moving way fast. Im going to post all of the code and maybe you guys can take a look at it? I understand everything well except for the run method. I dont understand exactly what I should be doing.

Im posting 3 classes and an Interface

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
import java.awt.Image;
import java.awt.Rectangle;


public interface Entity {
   
   public void move(long timeDiff);
   
   public void update(long timeDiff);
   
   public Image getImage();
   
   public int getXPosition();
   
   public int getYPosition();
   
   public boolean isVisible();
   
   public Rectangle getBounds();

}


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  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
171  
172  
173  
174  
175  
176  
177  
178  
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;

import javax.swing.ImageIcon;


public class Player implements Entity {

   private String player = "player.png";

   //x and y positions for the player
  private int xPosition;
   private int yPosition;

   //x and y speeds for the player
  private int xSpeed;
   private int ySpeed;

   //width and height of the player
  private int width;
   private int height;

   private Image image;

   private boolean wPressed;
   private boolean sPressed;
   private boolean aPressed;
   private boolean dPressed;

   private boolean visible;



   public Player() {

      ImageIcon playerIcon = new ImageIcon(this.getClass().getResource(player));
      image = playerIcon.getImage();

      //setting the players dimensions
     width = image.getWidth(null);
      height = image.getHeight(null);
     

      //setting the initial player position and speed
     xPosition = 0;
      yPosition = MainFrame.gameWindowHeight - 100;
     

      xSpeed = 1;
      ySpeed = 1;


      //setting visible
     visible = true;


   }

   @Override
   public void move(long timeDiff) {

      if(dPressed) {
         xPosition = xPosition + (int)(xSpeed * timeDiff);
         
      }
      if(aPressed) {
         xPosition = xPosition - (int) (xSpeed * timeDiff);

      }
      if(wPressed) {

         for(int j = 0; j < 50; j++) {
            if(j <= 25) yPosition += 1;//you'll go up when the jump is under halfway done
           if(j >= 25) yPosition -= 1;//you'll go down when the jump is over halfway done
        }
     
      }

   }


   @Override
   public void update(long timeDiff) {

      move(timeDiff);
   }

   @Override
   public Image getImage() {
      // TODO Auto-generated method stub
     return image;
   }

   @Override
   public int getXPosition() {
      // TODO Auto-generated method stub
     return xPosition;
   }

   @Override
   public int getYPosition() {
      // TODO Auto-generated method stub
     return yPosition;
   }

   @Override
   public boolean isVisible() {
      // TODO Auto-generated method stub
     return visible;
   }

   @Override
   public Rectangle getBounds() {
      // TODO Auto-generated method stub
     return new Rectangle(getXPosition(), getYPosition(), width, height);
   }

   public void keyPressed(KeyEvent e) {

      int key = e.getKeyCode();

      if(key == KeyEvent.VK_W) {

         wPressed = true;

      }

      if(key == KeyEvent.VK_S) {

         sPressed = true;

      }

      if(key == KeyEvent.VK_A) {

         aPressed = true;

      }

      if(key == KeyEvent.VK_D) {

         dPressed = true;

      }


   }

   public void keyReleased(KeyEvent e) {

      int key = e.getKeyCode();

      if(key == KeyEvent.VK_W) {

         wPressed = false;

      }

      if(key == KeyEvent.VK_S) {

         sPressed = false;

      }

      if(key == KeyEvent.VK_A) {

         aPressed = false;

      }

      if(key == KeyEvent.VK_D) {

         dPressed = false;

      }
   }
}


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  
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Toolkit;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;


import javax.swing.JPanel;



public class GamePanel extends JPanel implements Runnable {

   private Thread animator;

   private Player player;

   private final int DELAY = 50;



   public GamePanel() {

      addKeyListener(new KAdapter());
      setFocusable(true);
      setBackground(Color.black);
      setDoubleBuffered(true);

      player = new Player();

   }

   @Override
   public void run() {

      long beforeTime = 0, timeDiff, sleep;

      while(true) {

         timeDiff = System.currentTimeMillis() - beforeTime;
         beforeTime = System.currentTimeMillis();

         player.update(timeDiff);
         repaint();

         
         sleep = DELAY - timeDiff;

         if (sleep < 0)
            sleep = 2;
         try {
            Thread.sleep(sleep);
         } catch (InterruptedException e) {
            System.out.println("interrupted");
         }

     
      }


   }

   public void paint(Graphics g) {

      super.paint(g);

      Graphics2D g2 = (Graphics2D)g;

      if(player.isVisible()) {
         g2.drawImage(player.getImage(), player.getXPosition(), player.getYPosition(), this);
      }

      Toolkit.getDefaultToolkit().sync();
      g.dispose();

   }

   private class KAdapter extends KeyAdapter {

      public void keyReleased(KeyEvent e) {
         player.keyReleased(e);
      }

      public void keyPressed(KeyEvent e) {
         player.keyPressed(e);
      }
   }

   public void addNotify() {

      super.addNotify();  

      animator = new Thread(this);
      animator.start();
   }



}


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  
import javax.swing.JFrame;


public class MainFrame extends JFrame {
   
private static final long serialVersionUID = -628360966210671157L;
   
   //using these to tell anyone the dimensions of the window
  protected static int gameWindowWidth = 640;
   protected static int gameWindowHeight = 480;
   
   public MainFrame() {

      add(new GamePanel());

      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setSize(gameWindowWidth, gameWindowHeight);
      setLocationRelativeTo(null);
      setTitle("...");
      setResizable(false);
      setVisible(true);
   

   }
   
   public static void main(String[] args) {
     
      new MainFrame();
   }


}


Hopefully one of you can run this and tell me where Im going wrong
Offline dishmoth
« Reply #9 - Posted 2011-03-12 17:12:41 »

From a quick glance it looks like you're doing passive rendering, i.e., relying on the panel's repaint() method.
The animation should be smoother if you do "active rendering" (search the forums, and look at how rendering is done in this tutorial).
Simon

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

Junior Member





« Reply #10 - Posted 2011-03-12 18:08:40 »

Ok so does this mean I should entirely change the structure of my game? Should I drop the use of JPanels completely and use a canvas? Also, If I use a canvas, do I "add" this to an outer JFrame eventually? I never really used any other approach than Jpanel and Jframe so Im kinda confused. Im not trying to make an applet, so Im confused as to where the Canvas actually gets displayed
Offline ra4king

JGO Kernel


Medals: 345
Projects: 2
Exp: 5 years


I'm the King!


« Reply #11 - Posted 2011-03-12 19:55:19 »

JPanel is not really well suited for drawing. I recommend JComponent if you want to use Swing.
If you're fine use Frame instead of JFrame, I recommend Canvas since it was made for drawing.

On the repaint() subject, when you start doing double buffering, you should call createBufferStrategy(2) in your JComponent/Canvas (since this is a method in Component), then get the BufferStrategy by calling getBufferStrategy(). Your BufferStrategy will have getDrawGraphics() that returns the Graphics object to use, with which you manually call paint(g), then call show() on your BufferStrategy. You should also disable repaint by calling setRepaint(false) if you will do this.

On your code, you give your update() a long in milliseconds, so you need to convert this to seconds by dividing by 1000 in your move() so it would be:
1  
2  
3  
4  
double deltaTime = (double)timeDiff/1000;
//...
xPosition = xPosition + (xSpeed * deltaTime);
//...


Also, your position variables should be in double since you can also be moving in decimal values and those are significant as they add up.

Hope that helped Grin

EDIT: Never extend JFrame unless you really need to override one of it's methods. If you don't want to use an other component as your draw surface, then you may extend JFrame and override its paint(g).

Offline jparril1

Junior Member





« Reply #12 - Posted 2011-03-12 21:27:32 »

Ok yeah Ive read that I should be using canvas instead of swing. Im gonna scrap this project and start using Canvas for all of my games. Im gonna most likely be very lost though because Im so used to just calling repaint. So is the idea that I do all of my drawing in the Canvas class? Does that not need to be added to some sort of frame to be able to have a toolbar on top and all that? Also, should this canvas be a Runnable so that I can have my loop in it? Actually, is there even supposed to be a canvas class, or do I just create a canvas instance in my main game class? confused!
Offline jparril1

Junior Member





« Reply #13 - Posted 2011-03-13 01:17:34 »

Ok so I read over Kevs Space Invader tutorial again and used his method, but it wont work. Heres my code:

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

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.image.BufferStrategy;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class Game extends Canvas implements Runnable {

   private BufferStrategy bufferStrategy;

   private Thread animator;

   private Player player;
   
   private final int DELAY = 50;


   public Game() {

      JFrame container = new JFrame("Platformer");

     

      JPanel panel = (JPanel)container.getContentPane();
      panel.setPreferredSize(new Dimension(800,600));
      panel.setLayout(null);



      setBounds(0,0,800,600);
      panel.add(this);


      setIgnoreRepaint(true);


      container.pack();
      container.setResizable(false);
      container.setVisible(true);



      addKeyListener(new KAdapter());



      requestFocus();

      player = new Player();

      createBufferStrategy(2);
      bufferStrategy = getBufferStrategy();
     
      animator = new Thread(this);
      animator.start();

   

   }

   @Override
   public void run() {
     
      long beforeTime = 0, timeDiff, sleep;

      while(true) {
         
         timeDiff = System.currentTimeMillis() - beforeTime;
         beforeTime = System.currentTimeMillis();

         
         player.update(timeDiff);
         render();


         sleep = DELAY - timeDiff;

         if (sleep < 0)
            sleep = 2;
         try {
            Thread.sleep(sleep);
         } catch (InterruptedException e) {
            System.out.println("interrupted");
         }

     
      }


   }
   
   public void render() {
     
      Graphics2D g = (Graphics2D) bufferStrategy.getDrawGraphics();
      g.setColor(Color.black);
      g.fillRect(0,0,800,600);
     
      if(player.isVisible()) {
         g.drawImage(player.getImage(), (int)player.getXPosition(),(int) player.getYPosition(), null);
      }

     
   }

   private class KAdapter extends KeyAdapter {

      public void keyPressed(KeyEvent e) {

         player.keyPressed(e);


      }
      public void keyReleased(KeyEvent e) {

         player.keyReleased(e);


      }
     
   }
}

   


Its not painting the background or my image. Do I need those JFrame and JPanels inside of my class? I know that the canvas needs to be put into some kind of frame though. What seems wrong with this?
Offline jparril1

Junior Member





« Reply #14 - Posted 2011-03-13 02:18:18 »

Ok sorry I keep posting, but Ive gotten it to paint and move, but I still cant get it to be smooth. Theres got to be something screwey with either my delta or my move method. Im posting the player and game classes. PLEASEE HELP ME FROM GOING NUTS

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  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
171  
172  
173  
174  
175  
176  
177  
package platformer;

import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;

import javax.swing.ImageIcon;


public class Player implements Entity {

   private String player = "player.png";

   //x and y positions for the player
  private double xPosition;
   private double yPosition;

   //x and y speeds for the player
  private double xSpeed;
   private double ySpeed;

   //width and height of the player
  private int width;
   private int height;

   private Image image;

   private boolean wPressed;
   private boolean sPressed;
   private boolean aPressed;
   private boolean dPressed;

   private boolean visible;



   public Player() {

      ImageIcon playerIcon = new ImageIcon(this.getClass().getResource(player));
      image = playerIcon.getImage();

      //setting the players dimensions
     width = image.getWidth(null);
      height = image.getHeight(null);
     

      //setting the initial player position and speed
     xPosition = 500;
      yPosition = 500;
     

      xSpeed = 50;
      ySpeed = 50;


      //setting visible
     visible = true;


   }

   @Override
   public void move(long timeDiff) {

      if(dPressed) {
         
         xPosition = (xPosition + ((xSpeed * timeDiff) / 1000));
     
      }
      if(aPressed) {
         xPosition = (xPosition - ((xSpeed * timeDiff) / 1000));

      }
      if(wPressed) {

         
      }

   }


   @Override
   public void update(long timeDiff) {

      move(timeDiff);
   }

   @Override
   public Image getImage() {
      // TODO Auto-generated method stub
     return image;
   }

   @Override
   public double getXPosition() {
      // TODO Auto-generated method stub
     return xPosition;
   }

   @Override
   public double getYPosition() {
      // TODO Auto-generated method stub
     return yPosition;
   }

   @Override
   public boolean isVisible() {
      // TODO Auto-generated method stub
     return visible;
   }

   @Override
   public Rectangle getBounds() {
      // TODO Auto-generated method stub
     return new Rectangle((int)getXPosition(),(int)getYPosition(), width, height);
   }

   public void keyPressed(KeyEvent e) {

      int key = e.getKeyCode();

      if(key == KeyEvent.VK_W) {

         wPressed = true;

      }

      if(key == KeyEvent.VK_S) {

         sPressed = true;

      }

      if(key == KeyEvent.VK_A) {

         aPressed = true;

      }

      if(key == KeyEvent.VK_D) {

         dPressed = true;

      }


   }

   public void keyReleased(KeyEvent e) {

      int key = e.getKeyCode();

      if(key == KeyEvent.VK_W) {

         wPressed = false;

      }

      if(key == KeyEvent.VK_S) {

         sPressed = false;

      }

      if(key == KeyEvent.VK_A) {

         aPressed = false;

      }

      if(key == KeyEvent.VK_D) {

         dPressed = false;

      }
   }
}


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

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.image.BufferStrategy;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class Game extends Canvas implements Runnable {

   private BufferStrategy bufferStrategy;

   private Thread animator;

   private Player player;

   private final int DELAY = 50;


   public Game() {

      JFrame container = new JFrame("Platformer");



      JPanel panel = (JPanel)container.getContentPane();
      panel.setPreferredSize(new Dimension(800,600));
      panel.setLayout(null);



      setBounds(0,0,800,600);
      panel.add(this);


      setIgnoreRepaint(true);


      container.pack();
      container.setResizable(false);
      container.setVisible(true);



      addKeyListener(new KAdapter());



      requestFocus();

      player = new Player();

      createBufferStrategy(2);
      bufferStrategy = getBufferStrategy();

      animator = new Thread(this);
      animator.start();



   }

   @Override
   public void run() {

      long beforeTime = 0, timeDiff, sleep;

      while(true) {

         timeDiff = System.currentTimeMillis() - beforeTime;
         beforeTime = System.currentTimeMillis();


         player.update(timeDiff);
         render();


         sleep = DELAY - timeDiff;

         if (sleep < 0)
            sleep = 2;
         try {
            Thread.sleep(sleep);
         } catch (InterruptedException e) {
            System.out.println("interrupted");
         }
      }
   }

   public void render() {

      Graphics2D g = (Graphics2D) bufferStrategy.getDrawGraphics();
      g.setColor(Color.black);
      g.fillRect(0,0,800,600);

      if(player.isVisible()) {
         g.drawImage(player.getImage(), (int)player.getXPosition(),(int) player.getYPosition(), null);
      }

      g.dispose();
      bufferStrategy.show();


   }

   private class KAdapter extends KeyAdapter {

      public void keyPressed(KeyEvent e) {

         player.keyPressed(e);


      }
      public void keyReleased(KeyEvent e) {

         player.keyReleased(e);


      }

   }
}


I have all of the active rendering working, I just dont know what to do to make it look right. Plus Im not sure if I should still have instances of JFrame and JPanel.. I really didnt know any other way
Offline ra4king

JGO Kernel


Medals: 345
Projects: 2
Exp: 5 years


I'm the King!


« Reply #15 - Posted 2011-03-13 03:06:20 »

It's running fine. The only problem is that you've set it to 20FPS, when I cranked it up to 100FPS it was smooth.
Otherwise, I see nothing wrong with your code (other than forgetting to use setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) if you ever want your program to end Grin )

Offline ReBirth
« Reply #16 - Posted 2011-03-13 03:26:12 »

Decrease the value of DELAY to increase your FPS score. And I also think that there is no need to sleep 2ms if you already pass the time limit. Just straight continue to next loop. It makes the codes shorter and neater Cheesy

Offline jparril1

Junior Member





« Reply #17 - Posted 2011-03-13 03:37:20 »

Gotcha. What Im confused about is the calculation for the FPS. How does my DELAY relate to my fps? For instance my DEALY was set to 50, and you said thats 20fps? How did you figure that out
Offline ra4king

JGO Kernel


Medals: 345
Projects: 2
Exp: 5 years


I'm the King!


« Reply #18 - Posted 2011-03-13 03:41:20 »

You have 1000 milliseconds in a minute. If your delay is 50, then 1000/50=20 updates per second. Decreasing the DELAY will increase your FPS and vice versa. If you want 100 FPS, set the DELAY to 10 since 1000/10=100 updates per second Grin

Offline ra4king

JGO Kernel


Medals: 345
Projects: 2
Exp: 5 years


I'm the King!


« Reply #19 - Posted 2011-03-13 05:47:00 »

I did some thinking and realized that the sleep variable is done wrong.
You should do this:
1  
sleep = DELAY - (System.currentTimeMillis() - beforeTime);


This is because your loop should sleep for your DELAY time minus however long your update code took so this would be constant.

Also
1  
2  
3  
4  
5  
6  
7  
//CHANGE
if(sleep < 0)
    sleep = 2;

//TO
if(sleep < 0)
    continue;


Hope that helped Grin

Offline jparril1

Junior Member





« Reply #20 - Posted 2011-03-13 16:14:03 »

I fixed the second part, but the first part seems redundant I am subtracting timeDiff from DELAY and setting that to sleep. if you look above that, timeDiff in fact is (System.currentTimeMillis - befireTime). So in effect arent I already doing
sleep = DELAY - (System.currentTimeMillis() - beforeTime) ?
Offline ra4king

JGO Kernel


Medals: 345
Projects: 2
Exp: 5 years


I'm the King!


« Reply #21 - Posted 2011-03-13 18:41:54 »

No timeDiff includes the time spent sleeping. sleep = DELAY - (int)(System.currentTimeMillis()-beforeTime) only takes off the time spent in game code so your loop would run smoothly.

Offline jparril1

Junior Member





« Reply #22 - Posted 2011-03-13 19:36:33 »

Ok fixed that, made it a lot smoother. THANKS! But it also completely screwed my player movement. I printed out his xPositions and all it does is move between 2 and five hundred something. He just disappears most of the time. I want to post all of the code with the images so that you guys can run it and see what I mean. Im going to post the code anyway, I guess you can fill in the images to make it run. Thanks so much for this debugging help!

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  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
171  
172  
173  
174  
175  
176  
177  
178  
179  
180  
181  
182  
183  
184  
185  
186  
187  
188  
189  
190  
191  
192  
193  
194  
195  
196  
package platformer;

import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;

import javax.swing.ImageIcon;


public class Player implements Entity {

   private String player = "player.png";

   //x and y positions for the player
  private double xPosition;
   private double yPosition;

   //x and y speeds for the player
  private double xSpeed;
   private double ySpeed;

   //width and height of the player
  private int width;
   private int height;

   private Image image;

   private boolean wPressed;
   private boolean sPressed;
   private boolean aPressed;
   private boolean dPressed;

   private boolean visible;

   private boolean jumping;
   private double gravity;
   private double jumpVel;



   public Player() {

      ImageIcon playerIcon = new ImageIcon(this.getClass().getResource(player));
      image = playerIcon.getImage();

      //setting the players dimensions
     width = image.getWidth(null);
      height = image.getHeight(null);


      //setting the initial player position and speed
     xPosition = 500;
      yPosition = 340;


      xSpeed = 1;
      ySpeed = 1;


      //setting visible
     visible = true;

      jumping = false;
      gravity = 6;
      jumpVel = 2;
   }

   @Override
   public void move(long timeDiff) {



      if(dPressed && xPosition < 760) {

         xPosition = (xPosition + ((xSpeed * timeDiff) / 1000));

      }
      if(aPressed && xPosition > 0) {
         xPosition = (xPosition - ((xSpeed * timeDiff) / 1000));

      }

      //jumping
     if(wPressed && !jumping) {

         jumping = true;

      }
      if(jumping) {

         yPosition = (yPosition - ((jumpVel * timeDiff) / 1000));
         jumpVel -= gravity;
      }
      if(yPosition > 340) {
         jumping = false;
         jumpVel = 2;
      }

   }


   @Override
   public void update(long timeDiff) {

      move(timeDiff);
   }

   @Override
   public Image getImage() {
      // TODO Auto-generated method stub
     return image;
   }

   @Override
   public double getXPosition() {
      // TODO Auto-generated method stub
     return xPosition;
   }

   @Override
   public double getYPosition() {
      // TODO Auto-generated method stub
     return yPosition;
   }

   @Override
   public boolean isVisible() {
      // TODO Auto-generated method stub
     return visible;
   }

   @Override
   public Rectangle getBounds() {
      // TODO Auto-generated method stub
     return new Rectangle((int)getXPosition(),(int)getYPosition(), width, height);
   }

   public void keyPressed(KeyEvent e) {

      int key = e.getKeyCode();

      if(key == KeyEvent.VK_W) {

         wPressed = true;

      }

      if(key == KeyEvent.VK_S) {

         sPressed = true;

      }

      if(key == KeyEvent.VK_A) {

         aPressed = true;

      }

      if(key == KeyEvent.VK_D) {

         dPressed = true;

      }


   }

   public void keyReleased(KeyEvent e) {

      int key = e.getKeyCode();

      if(key == KeyEvent.VK_W) {

         wPressed = false;
      }

      if(key == KeyEvent.VK_S) {

         sPressed = false;

      }

      if(key == KeyEvent.VK_A) {

         aPressed = false;

      }

      if(key == KeyEvent.VK_D) {

         dPressed = false;

      }
   }
}


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  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
171  
172  
173  
174  
175  
176  
177  
178  
179  
180  
181  
182  
183  
184  
185  
186  
187  
188  
189  
190  
191  
192  
193  
194  
195  
196  
197  
198  
199  
200  
201  
202  
203  
204  
205  
206  
207  
208  
209  
210  
211  
212  
213  
214  
215  
216  
217  
218  
219  
220  
221  
222  
223  
224  
225  
226  
227  
228  
229  
230  
231  
232  
233  
234  
235  
236  
237  
238  
239  
240  
241  
242  
243  
244  
245  
246  
247  
248  
249  
250  
251  
252  
253  
254  
255  
256  
257  
258  
package platformer;

import java.applet.AudioClip;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferStrategy;
import java.util.ArrayList;

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


public class Game extends Canvas implements Runnable {

   private BufferStrategy bufferStrategy;

   private Thread animator;

   private Player player;
   private ArrayList<Bomb> bombs;

   private final int DELAY = 20;

   private boolean gamePlaying;
   private boolean gameOver;
   private boolean introScreen;

   private Image grass;

   private AudioClip jumpSound;


   public Game() {

      JFrame container = new JFrame("Platformer");



      JPanel panel = (JPanel)container.getContentPane();
      panel.setPreferredSize(new Dimension(800,400));
      panel.setLayout(null);



      setBounds(0,0,800,600);
      panel.add(this);

      //stop AWT from calling repaint (im painting myself!)
     setIgnoreRepaint(true);


      container.pack();
      container.setResizable(false);
      container.setVisible(true);


      //add the listener so we can get key presses
     addKeyListener(new KAdapter());



      requestFocus();

      player = new Player();
      initBombs();


      gamePlaying = true;
      gameOver = false;
      introScreen = false;

      //create 2 buffers and get the painting strategy
     createBufferStrategy(2);
      bufferStrategy = getBufferStrategy();

      //create and start the animation thread
     animator = new Thread(this);
      animator.start();

      //make sure the program exits when the window is closed
     container.addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent e) {
            System.exit(0);
         }
      });

      ImageIcon ii = new ImageIcon(this.getClass().getResource("grass.png"));
      grass = ii.getImage();



   }

   private void initBombs() {

      bombs = new ArrayList<Bomb>();

      for (int i=0; i<BombLocations.bombPositions.length; i++ ) {
         bombs.add(new Bomb(BombLocations.bombPositions[i][0], BombLocations.bombPositions[i][1]));
      }

   }

   @Override
   public void run() {

      while(true) {

         long beforeTime = 0, timeDiff, sleep;



         if(gamePlaying) {

            timeDiff = System.currentTimeMillis() - beforeTime;
            beforeTime = System.currentTimeMillis();


            player.update(timeDiff);

            for(Bomb bomb : bombs) {
               bomb.update(timeDiff);
            }

            collisionDetection();
            renderGame();


            sleep = DELAY - (System.currentTimeMillis() - beforeTime);

            if (sleep < 0)
               continue;
            try {
               Thread.sleep(sleep);
            } catch (InterruptedException e) {
               System.out.println("interrupted");
            }
         }

         if(introScreen) {

            renderIntroScreen();

         }

         if(gameOver) {

            gameOver();
         }
      }
   }

   public void renderGame() {

      Graphics2D g = (Graphics2D) bufferStrategy.getDrawGraphics();
      g.setColor(Color.gray);
      g.fillRect(0,0,800,400);



      if(player.isVisible()) {
         g.drawImage(player.getImage(), (int)player.getXPosition(),(int) player.getYPosition(), null);
      }
      for(Bomb bomb : bombs ) {
         if(bomb.isVisible()) {
            g.drawImage(bomb.getImage(), (int)bomb.getXPosition(), (int)bomb.getYPosition(), null);
         }
      }

      g.drawImage(grass, 0, 350, null);

      g.dispose();
      bufferStrategy.show();


   }

   public void gameOver() {

      Graphics2D g = (Graphics2D) bufferStrategy.getDrawGraphics();
      g.setColor(Color.black);
      g.fillRect(0,0,800,400);

      g.setFont(new Font("Courier", Font.BOLD, 25));
      g.setColor(Color.white);
      g.drawString("GAME OVER", 325, 200);

      g.dispose();
      bufferStrategy.show();


   }

   public void renderIntroScreen() {

      String introMessage = "Avoid the bombs!" + "\nUse A to go left" + "\nUse D to go right" +
            "\nUse W to Jump" + "\n\nPress ENTER to begin!";
     

      Graphics2D g = (Graphics2D) bufferStrategy.getDrawGraphics();
      g.setColor(Color.black);
      g.fillRect(0,0,800,400);

      g.setFont(new Font("Courier", Font.BOLD, 25));
      g.setColor(Color.white);
      g.drawString(introMessage, 325, 200);

      g.dispose();
      bufferStrategy.show();




   }

   public void collisionDetection() {

      Rectangle playerBounds = player.getBounds();

      for(Bomb bomb : bombs) {

         Rectangle bombBounds = bomb.getBounds();
         if(bombBounds.intersects(playerBounds)) {
            gamePlaying = false;
            gameOver = true;
         }

      }

   }

   private class KAdapter extends KeyAdapter {

      public void keyPressed(KeyEvent e) {

         player.keyPressed(e);


      }
      public void keyReleased(KeyEvent e) {

         player.keyReleased(e);


      }
     
   }
}


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

import java.awt.Image;
import java.awt.Rectangle;

import javax.swing.ImageIcon;

public class Bomb implements Entity {

   private String bomb = "bomb.png";

   //x and y positions for the bomb
  private double xPosition;
   private double yPosition;

   //x and y speeds for the bomb
  private double xSpeed;
   private double ySpeed;
   
   private int BOMB_VELOCITY;

   //width and height of the bomb
  private int width;
   private int height;

   private Image image;

   private boolean visible;

   private boolean jumping;
   private double gravity;
   private double jumpVel;

   public Bomb(double xPosition, double yPosition) {

      ImageIcon bombIcon = new ImageIcon(this.getClass().getResource(bomb));
      image = bombIcon.getImage();

      //setting the bomb dimensions
     width = image.getWidth(null);
      height = image.getHeight(null);


      //setting the initial bomb position and speed
     this.xPosition = xPosition;
      this.yPosition = yPosition;


      xSpeed = 1;
      ySpeed = 100;
     
      BOMB_VELOCITY = 2;


      //setting visible
     visible = true;

      jumping = false;
      gravity = 6;
      jumpVel = 750;
   }


   @Override
   public void move(long timeDiff) {
     
      if(xPosition > 740) {

         xSpeed = -(BOMB_VELOCITY);
           
      }
      if(xPosition < 0) {

         xSpeed = BOMB_VELOCITY;
           
      }
     
      xPosition += xSpeed;
     
         

      if(!jumping) {

         jumping = true;

      }
      if(jumping) {

         yPosition = (yPosition - (jumpVel/250));
         jumpVel -= gravity;
      }
      if(yPosition > 350) {
         jumping = false;
         jumpVel = 750;
      }

   

   }

   @Override
   public void update(long timeDiff) {
      move(timeDiff);

   }

   @Override
   public Image getImage() {
      // TODO Auto-generated method stub
     return image;
   }

   @Override
   public double getXPosition() {
      // TODO Auto-generated method stub
     return xPosition;
   }

   @Override
   public double getYPosition() {
      // TODO Auto-generated method stub
     return yPosition;
   }

   @Override
   public boolean isVisible() {
      // TODO Auto-generated method stub
     return visible;
   }

   @Override
   public Rectangle getBounds() {
      // TODO Auto-generated method stub
     return new Rectangle((int)xPosition, (int)yPosition, width, height);
   }

}


1  
2  
3  
4  
5  
6  
7  
8  
9  
package platformer;

public class BombLocations {
   
   protected static double[][] bombPositions =
     
   { {-50.0, 200.0}, {850.0, 175.0}};

}



The idea is to have an array of bombs and repeatedly add more every 10 seconds or so, when you get hit its over. What frustrates me is that I had the move methods for all my entities taking in the delta from the update loop and moving based on that. That worked well for the player but for the bombs jumping it totally broke, so I just hard coded in a value of 250 to get it to work. I think Im just still getting tripped up on the update times in the run method. I fixed what you said, now the bombs move nice and  smooth, but the players broken.
Offline jparril1

Junior Member





« Reply #23 - Posted 2011-03-13 19:46:32 »

I tested the value of timeDiff, and it winds up being a HUGE number. So when I pass it to the updates, multiply it by the speed, and divide that by 1000, I still get a gigantic number. Which explains why my movement is broken. So am I calculating timeDiff incorrectly? Or even more importantly, is timeDiff the value I should be giving to my update methodS?
Offline ra4king

JGO Kernel


Medals: 345
Projects: 2
Exp: 5 years


I'm the King!


« Reply #24 - Posted 2011-03-13 20:24:21 »

Hahaha your mistake was re-initializing beforeTime, timeDiff, and sleep inside your while loop.
Put
1  
long beforeTime = 0, timeDiff, sleep;

right before while(true) {
Tongue

EDIT: Running your code, the player still didn't move. Looking through the code, I found the problem to be in your move() in your Player class. Because timeDiff is a long, diving that by 1000 (an int) will return a long. timeDiff will be well below 1000 so dividing that by 1000 will return 0. You need to cast timeDiff into a double before dividing by 1000.
1  
xPosition += xSpeed * ((double)timeDiff/1000);

Offline jparril1

Junior Member





« Reply #25 - Posted 2011-03-13 21:45:00 »

AHHH Gothca.. Ok so should it be

xPosition = (xPosition + ((xSpeed * (double)timeDiff) / 1000 ));

or 


xPosition = (xPosition + (xSpeed * ((double)timeDiff) / 1000));


basically, should I multiply xSpeed and timediff then divide that result by 1000, or should I divide timediff by 1000, then multiply that result by xSpeed?
Offline ra4king

JGO Kernel


Medals: 345
Projects: 2
Exp: 5 years


I'm the King!


« Reply #26 - Posted 2011-03-13 23:13:20 »

Both are the same thing but stick with dividing first.

Offline ReBirth
« Reply #27 - Posted 2011-03-14 11:07:06 »

If your xPosition is already defined as double, them both are the same. However if not and you cast it with (double) to keep the value, divide first then cast it to double and multiply it. That what I do to keep the line easy to read by other.

Offline jparril1

Junior Member





« Reply #28 - Posted 2011-03-14 14:23:58 »

Thanks guys. Its working pretty well now. I just seem to be having an issue where when I run it from eclipse it looks ok. But I exported it as a Runnable jar to put on my website and opened it on another computer, the painting was off by about 10 pixels. Basically the right and bottom of the screen wasnt getting painted, it seemed like the rectangle that I was blanking the screen out with every loop was offset by a bit. Im guessing this is because Im using hard coded coordinates 800X400, and instead I should be using some sort of method that gets the size directly from the component? Im thinking this has to do with differences in the Frames on different operating systems.

Other than that, I just need to figure out how to get a sound clip to play when I press a button, and Im good to go
Offline ra4king

JGO Kernel


Medals: 345
Projects: 2
Exp: 5 years


I'm the King!


« Reply #29 - Posted 2011-03-14 17:33:12 »

Could you show us your example of that painting offset?

There is a getAudioClip(URL url) method in Applet Smiley
http://ra4king.is-a-geek.net/javadocs/java/applet/Applet.html

Pages: [1] 2
  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.

TehJavaDev (13 views)
2014-08-28 18:26:30

CopyableCougar4 (25 views)
2014-08-22 19:31:30

atombrot (38 views)
2014-08-19 09:29:53

Tekkerue (31 views)
2014-08-16 06:45:27

Tekkerue (32 views)
2014-08-16 06:22:17

Tekkerue (19 views)
2014-08-16 06:20:21

Tekkerue (29 views)
2014-08-16 06:12:11

Rayexar (66 views)
2014-08-11 02:49:23

BurntPizza (42 views)
2014-08-09 21:09:32

BurntPizza (34 views)
2014-08-08 02:01:56
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!