My circle moves opposite to what I input

When I hit the D or A key, my circle moves to the appropriate side but then changes direction if I continue to hold the key down. The circle then goes back and forth if I continue to hold either key down. I’d appreciate any help on figuring out why my circle just doesn’t continue one direction if I hold one key down.


Hi Randy.

Try this.

Place three tildes on a line before and after your code it will be very easy for others to see your code and maybe help you out.

Here is an example of some random code I copied and pasted.

I hope that will help you out.

<script type="text/javascript">
		jQuery(document).ready(function($) {
			$(".scroll").click(function(event){
				event.preventDefault();

		$('html,body').animate({scrollTop:$(this.hash).offset().top},1000);
			});
		});
</script>

You’re using direction to move the circle, which is getting flipped whenever the axe reaches the top or bottom edge of the screen.

Replace this:
circle_x += direction;

With this:

circle_x += 10

Do my pictures not make it easy to view my code? Thank you for the suggestion.

Direction has been initialized to 10, this hasn’t fixed the issue. I reposted this cause I wasn’t sure if I replied to you or just commented under my own post. Thank you for the time.

It just makes it easier to read.

Is it still not working?

Sure, direction is initialized to 10. But the value of Direction changes every time the axe reaches the end of the screen (Top/Bottom). So if you’re moving the circle using the same variable that explains the sudden change in movement while you’re holding the key down.

What I’m saying here is do not use direction for circle_x += direction and instead use something else. I suggested a value of 10 because that is what is in the code for the course.

I tried this but the same issue still occurs.

Ok, so just to make sure, this is your code after making that change?

if(IsKeyDown(KEY_D) && circle_x < width)
{
    circle_x += 10;
}

if(IsKeyDown(KEY_A) && circle_x > 0)
{
    circle_x -= 10;
}

Don’t forget to save and compile as well.

It’s still not working, I’m not sure what the problem is honestly.

I didn’t compile after I changed it. This actually solved the issue for me. Thank you so much for solving this issue for me. You’ve been a massive help!

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms