196
General / Re: Help with changing a sprite direction
« on: April 23, 2015, 04:45:46 pm »
Not sure if you are aware, but note that your 2 balldirection vectors are not the same variable even though they have the same name. The second time you are defining a new variable with the same name, but in a different scope. After your if statement, it will be like the second assignment never happened because the second version of the vector will go out of scope.
In addition to using the debugger you can also just think through what your code is doing line by line, specifically the "update ball" logic. If your ball is active you are going to move it up a bit each frame. if you have a collision you move it down a bit. This is already problematic because the down movement is likely going to cancel out the up movement and the ball won't move anywhere. You are moving the ball down by the same amount you moved it up. You shouldn't move your ball up in the case where you are colliding.
... But even if it didn't cancel out and you actually started moving down, as soon as you aren't colliding any more it would just move up again. You are only trying to move down in the case you are colliding. Any time you aren't colliding you are moving the ball up. This is likely not what you want.
There are many ways to approach this problem, though this sounds like it might be a homework assignment so I'm reluctant to give you concrete answers. Assuming your ball just needs to bounce up and down, one simple idea is to use a variable to hold what state the ball is in. If the ball is in the "Up" state, then move the ball up. When the ball is in the "Down" state, then move the ball down.
If your ball needs to bounce in arbitrary directions you will need to figure out how to store the ball's current trajectory and modify it when you collide with a wall.
Having said all of that, Jesper Juhl's advice on learning how to use a debugger is a great idea.
In addition to using the debugger you can also just think through what your code is doing line by line, specifically the "update ball" logic. If your ball is active you are going to move it up a bit each frame. if you have a collision you move it down a bit. This is already problematic because the down movement is likely going to cancel out the up movement and the ball won't move anywhere. You are moving the ball down by the same amount you moved it up. You shouldn't move your ball up in the case where you are colliding.
... But even if it didn't cancel out and you actually started moving down, as soon as you aren't colliding any more it would just move up again. You are only trying to move down in the case you are colliding. Any time you aren't colliding you are moving the ball up. This is likely not what you want.
There are many ways to approach this problem, though this sounds like it might be a homework assignment so I'm reluctant to give you concrete answers. Assuming your ball just needs to bounce up and down, one simple idea is to use a variable to hold what state the ball is in. If the ball is in the "Up" state, then move the ball up. When the ball is in the "Down" state, then move the ball down.
If your ball needs to bounce in arbitrary directions you will need to figure out how to store the ball's current trajectory and modify it when you collide with a wall.
Having said all of that, Jesper Juhl's advice on learning how to use a debugger is a great idea.