Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (108)
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  
  Movement is acting a bit odd  (Read 396 times)
0 Members and 1 Guest are viewing this topic.
Offline RyanMB97

Junior Member


Medals: 1
Projects: 1



« Posted 2013-05-14 19:36:02 »

Hello! I haven't used this forum that much besides browsing, but I find myself in a bit of a spot here. In a game I'm working on, the character moves around according to "Speed * game.delta", which is what it's supposed to be. The problem (and this is mostly a preference thing) is that when my character moves either up or left, he goes slightly faster than right or down, but it might just be appearances. I was just hoping that someone might be able to help me out here.

Github to Code: https://github.com/RyanMB97/Acreage
Classes that might be at fault:
Tiles themselves (DirtTile, GrassTile, StoneTile, etc) in respective classes
Player movement (Player class)
How the level is drawn/updated (Level class)
My own coding style?

Code Itself:
From the "Player" class
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  
public void tick(Game game) {
      this.game = game;
      bounding.setBounds(x, y, width, height);
      speed = 2 * game.delta;

      worldEdgeCollision();

      movement();
   }

   private void movement() {

      if (game.input.left.down && canLeft) {
         game.xOffset -= speed;
         directionFacing = 2;
         System.out.println(speed);
      }
      if (game.input.right.down && canRight) {
         game.xOffset += speed;
         directionFacing = 3;
         System.out.println(speed);
      }
      if (game.input.up.down && canUp) {
         game.yOffset -= speed;
         directionFacing = 1;
         System.out.println(speed);
      }
      if (game.input.down.down && canDown) {
         game.yOffset += speed;
         directionFacing = 0;
         System.out.println(speed);
      }
   }


How I Render Tiles in "Level" class
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
public void renderLevel(Graphics g) {
      // Tile loops
     for (int y = 0; y < game.worldHeight; y++) {
         for (int x = 0; x < game.worldWidth; x++) {
            if (tileArray[x][y].x >= game.player.x - (game.getWidth() / 2) - 32 && tileArray[x][y].x <= game.player.x + (game.getWidth() / 2) + 32 & tileArray[x][y].y >= game.player.y - (game.getHeight() / 2) - 32 && tileArray[x][y].y <= game.player.y + (game.getHeight() / 2) + 32) {
               tileArray[x][y].render(g);
            }
         }
      } // End loops
  } // End render


The rendering method itself is a bit odd, since I tossed it together on the spot. In brief explanation, it doesn't render tiles that are off of the viewable screen, plus 1 more for more seamless viewing while moving.

An example of a tile being drawn, "DirtTile" class
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
public void tick(Game game) {
      this.game = game;

      x = oX - game.xOffset; // Current x after movement, Offset, etc
     y = oY - game.yOffset; // Current y after movement, Offset, etc
     bounding = new Rectangle(x, y, size, size);
      bounding.setBounds(x, y, size, size);
   }

   public void render(Graphics g) {
      g.drawImage(game.res.tiles[tileID], x, y, game);

      if (game.showGrid) { // If the player wants to draw grids
        g.setColor(Color.WHITE); // White color
        g.drawRect(x, y, size - 1, size - 1); // Draw a border around tile
     }

      if (showBorders) { // If it is allowed to show borders
        g.setColor(Color.BLACK); // White color
        g.drawRect(x, y, size - 1, size - 1); // Draw a border around image
     }
   }


Sorry if the thread is a bit long, but this is really bothering me. Also, any advice or information to anything not of this topic is much appreciated. Thanks in advance for any and all responses!
Offline wreed12345

JGO Knight


Medals: 24
Projects: 2
Exp: 2 years


http://linebylinecoding.blogspot.com/


« Reply #1 - Posted 2013-05-15 02:18:25 »

Since your calling
1  
System.out.println(speed);


how does the speed compare when holding down each key?

Offline RyanMB97

Junior Member


Medals: 1
Projects: 1



« Reply #2 - Posted 2013-05-15 21:27:59 »

Sorry for the later response, just got home from a trip. Anyways, They don't vary by that much. It might just be a graphical component or just myself. Here are some read-outs:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
2.000003600087018 Up
2.000027040088614 Up
2.0000108401326906 Right
2.0000342801343614 Right
2.000021600135952 Right
2.0000198801734625 Down
2.0000072001750118 Down
2.0000306401766097 Down
2.000009040234559 Left
2.0000324802362086 Left


This is using the speed * delta stuff, hence the variety. It's not super-important, so I'm not stressing. But I'd prefer to get this out of the way, and I have no idea how to go about doing so. Again, thanks in advance.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline wreed12345

JGO Knight


Medals: 24
Projects: 2
Exp: 2 years


http://linebylinecoding.blogspot.com/


« Reply #3 - Posted 2013-05-16 02:31:53 »

From that it looks like everything should be going evenly. Would you mind sending me a link to the .jar to try it out?

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.

CogWheelz (16 views)
2014-07-30 21:08:39

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

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

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

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

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

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

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

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

Riven (31 views)
2014-07-23 20:56:16
List of Learning Resources
by SilverTiger
2014-07-31 13:54:12

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
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!