Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 27th, 2006, 9:06 AM   #1
koolkuzz
Newbie
 
Join Date: Mar 2006
Posts: 5
Rep Power: 0 koolkuzz is on a distinguished road
Question Bouncing Ball......Java Help

Hi,

I need some help on my code which I am using in Blue J:

[PHP]import ou.*;
/**
* The class Ball creates a ball which bounces and ricochets within the confines of the square inside the shapes window.
*
* @author (XXXXX)
* @version (v1.0)
*/
public class Ball extends Circle
{
// instance variables
private int x;

/**
* Constructor for objects of class Ball
*/
public Ball()
{
// initialise instance variables
x = 0;
}
/**
* Slows down the movement of the ball within the shapes window
*/
private void delay(int time)
{
try
{
Thread.sleep(time);
}
catch (Exception e)
{
System.out.println(e);
}
}

/**
* Moves the receiver to the position specified by the arguments xPos and yPos
*/
public void moveTo(int xPos, int yPos)
{
this.setXPos(xPos);
this.setYPos(yPos);
this.update();
}

/**
* Changes the colour of the receiver as follows
*/
public void changeColour()
{
if(this.getColour() == OUColour.BLUE)
{
this.setColour(OUColour.RED);
}


else if(this.getColour() == OUColour.RED)
{
this.setColour(OUColour.GREEN);
}


else
{
this.setColour(OUColour.BLUE);
}

}

/**
* Sets the position of the yPos such that the ball appears to be bouncing
*/
public void singleBounce()
{

while(this.getYPos()<250)
{
this.setYPos(this.getYPos()+10);
this.delay(10);

}

while(this.getYPos() + 20 != this.getYPos())
{
this.setYPos(this.getYPos()-10);
this.delay(10);

}


}
}[/PHP]

The problem is that the above code is for the Class Ball, The code should works as follows:


while the yPosition of the ball is less than 250, then the ball yPos should increment in steps of 10 (which I think I've done correctly), so that the ball appears to be moving downwards,
(The problem is with the singlebounce method in the code)

Then the yPos should be decremented in steps of 10 units, until the ball is 20 units below its original position so that the ball appears to bounce upwards in the Shapes window. This is where Im having all the problems, if you look at the code the ball bounces appears to move downwards,but on the way up I cant seem to stop it below 20 units its initial position.


Can anyone help?

Help is greatly appreciated.
koolkuzz is offline   Reply With Quote
Old May 27th, 2006, 10:35 AM   #2
Wizard1988
Professional Programmer
 
Wizard1988's Avatar
 
Join Date: Oct 2005
Location: Chitown
Posts: 422
Rep Power: 4 Wizard1988 is on a distinguished road
You need to rewrite your second while loop. Try to draw a picture of the ball on paper and step through the loop on paper.
Wizard1988 is offline   Reply With Quote
Old May 28th, 2006, 3:40 PM   #3
koolkuzz
Newbie
 
Join Date: Mar 2006
Posts: 5
Rep Power: 0 koolkuzz is on a distinguished road
Ive tried many times to draw on paper, but the thing is that I need to recall the original y position before I can lower it by 20 units.

I dont know how to recall the yPos function as the method for yPos returns void????
Do i use the super() function???
Is there any way u can use 'this' so that you can lower the position of the ball by 20 units???
koolkuzz is offline   Reply With Quote
Old May 29th, 2006, 5:19 PM   #4
starrboy
Newbie
 
Join Date: Apr 2006
Posts: 5
Rep Power: 0 starrboy is on a distinguished road
Quote:
Originally Posted by koolkuzz
I need to recall the original y position before I can lower it by 20 units.
Your so close to the answer try rewording what you need to do and you should get it. Don't want to say too much.

Starrboy
starrboy is offline   Reply With Quote
Old Jun 1st, 2006, 9:28 PM   #5
Harakim
Hobbyist Programmer
 
Join Date: May 2006
Location: West Jordan, Utah, United States
Posts: 176
Rep Power: 3 Harakim is on a distinguished road
Original Y position...
ps. this.getYPos() + 20 != this.getYPos()
will always return false
Harakim is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:11 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC