Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Moving Two Objects at Once

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Moving Two Objects at Once

    I am VERY new to java. My school had a beginners program and for our semester final we have to make a basic game. My game has an object fall from the top of an applet, and you have to shoot it with a gun that you can aim. My problem is that I can't aim my gun, have an object fall, and shoot the object all at the same time. There is probably a lot of better ways to do this, but here's my code.

    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;

    public class moveBox extends Applet
    implements KeyListener, MouseListener {

    Random rndm = new Random();
    int xBomb = rndm.nextInt(450);
    int x =250;
    int y = 425;
    int yShooter = 500;
    int counter = 0;
    int yBomb = 0;
    char xposs;

    public void init() {

    addKeyListener( this );
    addMouseListener( this );
    }

    public void keyPressed( KeyEvent e ) { }
    public void keyReleased( KeyEvent e ) { }
    public void keyTyped( KeyEvent e ) {
    char c = e.getKeyChar();
    xposs = c;
    if( c != KeyEvent.CHAR_UNDEFINED)
    {repaint();
    e.consume();}
    }

    public void mouseEntered( MouseEvent e ) { }
    public void mouseExited( MouseEvent e ) { }
    public void mousePressed( MouseEvent e ) { }
    public void mouseReleased( MouseEvent e ) { }
    public void mouseClicked( MouseEvent e ) {
    x= e.getX();
    repaint();
    e.consume();
    }

    public void paint( Graphics g ) {
    try
    {
    while(y<501)
    {
    g.setColor( Color.white );
    g.drawRect(xBomb,yBomb,5,5);
    yBomb+=1;
    g.setColor( Color.black );
    g.drawRect(xBomb,yBomb,5,5);
    Thread.sleep(10L);
    }
    y=0;
    if(xposs == ',')
    {x-=2;}
    else if(xposs == '.')
    {x+=2;}
    else if(xposs == ' ')
    while(yShooter>-8)
    {
    g.setColor( Color.white );
    g.drawRect(x+5,yShooter,5,5);
    yShooter-=1;
    g.setColor( Color.black );
    g.drawRect(x+5,yShooter,5,5);
    Thread.sleep(1L);
    g.drawRect(x,y,20,50);
    g.drawRect(200,yBomb,10,10);
    }
    else if(xposs == 'c')
    {
    System.exit(1);
    }
    yShooter=500;
    g.drawString(""+x,450,499);
    g.drawRect(x,y,20,50);
    g.drawRect(200,yBomb,10,10);
    yBomb++;
    }
    catch(Exception e){}
    }

    }


  2. #2
    Member
    Join Date
    Jan 2013
    Posts
    39
    My Mood
    Relaxed
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: Moving Two Objects at Once

    For this you should use a threaded loop. A thread is a subprosses. This allows a program to do multiple things at once.
    And you should use something else then thread.sleep(); because these could be inregular.
    Here is a website that explains the threaded loop:
    [URL="http://www.java-gaming.org/index.php?topic=24220.0"]
    And for gameprogramming i should recommend "Thechernoproject" on youtube
    he has a series going on about 2d games and the threaded loop.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Moving Two Objects at Once

    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.

    A Timer would be better for this simple animation.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [Question] Objects instantiated within objects.
    By Xerosigma in forum Object Oriented Programming
    Replies: 6
    Last Post: April 25th, 2012, 10:53 AM
  2. Java TV Board Game/Grid + Moving objects
    By Massaslayer in forum Java Theory & Questions
    Replies: 6
    Last Post: December 13th, 2011, 07:03 AM
  3. Moving Average
    By Donieob in forum What's Wrong With My Code?
    Replies: 0
    Last Post: September 3rd, 2011, 08:58 AM
  4. Moving average
    By bondage in forum Loops & Control Statements
    Replies: 0
    Last Post: May 7th, 2010, 04:20 AM