Page 3 of 5 FirstFirst 12345 LastLast
Results 41 to 60 of 85
Like Tree1Likes

Thread: i want write 2d java game

  1. #41
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,448
    Rep Power
    25

    Default Re: i want write 2d java game

    Continue looking to find it.

  2. #42
    Join Date
    Oct 2011
    Posts
    50
    Rep Power
    0

    Default Re: i want write 2d java game

    is it possible to error in another class?

  3. #43
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,448
    Rep Power
    25

    Default Re: i want write 2d java game

    Yes you can get errors in any and all classes.

  4. #44
    Join Date
    Oct 2011
    Posts
    50
    Rep Power
    0

    Default Re: i want write 2d java game

    i solved it. but now move function doesn't work.

    Java Code:
      public void run() {
        	  
        	  while(ingame){     		  
        		  plane.move();
        		  repaint();
               }
      }
    i set ingame to true at beginning of code.

  5. #45
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,448
    Rep Power
    25

    Default Re: i want write 2d java game

    move function doesn't work.
    Please explain what "doesn't work" means.

    Add printlns to the code to show the execution flow.

  6. #46
    Join Date
    Oct 2011
    Posts
    50
    Rep Power
    0

    Default Re: i want write 2d java game

    plane doesn't move. move function or repaint function doesn't work. i write println in the while loop and it prints infinitely

  7. #47
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,448
    Rep Power
    25

    Default Re: i want write 2d java game

    move function or repaint function doesn't work.
    What does the move() method do? Is it called?
    Your infinite loop could be preventing the GUI from getting control so it can call the paint method.

    Is the paint/paintComponent method being called? If not, then you must exit the loop and the method and return control to the JVM so it can call the paint method.

  8. #48
    Join Date
    Oct 2011
    Posts
    50
    Rep Power
    0

    Default Re: i want write 2d java game

    move function get new x and y values of plane. where should i call these function?

    i look an example code,it writes:

    Java Code:
      public void run() {
    
            long beforeTime, timeDiff, sleep;
    
            beforeTime = System.currentTimeMillis();
    
            while (ingame) {
                repaint();
                animationCycle();
    
                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();
            }
            gameOver();
        }
    i write move and repaint in actionperformed when i used actionlistener
    Last edited by captain_turkiye; 12-12-2011 at 07:47 PM.

  9. #49
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,448
    Rep Power
    25

    Default Re: i want write 2d java game

    If your code is using the GUI's thread it should set the locations for paint to use when it draws, call repaint() and then exit the method.
    The JVM will call the paint method which will draw at that location and exit.
    You need a Timer to call your code where you update the location to a new location, call repaint and exit.
    This is a big loop thru your code, the JVM and the paint method.

    If your code is using its own thread, it can work like the code you posted.

  10. #50
    Join Date
    Oct 2011
    Posts
    50
    Rep Power
    0

    Default Re: i want write 2d java game

    i call thread in my constructor like this:

    Java Code:
    thread = new Thread(this);
            thread.start();

  11. #51
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,448
    Rep Power
    25

    Default Re: i want write 2d java game

    Yes that looks like it would create and start a thread executing.

  12. #52
    Join Date
    Oct 2011
    Posts
    50
    Rep Power
    0

    Default Re: i want write 2d java game

    then my code is correct. but it doesn't work.

  13. #53
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,448
    Rep Power
    25

    Default Re: i want write 2d java game

    I can not say that your code is correct. You have only shown very small, disconnected parts of it.
    Because it "doesn't work" it must not be correct.

    You need to debug your code by adding lots of printlns to see where it executes and show the values of variables.
    For example do you know that the paint method is being called often? Are the x,y values being changed correctly each time?

  14. #54
    Join Date
    Oct 2011
    Posts
    50
    Rep Power
    0

    Default Re: i want write 2d java game

    it doesn't take key events. i use keyAdapter
    Java Code:
    public class adapter extends KeyAdapter {
    
        	public void keyPressed(KeyEvent e) {
                plane.keyPressed(e);
            }
        	
            public void keyReleased(KeyEvent e) {
                plane.keyReleased(e);
            }
    
            
        }
    in constructor:

    addKeyListener(new adapter());

  15. #55
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,448
    Rep Power
    25

    Default Re: i want write 2d java game

    Sorry, that code can not show what is wrong with your program.

    it doesn't take key events. i use keyAdapter
    This makes no sense.
    All the methods in the code you've shown take KeyEvent objects.

  16. #56
    Join Date
    Oct 2011
    Posts
    50
    Rep Power
    0

    Default Re: i want write 2d java game

    keypressed and keyrelease at in the other class. it worked this way when i used actionlistener. i think it is much easier than this way. can i complete game with actionperformed and timer?

  17. #57
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,448
    Rep Power
    25

    Default Re: i want write 2d java game

    can i complete game with actionperformed and timer?
    I have not enough information to say anything about your completing the game.
    Can you explain what you are using the Timer for?

  18. #58
    Join Date
    Oct 2011
    Posts
    50
    Rep Power
    0

    Default Re: i want write 2d java game

    i use timer for call actionPerformed method. it call move, shot for plane and call repaint. i want add enemy planes.

  19. #59
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,448
    Rep Power
    25

    Default Re: i want write 2d java game

    That sounds like it should work.

  20. #60
    Join Date
    Oct 2011
    Posts
    50
    Rep Power
    0

    Default Re: i want write 2d java game

    actionperformed and timer is working like thread and run, isn't it? what is the difference?

Page 3 of 5 FirstFirst 12345 LastLast

Similar Threads

  1. Replies: 7
    Last Post: 11-30-2011, 06:58 PM
  2. trying to write a program for hangman word game
    By durdanto in forum New To Java
    Replies: 1
    Last Post: 02-12-2011, 02:53 AM
  3. How to write it in Java
    By agajantorayev in forum New To Java
    Replies: 8
    Last Post: 08-11-2010, 07:37 AM
  4. how to write except in java??
    By napi1234 in forum New To Java
    Replies: 1
    Last Post: 08-09-2010, 04:49 AM
  5. How do i write this in java...?
    By johnny7white in forum New To Java
    Replies: 1
    Last Post: 11-28-2007, 07:05 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •