© The Author(s), under exclusive license to APress Media, LLC, part of Springer Nature 2023
B. TyersGameMaker Fundamentalshttps://doi.org/10.1007/978-1-4842-8713-2_27

27. Views

Ben Tyers1  
(1)
Worthing, West Sussex, UK
 
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.

A screenshot of two panels, room editor, and splash. Room editor has dropdown options for layers room splash, instances, background, instance layer properties, properties room splash with room settings, and highlights width and height. Room splash has a grid and highlights the dot with a circle.

Figure 27-1

Showing room settings and instance placed in room

Setting a View

Next create a room room_view_example and set it as shown in Figures 27-2 and 27-3.

A property dialog window of room view example. It has room settings and selects the clear display buffer with a width of 1600 and height of 800. The viewports and cameras enable the viewports as visible.

Figure 27-2

Showing settings

A property screen of room view example. It consists of camera and viewport properties of X and Y Pos as 0 and width and height as 800 and 400. Object player with horizontal and vertical borders and speeds.

Figure 27-3

Showing more settings

Next, place one instance of obj_player in the room, and several of obj_wall, something similar to Figure 27-4.

A grid illustration of the test room. It has scattered square blocks and an animated game character. Highlight the top left portion of the room.

Figure 27-4

Showing instances placed in room for testing

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
vcam=view_get_camera(0);
You can set the size with:
camera_set_view_size(vcam,400,400);
You can set it to follow an instance:
camera_set_view_pos(vcam,obj_example.x-100,obj_example.y-100);

Advance Projects

  1. A)

    Make a split screen with two windows, each of which follows a separate instance.

     
  2. 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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.29.76