Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By Norm

Thread: Projectiles

  1. #1
    krab117 is offline Member
    Join Date
    Mar 2012
    Posts
    1
    Rep Power
    0

    Question Projectiles

    I'm trying to make projectiles(bullets)for a game I'm working on,but I still need to find out how to do a couple things.My projectiles don't appear,and I need a way to get rid of them,since there's no way to delete objects in java from what I've heard.I also need a way to stop everything from happening immediately,such as the sleep proc in my previous language.Thanks in advance!


    Java Code:
    public void paint(Graphics g){
        super.paint(g); 
        if(shot == true)
            if(p.direction == 1);
               g.setColor(Color.BLACK);
               g.fillOval(tileX,tileY, 64, 64);
               //insert deletion code between these,to give the illusion projectile is moving
               g.fillOval(tileX+26, tileY, 64, 64);
               g.fillOval(tileX+51, tileY, 64, 64);
               g.fillOval(tileX+76, tileY, 64, 64);
               g.fillOval(tileX+101,tileY, 64, 64);
            if(p.direction == 2);
               g.setColor(Color.BLACK);
               g.fillOval(tileX,tileY, 64, 64);
               g.fillOval(tileX+26, tileY, 64, 64);
               g.fillOval(tileX+51, tileY, 64, 64);
               g.fillOval(tileX+76, tileY, 64, 64);
               g.fillOval(tileX+101,tileY, 25, 64);
            if(p.direction == 3);
               g.setColor(Color.BLACK);
               g.fillOval(tileX,tileY, 64, 64);
               g.fillOval(tileX+26, tileY, 64, 64);
               g.fillOval(tileX+51, tileY, 64, 64);
               g.fillOval(tileX+76, tileY, 64, 64);
               g.fillOval(tileX+101,tileY, 25, 64);
            if(p.direction == 4);
               g.setColor(Color.BLACK);
               g.fillOval(tileX,tileY, 64, 64);
               g.fillOval(tileX+26, tileY, 64, 64);
               g.fillOval(tileX+51, tileY, 64, 64);
               g.fillOval(tileX+76, tileY, 64, 64);
               g.fillOval(tileX+101,tileY, 25, 64);
    
    
                    
    
    
                    
    
                            if(keycode == KeyEvent.VK_1){
                                shot = true;
                                repaint();
                                
                            }

  2. #2
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default Re: Projectiles

    Use Thread.sleep(ms) to stop the thread for a number of millis.

    You can delete objects in java. Just assign the object to null when its not needed and the garbage collector will remove it.

    You should read a basic tutorial on game programming in Java, here is a link:
    Java(TM) Boutique - The Java Game Development Tutorial - Page 3

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,579
    Rep Power
    23

    Default Re: Projectiles

    You won't want to put a call to sleep in the paintComponent method.

    The call to repaint() should be done outside of the paintComponent method

    What do you mean by "get rid of projectiles"? Are you talking about something that is being drawn on the GUI?
    Stop drawing them will make them disappear.
    Fubarable likes this.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Projectiles

    Quote Originally Posted by ozzyman View Post
    Use Thread.sleep(ms) to stop the thread for a number of millis.
    Not in Swing on the EDT. Far better is to use a Swing Timer.

Tags for this Thread

Posting Permissions

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