You are here:

C++/Moving Shapes

Advertisement


Question
-------------------------
Followup To
Question -
Hi Eddie,
I am trying to write a program to get a circle to move on a form .I have set the ball moving from left to right when the program runs.There are 4 buttons on the screen Up,Down,Left and Right and i have to get the circle to go in the direction of the button which is pressed.The properties  of the circle-Left and Top-move the circle on the screen.I have tried to put Circle->Top = Circle->Top + 1  as the event handler for the Up button but all it does is move the circle one pixel up the screen!I know some sort of loop is required but i am finding it a bit confusing.I know that i will have to declare a variable in the forms header file if the variable is to be used by more than one event handler(Left,Right,Up and Down). If i can get the circle to move corresponding to the button clicks I can progress with my project.
I would very much appreciate your help with this problem.
I have written Circle->Left=Circle->Left+1  as the code for the OnTimer which is also on the form to set the circle moving from left to right when the program starts.
Do I have to write any more code for the OnTimer to get the circle to move or is the code written in the individual button event handlers?
Thank you very much
Yours sincerely
Alun
I am using Windows XP
The software is Borland Builder 5
Answer -
Hello Alun,

I would like to know if this is a Windows or a Console Application. Depending on which one, the answer will be different. Thanks a lot,

- Eddie

Hi Eddie,
It is a console application
Thanks
Alun

Answer
Hello Alun,

As opposed to Java, in C++ there is not default event handler for key presses. The function GetAsyncKeyState takes in a virtual key code and returns a value when the button is pressed. It is then up to the programmer to handle what needs to happen. I'm assuming that console applications render the same as Windows applications, with x and y representing the top left corner of the ellipse. An easy way to move the circle like you want would be to create an Update() function in the Circle class; i.e.

class Circle
{
public:
int xPos, yPos, xVector, yVector;

Circle() {xPos = yPos = xVector = yVector = 0;}

void Update
{
   xPos += xVector;
   ypos += yVector;
}

// in main
int main()
{
   Circle circle;

   while(true)
   {
        circle.Update();
        // left arrow
        if(GetAsyncKeyState(VK_LEFT))
        {
         circle.xVector = -5;
         circle.yVector = 0;
        }
        // right arrow
        if(GetAsyncKeyState(VK_RIGHT))
        {
         circle.xVector = 5;
         circle.yVector = 0;
        }
        // down arrow
        if(GetAsyncKeyState(VK_DOWN))
        {
         circle.yVector = 5;
         circle.xVector = 0;
        }
        // up arrow
        if(GetAsyncKeyState(VK_UP))
        {
         circle.yVector = -5;
         circle.xVector = 0;
        }
        // terminating condition
        if(GetAsyncKeyState(VK_ESCAPE))
         break;

        // render circle here
   }       
}

That code snippet will force the circle to go up, down, left and right by altering the vector it updates itself on based on checking key presses.

As for the OnTimer, I'm not familiar with that function. Sorry. I hope that this was helpful to you and PLEASE write me back if yo have any further troubles.

- Eddie  

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Eddie

Expertise

I can answer questions about the C++ language, object oriented design and architecture. I am knowledgable in a lot of the math that goes into programming, and am certified by ExpertRating.com. I also know a good deal about graphics via OpenGL, and GUIs.

Experience

I have completed numerous games and demos created with the C++ programming language. Currently employed as a software engineer in the modeling and simulation field. I have about 7 years experience.

©2016 About.com. All rights reserved.