Moving the object on the window
-
Sunday, February 19, 2012 7:08 PM
Hello, I´d like to program some small interactive application. Just something where you cann move with the common circle as it would be in 2D game. I made it, but there is an isue with the moving itself. I want the circle to move continuously but when I press the left, right, up or down key and hold it, the circle makes one move, then it stopes and then it moves again but much faster and still it accelerates.
Here is the id for that application where you can find the source code too.
GWQ067
It would be great if there is anyone who could help.
All Replies
-
Sunday, February 19, 2012 9:28 PMModerator
2 points:
1] To get the best control of moving objects use Shapes.Move rather than Shapes.Animate. To do this you will need to keep track of the shapoe coordinates at each screen refresh.
2] There is an auto repeat delay on button presses imposed by the OS. To get smooth movement we need to detect key down and key up. Between these 2 evenets the key is pressed. The first movement you see is the initial key press and the much faster movement after a short delay is the auto-repeat kicking in and registering lots of key-down events in quick succession.
Simple example:
'Initialisation Window gw = 800 gh = 600 GraphicsWindow.Width = gw GraphicsWindow.Height = gh 'Initialise Key events GraphicsWindow.KeyDown = OnKeyDown GraphicsWindow.KeyUp = OnKeyUp keyLeft = 0 keyRight = 0 keyUp = 0 keyDown = 0 'Initialise ball ball = Shapes.AddEllipse(50,50) ballX = 100 ballY = 100 speed = 3 Shapes.Move(ball,ballX-25,ballY-25) 'Main loop - just move the ball While ("True") processKey() Shapes.Move(ball,ballX-25,ballY-25) Program.Delay(10) EndWhile 'Key press event subroutines 'A separate Down and Up is checked for each key, this tells us the state for any key 'And isn't affected by auto-repeat delays for keys Sub OnKeyDown lastKey = GraphicsWindow.LastKey If (lastKey = "Left") Then keyLeft = 1 ElseIf (lastKey = "Right") Then keyRight = 1 ElseIf (lastKey = "Up") Then keyUp = 1 ElseIf (lastKey = "Down") Then keyDown = 1 EndIf EndSub Sub OnKeyUp lastKey = GraphicsWindow.LastKey If (lastKey = "Left") Then keyLeft = 0 ElseIf (lastKey = "Right") Then keyRight = 0 ElseIf (lastKey = "Up") Then keyUp = 0 ElseIf (lastKey = "Down") Then keyDown = 0 EndIf EndSub Sub processKey 'Move object - note it can move diagonally if 2 keys are pressed If (keyLeft = 1) Then ballX = ballX-speed EndIf If (keyRight = 1) Then ballX = ballX+speed EndIf If (keyUp = 1) Then ballY = ballY-speed EndIf If (keyDown = 1) Then ballY = ballY+speed EndIf 'Check for hitting edges If (ballX < 0) Then ballX = ballX+gw EndIf If (ballX > gw) Then ballX = ballX-gw EndIf If (ballY < 0) Then ballY = ballY+gh EndIf If (ballY > gh) Then ballY = ballY-gh EndIf EndSub- Edited by litdevMicrosoft Community Contributor, Moderator Sunday, February 19, 2012 10:52 PM more details and typos
- Proposed As Answer by litdevMicrosoft Community Contributor, Moderator Thursday, February 23, 2012 7:22 PM
- Marked As Answer by Ed Price - MSFTMicrosoft Employee, Owner Tuesday, October 23, 2012 12:34 AM

