Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (499)
Games in Android Showcase (118)
games submitted by our members
Games in WIP (568)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
   Home   Help   Search   Login   Register   
  Show Posts
Pages: [1]
1  Game Development / Newbie & Debugging Questions / Re: getX() inaccurate for mouselistener? on: 2014-01-07 20:45:51
Thanks a lot. I had a feeling it was something small and stupid that I overlooked. Got it all fixed up though.
2  Game Development / Newbie & Debugging Questions / getX() inaccurate for mouselistener? on: 2014-01-07 17:52:59
So I'm using a pretty standard JPanel design for my game. I'm trying to test out using mouselisteners, but the getX() and getY() methods both seem to return inaccurate coordinates. When I draw a test string out on the screen at say, (100,100), and then click the on the area around the string, the getX and getY is returning values almost 100 pixels larger than what it should be. Does anyone know what I'm doing wrong exactly? I'll post some 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  
131  
132  
133  
134  
135  
136  
137  
138  
139  
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.event.*;

import javax.swing.JPanel;

import managers.Keys;
import managers.Mouse;

public class Game extends JPanel
   implements Runnable, KeyListener, MouseListener{
   
   // dimensions
  public static final int WIDTH = 480;
   public static final int HEIGHT = 320;
   public static final int SCALE = 2;
   
   // game thread
  private Thread thread;
   private boolean running;
   private int FPS = 60;
   private long targetTime = 1000 / FPS;
   
   // image
  private BufferedImage image;
   private Graphics2D g;
   
   
   public Game() {
      super();
      setPreferredSize(
         new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
      setFocusable(true);
      requestFocus();
   }
   
   public void addNotify() {
      super.addNotify();
      if(thread == null) {
         thread = new Thread(this);
         addKeyListener(this);
         addMouseListener(this);
         thread.start();
      }
   }
   
   private void init() {
     
      image = new BufferedImage(
               WIDTH, HEIGHT,
               BufferedImage.TYPE_INT_RGB
            );
      g = (Graphics2D) image.getGraphics();
     
      running = true;
     
   }
   
   public void run() {
     
      init();
     
      long start;
      long elapsed;
      long wait;
     
      // game loop
     while(running) {
         
         start = System.nanoTime();
         
         update();
         draw();
         drawToScreen();
         
         elapsed = System.nanoTime() - start;
         
         wait = targetTime - elapsed / 1000000;
         if(wait < 0) wait = 5;
         
         try {
            Thread.sleep(wait);
         }
         catch(Exception e) {
            e.printStackTrace();
         }
         
      }
     
   }
   
   private void update() {
      Keys.update();
   }
   private void draw() {
      gsm.draw(g);
      g.drawString("point",100,100);
   }
   private void drawToScreen() {
      Graphics g2 = getGraphics();
      g2.drawImage(image, 0, 0,
            WIDTH * SCALE, HEIGHT * SCALE,
            null);
      g2.dispose();
   }
   
   public void keyTyped(KeyEvent key) {}
   public void keyPressed(KeyEvent key) {
      if(key.isControlDown()) {

      }
      Keys.keySet(key.getKeyCode(), true);
   }
   public void keyReleased(KeyEvent key) {
      Keys.keySet(key.getKeyCode(), false);
   }

   public void mouseClicked(MouseEvent e) {
      //System.out.print(e.getX());
       
   }


   public void mouseEntered(MouseEvent arg0) {
     
   }

   public void mouseExited(MouseEvent arg0) {
   }

   public void mousePressed(MouseEvent e) {
      System.out.println(e.getX());
   }

   public void mouseReleased(MouseEvent arg0) {
     
   }
}
 
3  Game Development / Newbie & Debugging Questions / Re: not detecting collision with keybindings? on: 2013-10-03 20:50:29
Alright thanks a lot. I checked it out and have a few questions because I can be super slow sometimes. So would the AABB in the explanation be like the player and the blocks in my game? Do I find the center and size of them based on the universal coordinates?
4  Game Development / Newbie & Debugging Questions / Re: not detecting collision with keybindings? on: 2013-10-03 20:32:37
The player stays at the same screen coordinates.
5  Game Development / Newbie & Debugging Questions / Re: not detecting collision with keybindings? on: 2013-10-03 20:18:08
Sorry I don't know if this was a mis-communication or something, but yeah the player stays at the same coordinates on the screen constantly, while other blocks and the background are moving. Like, I dunno, Super Mario I believe?
6  Game Development / Newbie & Debugging Questions / Re: not detecting collision with keybindings? on: 2013-09-27 20:07:40
Well, could you explain some about your code? Like what the for loop does, what it iterates, what the offset is, etc.

Yeah sure. So I have a text file that has the test level saved into it. The Level class reads the file and adds blocks to an arraylist. Each block contains its own coordinates. The player is stationary in my game, so I use the offsetX to scroll every object that isn't the player, including the background and everything. So the iteration that I posted goes through the arraylist to check and see if the player is making contact with any block. If it isn't, then the offset is changed to scroll the objects in whatever direction around the player. I hope this explanation was informative enough.
7  Game Development / Newbie & Debugging Questions / Re: not detecting collision with keybindings? on: 2013-09-27 14:55:29
Hey thanks a whole lot! This is doing the trick for right now, so I hope it'll continue to work. I know I'm probably setting up my movement in the least efficient way possible, but I actually have an arraylist of all blocks in the level and a for-loop statement that checks to see if the player's min x is touching the max y of each block inside the move method you just posted.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
public void mover(float movex, float movey) {
        // Hypothetical positions that you could be in
       int collisionx = (int)(offsetX+movex);
        int collisiony = (int)(offsetY+movey);

        for(int i=0;i<level1.Blocks.size();i++){
           if (Math.abs(p1.minX) < Math.abs(level1.Blocks.get(i).maxX)){
              moving=false;
           }
        }
        if (moving==true&&offsetX!=2){
           offsetX = (int)collisionx;
        }
        if (moving==false){
           offsetX = 0;
        }
    }


It's working for my purposes right now, but I'd really appreciate feedback on how to make it more, I don't know, professional I guess?
8  Game Development / Newbie & Debugging Questions / not detecting collision with keybindings? on: 2013-09-26 20:00:47
So in my little side-scroller Java game I'm attempting to make collisions for my character against walls. I am using keybindings instead of key listeners because of the focusing issues it often caused. Here's the code I'm having trouble with:

1  
2  
3  
4  
5  
6  
7  
8  
 Action moveleft = new AbstractAction(){
      public void actionPerformed(ActionEvent e)
      {
          if (moving==true&&offsetX!=2){
              offsetX=offsetX+2;
          }
      }
    };

The problem is that when a key is held down to make the character move, in the actionperformed method, it doesn't recheck the conditional statement that determines whether moving is true or false when the key is held down. This results in the character moving indefinitely through all walls until the key is released.

So why is this? What can I do to remedy it? If any more code is required from my program I can provide! Thanks!
9  Game Development / Newbie & Debugging Questions / Re: removing objects when off-screen on: 2013-09-09 19:37:49
I'm sorry, but I'm not familiar with the term AABB. Could I get a quick explanation or something?
Thanks!

AABB = Axis Aligned Bounding Box

Easy system to implement, because each box just has a x/y and width/height  and you just check those things
x and x+width,  y and y+height  to the others and make sure they aren't overlapping/intersecting.


Basically rectangles that can't be rotated.   Grin



Ah! Seems simple enough! This is what I was planning on doing anyway, just didn't know it was named that. Thanks!
10  Game Development / Newbie & Debugging Questions / Re: removing objects when off-screen on: 2013-09-09 18:48:26
Load the entire level all at once. It will fit in memory unless your game is much, much bigger than I imagine it is. 1000 blocks should be around the order of several kilobytes. A megabyte is 1000 times larger than that and a gigabyte of RAM is 1000 megabytes of RAM.

Alright thanks, I really wasn't sure if this was a bad practice or not, but my game probably will not ever get very large.

Collision detection and drawing will be the problem if you try to do it all at once. (Especially since collision detection could be O(n^2).) To narrow down the number of items to visible and nearby items, test if each items AABB overlaps a rectangle slightly larger than the visible area of the level. Items won't need to be discarded until the end of the level. You can leave them wherever they are when they go outside the rectangle or reset their position to where they started. You could sort items by their x position or divide the level into grids/chunks, but it might not matter.


I'm sorry, but I'm not familiar with the term AABB. Could I get a quick explanation or something?

(Awesome name btw.)

Thanks!
11  Game Development / Newbie & Debugging Questions / Re: removing objects when off-screen on: 2013-09-08 04:57:25
Add this code to every object which can go left the screen in their update code.

1  
2  
3  
4  
5  
public void update()
{
    if (x <= -tileWidth)
        destroy();
}

That is when each object moves out of screen from left, we destroy them.
Yeah, I've actually got this figured out. What I need to know is how to recreate those objects if the player were to go backwards without re-drawing every single object that is already on the screen.  Undecided
12  Game Development / Newbie & Debugging Questions / Re: removing objects when off-screen on: 2013-09-08 04:42:14
Wow that's a pretty great idea! Much simpler than anything I was thinking of. I could see the amount of files getting out of hand if the game's size (in terms of content) were to increase dramatically, but I definitely will consider this! Thanks!
13  Game Development / Newbie & Debugging Questions / removing objects when off-screen on: 2013-09-08 04:09:46
So I'm making a little side-scrolling platformer in Java right now as some practice. I generate my levels from text files with different numbers signifying different types of blocks and objects in the game world, as I often see people do on the internet. My problem is that when I start the game, all individual walls and other objects are all created at the same time in an arraylist. If my levels were to get much larger than they currently are, I can see having thousands of instances of the same object existing off-screen would be very inefficient. It would be preferable if I could only create blocks and enemies at a certain distance away from the player as it approaches them, and when they exceed that distance, they would be removed from the arraylist. I've been able to remove the objects when they go off screen so far, but not re-draw them again. So this leads me to my question: How would I go about creating only the objects that I need on the fly rather than creating them all at once when the game starts up like I'm currently doing. If anyone needs to see any code, that's cool, I can show, or if you wanna just give me a short explanation, that's fine too. Any help would be greatly appreciated!

Also if any of my Java jargon is incorrect above, please correct me! I'm still quite a novice! Grin
14  Game Development / Newbie & Debugging Questions / Re: My game project freezing when I run it? on: 2013-04-27 03:05:15
Did that help at all?
15  Game Development / Newbie & Debugging Questions / Re: My game project freezing when I run it? on: 2013-04-27 01:58:05
enemy: http://pastebin.com/5PadEpTv
map: http://pastebin.com/jcJAyian
fireball: http://pastebin.com/w31QSJUJ
goal: http://pastebin.com/ectSyUfP

There's the rest! Thanks a lot!
16  Game Development / Newbie & Debugging Questions / Re: My game project freezing when I run it? on: 2013-04-27 01:09:16
I mean, I don't press anything until it's fully loaded though.  Clueless
17  Game Development / Newbie & Debugging Questions / Re: My game project freezing when I run it? on: 2013-04-27 00:58:12
Whoops sorry. Here's the pastebin to my player class:

http://pastebin.com/8NhSXwLj

18  Game Development / Newbie & Debugging Questions / Re: My game project freezing when I run it? on: 2013-04-27 00:30:11
Can no one help me with this???
19  Game Development / Newbie & Debugging Questions / My game project freezing when I run it? on: 2013-04-26 17:43:36
Hey guys, I've posted this question on a number of different places, but no one could help me. Someone finally recommended that I try here.

I've been diligently working on a 2-D tile-based Java game as a project for my Java class, and it's been a lot of fun. Everything is going very well so far, but a pretty big problem that I'm having is that when I run my game, it will start frozen about 50% of the time, as the player won't react to any keys pressed or anything like it should. Sometimes it works and sometimes it doesn't.  I have a feeling it is happening in my game panel class, because everything seems to function as intended when the game DOES run correctly, so I'll post that class. I also apologize for anything strange or unconventional in my code, I'm still kind of an amateur at coding so if clarification is needed I will supply it.

tl;dr: Why does my game freeze when I run it sometimes?

here's my panel 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  
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  
259  
260  
261  
262  
263  
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.Timer;

public class panel extends JPanel implements KeyListener, ActionListener
{
    //game variables
   private boolean running = false;
    private boolean mapFinished = false;
    player p1;
   
    //panel variables
   static final int Width = 480;
    static final int Height = 432;
    static final Dimension dim = new Dimension(Width,Height);
   
    //maps
   map map1;
    map map2;
    map map3;
    map map4;
    map end;
   
    enemy e;
   
    boolean map1Finished;
    boolean map2Finished;
    boolean map3Finished;
    boolean map4Finished;
   
    //drawing variables
   private BufferedImage image;
    private Graphics g;
    goal ng;
    private Image levelImage;
   
    private boolean map2Spawn;
    private boolean map3Spawn;
    private boolean map4Spawn;
    fireball fb;
    private boolean fireExist=false;
    private boolean gameOver=false;
    Timer loopTimer;
   
    public panel(){
        map1 = new map("tester.txt");
        map2 = new map("tester2.txt");
        map3 = new map("tester3.txt");
        map4 = new map("tester4.txt");
        end = new map("gameover.txt");
        p1 = new player(map1);
        ng = new goal(map1);
        e = new enemy(map1);
        setPreferredSize(new Dimension(Width,Height));
        setFocusable(true);
        requestFocus();
        fb=null;
        this.addKeyListener(this);
   loopTimer = new Timer(10, this);
   loopTimer.start();
    }
   
    @Override
    public void actionPerformed(ActionEvent evt){
   if (!running){
      startGame();
   }
   gameUpdate();
   repaint();
}
   
    private void gameUpdate(){
        if(gameOver==true){
            running=false;
        }
        p1.update();
        e.getPlayerCoord(p1.playerRec.x,p1.playerRec.y);
        e.update();
        if((p1.playerRec.x/48)==(ng.goalRec.x/48) && (p1.playerRec.y/48)==(ng.goalRec.y/48)){
            if(!map1Finished){
                map1Finished=true;
            }
            else if(map1Finished&&!map2Finished){
                map2Finished=true;
            }
            else if(map2Finished&&!map3Finished){
                map3Finished=true;
            }
            else if(map3Finished&&!map4Finished){
                map4Finished=true;
            }  
        }
        if(e.fireShot==true&&fireExist==false){
            fb=new fireball(e.fireX,e.fireY,e.fireDir);
            fireExist=true;
        }
        if(fireExist==true){
            if(fb.playerCol(p1.playerRec.x,p1.playerRec.y)==true){
                gameOver=true;
            }
        }
        fireWallCol();
        changeSpawn();
       
    }
   
    @Override
    public void paintComponent(Graphics g){
   super.paintComponent(g);
        g.setColor(Color.WHITE);
        g.fillRect(0,0,Width,Height);
        if(gameOver==true){
            end.draw(g);
            levelImage=new ImageIcon("sprites/gameover.png").getImage();
            g.drawImage(levelImage,100,100,null);
            //running=false;
       }
        if(gameOver==false){
            if (!map1Finished){
                map1.draw(g);
                p1.draw(g);
                e.draw(g);
                if (fireExist==true){
                    fb.draw(g);
                }
            }
       
            if (map1Finished&&!map2Finished){
                map2.draw(g);
                p1.draw(g);
                e.draw(g);
                if (fireExist==true){
                    fb.draw(g);
                }
                levelImage=new ImageIcon("sprites/level1.png").getImage();
                g.drawImage(levelImage,5,5,null);
            }
            if (map2Finished&&!map3Finished){
                map3.draw(g);
                p1.draw(g);
                e.draw(g);
                if (fireExist==true){
                    fb.draw(g);
                }
                levelImage=new ImageIcon("sprites/level2.png").getImage();
                g.drawImage(levelImage,5,5,null);
            }
            if (map3Finished&&!map4Finished){
                map4.draw(g);
                p1.draw(g);
                e.draw(g);
                if (fireExist==true){
                    fb.draw(g);
                }
                levelImage=new ImageIcon("sprites/level3.png").getImage();
                g.drawImage(levelImage,5,5,null);
            }
        }
        //g.drawString(""+map1.tileMap[1][7], 100, 100);
   }
   
   
    public void keyTyped(KeyEvent key) {}
   
    public void keyPressed(KeyEvent key) {
     
   int code = key.getKeyCode();  
   if(code == KeyEvent.VK_LEFT) {
            p1.setLeft(true);
   }
   if(code == KeyEvent.VK_RIGHT) {
            p1.setRight(true);
   }
   if(code == KeyEvent.VK_UP) {
            p1.setUp(true);
   }
   if(code == KeyEvent.VK_DOWN) {
            p1.setDown(true);
   }
    }
   
    public void keyReleased(KeyEvent key) {
    }
   
   
    public void startGame(){
        if (running == false){
            running = true;
        }
    }
   
    public void stopGame(){
        if (running == true)
        {
            running = false;
        }
    }
   
    public void changeSpawn(){
        if(map1Finished==true && map2Spawn==false){
            p1=new player(map2);
            ng=new goal(map2);
            e=new enemy(map2);
            if(fireExist==true){
                fireExist=false;
            }
            map2Spawn=true;
        }
       
        else if(map2Finished==true && map3Spawn==false){
            p1=new player(map3);
            ng=new goal(map3);
            e=new enemy(map3);
            if(fireExist==true){
                fireExist=false;
            }
            map3Spawn=true;
        }
        else if(map3Finished==true && map4Spawn==false){
            p1=new player(map4);
            ng=new goal(map4);
            e=new enemy(map4);
            if(fireExist==true){
                fireExist=false;
            }
            map4Spawn=true;
        }
    }
    public void fireWallCol(){
        if(fireExist==true){
            if(!map1Finished){
                    if(map1.tileMap[fb.fireRec.y/48][fb.fireRec.x/48]==1){
                        fb=null;
                        fireExist=false;
                        e.fireShot=false;
                    }
            }
            if(map1Finished&&!map2Finished){
                    if(map2.tileMap[fb.fireRec.y/48][fb.fireRec.x/48]==1){
                        fb=null;
                        fireExist=false;
                        e.fireShot=false;
                    }
            }
            if(map2Finished&&!map3Finished){
                    if(map2.tileMap[fb.fireRec.y/48][fb.fireRec.x/48]==1){
                        fb=null;
                        fireExist=false;
                        e.fireShot=false;
                    }
            }
        }    
    }
}
Pages: [1]
 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Pippogeek (40 views)
2014-09-24 16:13:29

Pippogeek (31 views)
2014-09-24 16:12:22

Pippogeek (21 views)
2014-09-24 16:12:06

Grunnt (47 views)
2014-09-23 14:38:19

radar3301 (29 views)
2014-09-21 23:33:17

BurntPizza (65 views)
2014-09-21 02:42:18

BurntPizza (37 views)
2014-09-21 01:30:30

moogie (43 views)
2014-09-21 00:26:15

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

BurntPizza (55 views)
2014-09-19 03:14:18
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!