Jump to content

  • Log In with Google      Sign In   
  • Create Account

Calling all IT Pros from Canada and Australia.. we need your help! Support our site by taking a quick sponsored surveyand win a chance at a $50 Amazon gift card. Click here to get started!


Ball in Pong stops moving


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 ThinkingsHard   Members   -  Reputation: 236

Like
0Likes
Like

Posted 23 July 2013 - 01:53 PM

This is my Play class, I have only been working on Java here for about 2 weeks, I'm unsure of why my ball will move for about half a second, and then just stop

 

The Code

Spoiler

 

I'm not sure how to fix this >.<



Sponsor:

#2 David.M   Members   -  Reputation: 731

Like
2Likes
Like

Posted 23 July 2013 - 02:43 PM

I haven't run your code but I'm guessing your ball will move to one edge of the screen then stop? I added some comments to problem areas of your code. I'll expand on them after the code.

// Only the update code here. It's where the problem is. The rest of the class looks fine.
public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException{
    Input input = gc.getInput();
 
    if((ballStartX >=1) && (ballStartX <=300))
        ballStartX += 1 ;
 
    if((ballStartX <= 1) && (ballStartX >= 300)) // This will never return true. ballStartX cannot be both less than 1 and greater than 300.
        ballStartX -= 1;
 
    if((ballStartY >=1) && (ballStartY <=250))
        ballStartY += 1;
 
    if((ballStartY <=1) && (ballStartY >=250)) // Same as above. Never true.
        ballStartY -= 1;
 
    if(input.isKeyDown(Input.KEY_A)){
        paddleXStart -= 1 ;
        if(paddleXStart < 0){
           paddleXStart =+ 1 ; // paddleXStart += 1; May want to change to if (paddleXStart < 0) { paddleXStart = 0; }
        }
    }
    if(input.isKeyDown(Input.KEY_D)){
        paddleXStart += 1 ;
        if(paddleXStart > 439){ // Same as above. Might be better as if (paddleXStart > 439) { paddleXStart = 439; }
            paddleXStart -= 1;
        }
    }
}

Edit: Lost half my post.

 

As you can see, the ballwill never move left or I think up. I can't remember how Slick's coordinate system is. ballStartX will never be both less than 1 and greater than 300. You'll want to change

if((ballStartX <=1) && (ballStartX >=300))

to

if (ballStartX < 1) || ballStartX > 300)

The same goes for vertical movement.

if(paddleXStart < 0){
   paddleXStart =+ 1 ; // paddleXStart += 1; May want to change to if (paddleXStart < 0) { paddleXStart = 0; }
}

The above will not always exhibit the same behavior. If paddleStartX is located at (-0.5, 50) it will be set to (0.5, 50) while a paddle at (-1, 50) will go to (0, 50). Always setting its postion to 0 will fix this.

 

 

Your code is framerate-dependent right now. You may want to look at Slick's documentation and wiki and read up on delta time and framerate-independent motion.

 

ballStartX, ballStartY, paddleStartX, and paddleStartY sound like they store the initial positions of the paddle and ball so they can be returned to their initial configuration when a player scores. Personally I would rename them. However, it's more important that you understand your code since you're the one having to work with it.

 

You said you're just starting in Java so I'll let you know you may want to be consistent in your bracket usage. Some of your if statements use brackets while others do not. I can understand having single-line ifs not use brackets but some of yours do and some don't. Standardizing this may help readability.

 

Edit 2: Your ball is only moving one direction when it's on-screen.

if((ballStartX >=1) && (ballStartX <=300))
        ballStartX += 1 ;

If you want to change this behavior, which I assume you do, use a velocity variable and do something like

private float ballVelocity = 1f;
 
// update method
if (ballStartX < 1 || ballStartX > 300) {
    ballVelocity *= -1;
}
 
ballStartX += ballVeocity

Edited by David.M, 23 July 2013 - 02:54 PM.


#3 ThinkingsHard   Members   -  Reputation: 236

Like
1Likes
Like

Posted 23 July 2013 - 04:23 PM

Yeah, I tweaked a LOT of the current code, I've gotten the ball to move around the screen left right up and down now. I ended up deleting almost everything I've posted here.



#4 Kaptein   Prime Members   -  Reputation: 2208

Like
0Likes
Like

Posted 24 July 2013 - 03:34 PM

Yeah, I tweaked a LOT of the current code, I've gotten the ball to move around the screen left right up and down now. I ended up deleting almost everything I've posted here.

 

That's a success story. Welcome to programming :)

The REALLY good days, are when you find your problem, remove the bad parts, and replace it with something that works :)

 

The bad days are when you can't for the life of you find out what goes wrong, where or even why :)






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS