Sign in to follow this  
davidcoleman

Moving object with buttons

Recommended Posts

davidcoleman    134
I have on dialog 4 buttons (Left, Right, Up, Down) and I want when user click+hold one button to have object moving in associated direction. I would find the way to make it using winforms, but I need to use MFC for it. What is the best way to make it?

Share this post


Link to post
Share on other sites
lightfoot    109

One solution; Not entirely sure if its the best one...

Basically you have a "state" object that keeps a value of which direction you're going. You only have 1 mouse point so you can only click one button at a time so this value could simply be an integer.

Set up the event handlers on the buttons so you know when the button is pressed and when the button is released. When all of the buttons are released, set the value back to 0, or -1 or something. When any of the buttons are pressed set a value, so for left it could be 1, right could be 2, up could be 3, etc.

Then, have a timer that gets called 20, 30, 60 times a second and checks this value. If the value is 1 - or left, then move an object to the left slightly and re-render. If the value is 2 - or right, then move the object right.

So. You need to look up on how to add events to buttons and how to handle those events; How to create a timer and get it to call a method every N seconds, and then you need look up how to render objects or move shapes on the screen.

Chris

Share this post


Link to post
Share on other sites
ToohrVyk    1596
Quote:
Original post by lightfoot
Set up the event handlers on the buttons so you know when the button is pressed and when the button is released. When all of the buttons are released, set the value back to 0, or -1 or something. When any of the buttons are pressed set a value, so for left it could be 1, right could be 2, up could be 3, etc.
I agree with your solution, except this part. Why store directions as integers? A simpler approach is to store them as directions: left is (-1,0), right is (1,0), up is (0,-1), down is (0,1), and so on.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this