Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Moving Images with arrow keys (while doing other stuff)
Index -> Programming, Visual Basic and Other Basics -> Visual Basic Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
supaphreek




PostPosted: Mon Jan 24, 2011 6:20 pm   Post subject: Moving Images with arrow keys (while doing other stuff)

So, as the title says, but to elaborate.

Im making a game in which an image has to move... The thing is, the image always has to be moving (and i have the code to set it to keep going)

the problem is, the code i found to use arrows is as follows

code:
Private Sub Form_KeyDown(keycode As Integer, shift As Integer) 'make sure KeyPreview is True on Form Properties
        Sleep (50)
        Select Case keycode

        Case vbKeyLeft    'Then Find which key you want to use from the list below
            direction = direction + 90
            imgblue.Left = imgblue.Left - 1    'Code to run when key is pressed

        Case vbKeyRight    'Then Find which key you want to use from the list below
            direction = direction - 90
            imgblue.Left = imgblue.Left + 1    'Code to run when key is pressed
        End Select

End Sub


So, this does the job and makes the image move when i press a key... But what i want it to do is always run the code imgblue.top = imgblue.top - 1... at all times

how can i do that?

Thanks
Sponsor
Sponsor
Sponsor
sponsor
z_cross_fire




PostPosted: Wed Feb 02, 2011 7:01 pm   Post subject: RE:Moving Images with arrow keys (while doing other stuff)

Do you mean you want something like this:
Press Up: The Image keeps moving upwards until another key is pressed...

Is that what you want?
Can you please explain what you need to do?
Insectoid




PostPosted: Wed Feb 02, 2011 7:08 pm   Post subject: RE:Moving Images with arrow keys (while doing other stuff)

You need a direction flag, most easily consisting of 2 'velocity' variables- X and Y. When someone hits the 'up' key, set the Y velocity to '1' and X to '0'. when someone hits the left key, set X to -1 and Y to 0, etc.

Then every time the main loop iterates, add the X and Y flags to the X and Y coordinates.

For example,
code:

loop {
    if (up arrow pressed){
        y_vel = 1;
        x_vel = 0;
    }
    else if (down arrow pressed){
        y_vel = -1;
        x_vel = 0;
    }
    else if (left arrow pressed){
        y_vel = 0;
        x_vel = -1;
    }
    else if (right arrow pressed){
        y_vel = 0;
        x_vel = 1;
    }
    x_coord += x_vel;
    y_coord += y_vel;
}
Display posts from previous:   
   Index -> Programming, Visual Basic and Other Basics -> Visual Basic Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 3 Posts ]
Jump to:   


Style:  
Search: