In this lesson you will learn how to create reusable Functions that you can use to control movement of your player, or camera, or other game pieces. Code can be created in separate files and then inserted using an Include command to help keep things organized and uncluttered as well as kept in a convenient reusable form for your other future programs! We will be keeping the code for these lessons split up into 3 separate files: A main lesson.bb, a functions.bb file and a loadworld.bb file. To keep things clean and simple, as you learn new code it will be added to these 3 files and we will just keep track of the new additions! |

| NEW CODE is BOLD. |
Include "functions2.bb" ; INSERTS this files contents here |
Global up_key = 200, down_key = 208, left_key = 203, right_key = 205 |
cube1=CreateCube() |
| Include "functions2.bb" ; INSERTS this files contents here . . . ; Point camera down for top down view! RotateEntity cam1, 90, 0, 0 . . . Include "loadworld2.bb" ; INSERTS this files contents here ; Start mouse cursor at center of screen! MoveMouse screen_width/2, screen_height/2 . . . ; key & mouse control functions! object_key_control( cube1 ) object_key_control_2( cone1 ) ; Player 2 ! mouse_control( cam1 ) . . . |
| EXPERIMENT ! |
| Rotate cube1, cone1, and cam1 into different functions object_key_control( cube1 ) object_key_control_2( cone1 ) mouse_control( cam1 ) Notice anything interesting? |
| Undo the changes you made... |
| ; Character Control Key variable names for player 1 & 2 ; Names make more sense than the "scancode" numbers Global up_key = 200, down_key = 208, left_key = 203, right_key = 205 Global W_key = 17, S_key = 31, A_key = 30, D_key = 32 ; Our FIRST FUNCTIONS! ; The "dummy" variable "obj" is used so that we can REUSE this function ; to control ANY object! (camera, player, etc) Function object_key_control( obj ) ; IF the up arrow key is pressed THEN the object is MOVED forward (+Z) If KeyDown( up_key ) = True Then MoveEntity obj, 0, 0, .2 ; IF the down arrow key is pressed THEN the object is MOVED backward (-Z) If KeyDown( down_key ) = True Then MoveEntity obj, 0, 0, -.2 ; IF the left arrow key is pressed THEN the object is TURNED left (+Yaw) If KeyDown( left_key ) = True Then TurnEntity obj, 0, 2, 0 ; IF the left arrow key is pressed THEN the object is TURNED right (-Yaw) If KeyDown( right_key ) = True Then TurnEntity obj, 0, -2, 0 End Function ; Same as before but different key set "WASD" Function object_key_control_2( obj ) If KeyDown( W_key ) = True Then MoveEntity obj, 0, 0, .2 If KeyDown( S_key ) = True Then MoveEntity obj, 0, 0, -.2 If KeyDown( A_key ) = True Then TurnEntity obj, 0, 2, 0 If KeyDown( D_key ) = True Then TurnEntity obj, 0, -2, 0 End Function ; Mouse screen scrolling function! Function mouse_control( obj ) ; Get Mouse X & Y position and store in variables mx & my mx = MouseX() : my = MouseY() ; IF Mouse gets within 50 pixels from the screen edge it will start scrolling! If mx < 50 Then MoveEntity obj, -.3, 0, 0 If mx > screen_width-50 Then MoveEntity obj, .3, 0, 0 If my < 50 Then MoveEntity obj, 0, .3, 0 If my > screen_height-50 Then MoveEntity obj, 0, -.3, 0 End Function |
| EXPERIMENT ! |
| Change the speed of moving and turning in any of the 3 control functions! |
| Undo the changes you made... |
. . . |
| EXPERIMENT ! |
Try changing the color of the water planeTry changing the scaling of the water texture |
| Undo the changes you made... |
| THE CHALLENGE!!! |
| Add "included" controls to your previous Lesson 1 Challenge |