Page 2 of 5 FirstFirst 1234 ... LastLast
Results 21 to 40 of 85
Like Tree1Likes

Thread: i want write 2d java game

  1. #21
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default Re: i want write 2d java game

    There are many threads executing when you execute your program.
    Some are used by the Timer class.
    Some can be created by you and they will use the run() method.

  2. #22
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default Re: i want write 2d java game

    Pretty sure the Timer runs automatically on the event dispatch thread
    The Event Dispatch Thread (The Java™ Tutorials > Creating a GUI With JFC/Swing > Concurrency in Swing)

  3. #23
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default Re: i want write 2d java game

    There are three Timer classes. Which one is the OP using?

  4. #24
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default Re: i want write 2d java game

    I presumed swings Timer due to the actionPerformed method.

  5. #25
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default Re: i want write 2d java game

    Sorry I don't understand. The Swing Timer class requires that you have an actionPerformed method for it to call.

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

    Default Re: i want write 2d java game

    i changed my code to thread to draw enemy planes. but it gives an error.

    Java Code:
    public class board extends JPanel implements Runnable
    {
    
        craft craft;
        Image myimage,myimage2;
        ImageIcon image,image2;
        int dx;
        boolean ingame=true;
        Thread thread;
        shot shot;
    
        public board() {
    
            addKeyListener(new adapter());
            image2 = new ImageIcon(getClass().getResource("background.jpg"));
            myimage2=image2.getImage();
            
            thread = new Thread(this);
            thread.start();
            
            gameInit(); 
        }
        
        public void addNotify() {
            super.addNotify();
            gameInit();
        }
    
        public void gameInit() {
            craft = new craft();   
            shot = new shot();
            thread = new Thread(this);
            thread.start();     
        }
    
        public void paintComponent(Graphics g) {
            
        	super.paintComponents(g);
        	g.drawImage(myimage2,0,0, null);
        	image = new ImageIcon(getClass().getResource("craft.jpg"));
        	myimage = image.getImage();
            g.drawImage(myimage, craft.getX(), craft.getY(),null);
    
        }	
          public void run() {
    
        	  while(ingame){
           		  craft.move();
        		  repaint();
        	  }
      }
    
    
        public class adapter extends KeyAdapter {
    
        	public void keyPressed(KeyEvent e) {
                craft.keyPressed(e);
            }
        	
            public void keyReleased(KeyEvent e) {
                craft.keyReleased(e);
            }
    
            
        }
    
    }

    Exception in thread "Thread-1" java.lang.NullPointerException
    at screen.run(screen.java:120)
    at java.lang.Thread.run(Unknown Source)

    120. line : craft.nove()

    i want adapt this code to my code.

    Space Invaders

  7. #27
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default Re: i want write 2d java game

    java.lang.NullPointerException
    at screen.run(screen.java:120)
    Your code uses a variable on line 120 that has a null value. Look at line 120 and find what variable has a null value and backtrack to see why that variable does not have a valid value.

    Use printlns to show the variables value when it is set and when it is used so you can see where the problem is.
    PhQ likes this.

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

    Default Re: i want write 2d java game

    Quote Originally Posted by Norm View Post
    Your code uses a variable on line 120 that has a null value. Look at line 120 and find what variable has a null value and backtrack to see why that variable does not have a valid value.

    Use printlns to show the variables value when it is set and when it is used so you can see where the problem is.
    in that line i call craft.move() which is move function of craft class. it is same as Space Invaders

  9. #29
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default Re: i want write 2d java game

    Does the variable: craft have a value of null?
    If so, you will get an exception.

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

    Default Re: i want write 2d java game

    i write craft = new craft(); what else can i do?

    or should i back to use timer?

  11. #31
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default Re: i want write 2d java game

    Are you still gettting this error: Exception in thread "Thread-1" java.lang.NullPointerException

    Or have you fixed it and the program is executing OK?

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

    Default Re: i want write 2d java game

    i am still getting error. program is starting, draw background and plane, but plane doesn't move.

  13. #33
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default Re: i want write 2d java game

    What error are you getting? If you do not post the error message we can't help.

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

    Default Re: i want write 2d java game

    i posted error in previous message, but here it is:

    Exception in thread "Thread-1" java.lang.NullPointerException
    at screen.run(screen.java:116)
    at java.lang.Thread.run(Unknown Source)


    116 line is : craft.move()

    can i use actionListener and threads together. the code with actionListener is work without enemies... etc. it just move and shot.

  15. #35
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default Re: i want write 2d java game

    ava.lang.NullPointerException
    at screen.run(screen.java:116)
    The error message says there is a null variable being used on line 116. What variable on line 116 is null?
    If 116 line is : craft.move()
    Then the variable: craft must be null. If you can't see it, then add a println just before line 116 that prints the value of craft to show it.

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

    Default Re: i want write 2d java game

    print this, what is this?

    craft@26d607craft@26d607craft@26d607..........

  17. #37
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default Re: i want write 2d java game

    Your print out shows that the variable is not null. Check where the error is coming from.
    Why did the program not show the error message just after what you printed out???
    The last value printed should have been: null before the Exception occurred.
    Was there any "null" printed out???

    What was printed: craft@26d607 was from the Object class's toString method. It is the name of the class followed by @ followed by the hashcode for the object.

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

    Default Re: i want write 2d java game

    it print previous message and null for a second but then print craft@"number" continuously. i don't understand anything. :(

  19. #39
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default Re: i want write 2d java game

    If it prints null sometime that means that the value of the variable is null when the println was called that time.
    Some where the code must set the value of the variable to null. You need to find out where that happens and why so you can fix it.

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

    Default Re: i want write 2d java game

    i didn't find where that happens :( should i complete game with actionPerformed and timer?

Page 2 of 5 FirstFirst 1234 ... 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
  •