  C RUBY-ON-RAILS MYSQL ASP.NET DEVELOPMENT RUBY .NET LINUX SQL-SERVER REGEX WINDOWS ALGORITHM ECLIPSE VISUAL-STUDIO STRING SVN PERFORMANCE APACHE-FLEX UNIT-TESTING SECURITY LINQ UNIX MATH EMAIL OOP LANGUAGE-AGNOSTIC VB6 MSBUILD # Why does my move() method not keep the ball in bounds?  » java » Why does my move() method not keep the ball in bounds?

By : user2954134
Date : November 22 2020, 01:01 AM
To fix this issue Your move is wrong. The logic of it is false. You want the ball to bounce so make its movement reverse when it hits a wall. What you've done is : if it is outside get it inside and when inside try to get it outside! Change to: code :
``````   public void move(){
if (x < 0) { // left bound
dx = 1;  // now move right
}
if (y < 0){  // upper bound
dy = 1;  // now move down
}
if (x > 600){// right bound
dx = -1; // now move left
}
if (y >500){ // lower bound
dy = -1; // now move up
}
x += dx;
y += dy;
}
``````
``````   public void move(){
if (x < 0) {   // left bound
dx = -dx;  // now move right, same speed
x=-x;      // bounce inside
}
if (y < 0){    // upper bound
dy = -dy;  // now move down, same speed
y=-y;      // bounce inside
}
if (x > 600){      // right bound
dx = -dy;      // now move left, same speed
x=600-(x-600); // bounce inside
}
if (y >500){       // lower bound
dy = -dy;      // now move up, same speed
y=500-(y-500); // bounce inside
}
x += dx;
y += dy;
}
`````` ## Making a method so as to be able to move the ball in PapiJump sideways by touching the sides of the screen

Date : March 29 2020, 07:55 AM
wish help you to fix your issue Nothing obviously wrong there that I can see (assuming you have defined all your constants, etc.)
Having been bitten by it a few times, the first thing I check now with odd crashes is if there are any broken IBOutlet or IBAction links on the viewController owner (indicated by those orange alert triangles). ## Program that should move a ball, but method run is not executed

By : Greg Zeck
Date : March 29 2020, 07:55 AM
I wish this help you , You need to pass an instance of Runnable to Thread constructor:
code :
``````Thread th = new Thread();
``````
``````Thread th = new Thread(this);
`````` ## How to add a ball over the line and move the ball with each click in HTML5?

By : hamza nasab
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further I'm trying to create this ball moving. So each click it's supposed to add a ball to the end of the line from the right and as the animation still runs the ball will move to the left until it disappears. So if I click the button 5 times I should have 5 balls moving at the same time but the first will go first and follow by the rest of the balls. And the distance should depend on when the button is clicked. , You can make a list of balls
code :
``````var balls = [];
``````
``````jQuery('#addBall').click(function() {
balls.push(new Ball());
});
``````
``````function Ball() {
this.pos = 400;
this.render = function() {
this.pos -= 1;

context.beginPath();
context.arc(this.pos, 10, 5, 0, 2 * Math.PI, false);
context.fillStyle = 'green';
context.fill();
context.lineWidth = 5;
context.strokeStyle = '#003300';
context.stroke();
};
}
``````
``````function animloop() {
requestAnimFrame(animloop);

clearScreen();
drawALine();
for(var i = 0; i < balls.length; i++) {
balls[i].render();
}
}
`````` ## Swift Pong(No SpriteKit): How to calculate ball angles to move ball

By : Maris Reinfelds
Date : March 29 2020, 07:55 AM
I hope this helps . Simply put, you need to reverse the ball velocity 'x' if reached x-boundary and 'y' if reached y-boundary.
Considering no friction or acceleration any and no other factors to effect the velocity. Lets say the ball is traveling 60 degrees towards bottom right and hits right boundary. It needs to travel below on same 'y' velocity while returning towards left with same 'x' velocity. And as it hits bottom boundary, it will still move towards left, while returning towards top with same 'y' velocity.
code :
``````var posX = 0
var posY = 0
var velX = 5
var velY = 8

func moveBall(){
posX += velX
posY += velY
if collideOnXBoundary {
velX = -velX
}
if collideOnYBoundary {
velY = -velY
}
}
`````` ## iPhone:Move a ball according to shake and bounce the ball on edge

By : user3903067
Date : March 29 2020, 07:55 AM
I wish this help you Your drawing code won't make much of a difference, I will assume you know how to draw what it is you want to draw. What is important here is setting up physics. If you have a physics engine running (like Chipmunk or others) then the effects of the shake to move the ball, which I'll get to in a second, may cause the ball to move in a different direction, if that's how you've written your physics. Keep this in mind. I'll assume for the moment that gravity is off.
The idea you want to do is first to figure out whether or not you're going to target iOS prior to version 3.2, because our gesture APIs are different between the two (one way works in 3.2 and later as well, but you really want to use gesture recognizers if you're not worried about supporting 3.0 or 3.1). 