There are times when you will want a larger play area than the window itself. Views can be used for this. Game genres that may require this are
Space shooter.
Racing game.
Platformer.
Views can be set up using room settings or GML.
First let’s set up a player obj_player and obj_wall with sprites assigned to them, resized to 64x64 with the origin as middle center. In the Step Event of obj_player pop in:
if keyboard_check(ord("A")) {x-=2;}
if keyboard_check(ord("D")) {x+=2;}
if keyboard_check(ord("W")) {y-=2;}
if keyboard_check(ord("S")) {y+=2;}
if x<0 x=0;
if x>room_width x=room_width;
if y<0 y=0;
if y>room_height y=room_height;
movement code for testing
Also create an object obj_splash with the following code in the Create Event. You do this because the starting room tells the game what the window size is (in this case 800 by 400), failure to do this may have undesired effects:
/// @description Goto room
room_goto(room_view_example);
Rename the room you have as room_splash and pop in an instance of obj_splash, setting the room size, as shown in Figure 27-1.
Setting a View
Next create a room room_view_example and set it as shown in Figures 27-2 and 27-3.
Next, place one instance of obj_player in the room, and several of obj_wall, something similar to Figure 27-4.
Also note, in Figure 27-4, the white border that shows the view size.
Now test your game. If you’ve done everything correctly so far, you’ll be able to move the player around. The view should show just a portion of the whole room, and move to follow the player.
It is also possible to set up and control views.
An example for this project is in the resources folder, named Chapter 27 – A.
If you have a view set up, you can reference it, for example, with
Make a split screen with two windows, each of which follows a separate instance.
B)
Make a function that keeps two instances in view, adapting view as required.
Useful Functions
Sometimes it’s useful to know the size, which can be done with:
width = camera_get_view_width(view_camera[0]);
height = camera_get_view_height(view_camera[0]);
There may be times you want to rotate a view, this can be achieved with:
camera_set_view_angle(view_camera[0], 45);
Summary
You now know how to set up a basic view that can follow an instance and keep it in view. You’ll now be able to create larger play areas for your games.