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 6 of 6

Thread: Help with bouncing on a panel

  1. #1
    Junior Member
    Join Date
    Dec 2012
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Help with bouncing on a panel

    Here I made a code that takes an object, in this case an enemy ship, and move it to the left. I want that object to bounce to the right when he touches the left side of the screen and bounce to the left when he touches the right side of the screen.

    What I get when I run the program is the enemy ship moving to the left and it stays on the left side of the panel without bouncing to the right.


    graphicsMan.drawEnemyShip(enemyship, g2d, this);
     
     
    			if(enemyship.getX() - enemyship.getSpeed() >= 0)
     
    			{
    				ship.translate(-enemyship.getSpeed(), 0);
     
    			}
     
    			if (gameLogic.leftBorderCollision()){
     
    				enemyship.translate(enemyship.getSpeed(), 0);
     
    			}
     
    			if (gameLogic.rightBorderCollision()){
     
    				enemyship.translate(-enemyship.getSpeed(),0);
     
    			}
     
    In another class named GameLogic I have the collision methods defined as:
     
     
            public boolean rightBorderCollision(){
     
    		if(enemyship.getX() +  enemyship.width >= 500){
     
    			return true;
    		}
    		return false;
    	}
     
    	public boolean leftBorderCollision(){
     
    		if(enemyship.getX() <= 0){
    			return true;
    		}
    		return false;
    	}


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with bouncing on a panel

    What variables control the direction the objects are moving?
    Do the collision detecting methods work?
    How does the code change the direction that the objects are to move?

    There is a lot of code that isn't shown.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Dec 2012
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Help with bouncing on a panel

    The method that controls movement is the translate method. For example if I put

    enemyship.translate(enemyship.getSpeed(), 0);

    The enemyships move to the right drawing him self 3 pixels to the right each time that (1/60) second pass.

    The collision detecting methods recognize each time the ship touches the right or left panel of the screen.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with bouncing on a panel

    So if the collusion detection methods work and call the correct code to change the direction of movement for the shapes, can you explain what the problem is?

    What determines what direction the object is to move in?

    I assume you have tested the collision detecting methods
    and the direction changing methods.
    And that they both worked.

    If not, please explain.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Dec 2012
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Help with bouncing on a panel

    If the ship touches the left side of the screen in another words if the location of the ship is negative in the x axis I want the ship to turn in the other direction (right). I did a System.out.print inside the left collision method and the console displays the System.out.print, so the method detects the collision but I am not sure if the changing direction method works.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with bouncing on a panel

    What determines what direction the object is to move in?
    Is there a state for the object that says: moving to the right
    or moving to the left?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Help with bouncing ball
    By aortell24 in forum What's Wrong With My Code?
    Replies: 22
    Last Post: August 1st, 2012, 03:20 PM
  2. Bouncing Balls
    By roza in forum Threads
    Replies: 6
    Last Post: June 7th, 2011, 10:10 AM
  3. Bouncing Ball Program Random Color Change
    By coderEvolution in forum What's Wrong With My Code?
    Replies: 10
    Last Post: March 3rd, 2011, 04:01 PM
  4. Panel Problems?
    By Dellick17 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 22nd, 2010, 01:31 PM
  5. Creating a custom panel:
    By xterradaniel in forum AWT / Java Swing
    Replies: 19
    Last Post: October 3rd, 2010, 07:15 PM