banner



How To Destroy An Object When It's Finished It's Animation Gamemaker

Welcome to the BreakThrough tutorial! This tutorial is designed to go you lot upwardly and running using GameMaker Studio 2 in as brusque a time every bit possible and volition take you through the steps required to make a "breakout" style game. In this tutorial y'all'll be making a bones but playable version of the game from scratch using DnD™ (Drag and Drop™). Should you prefer a more textual approach to game making, this tutorial is available using the GameMaker Linguistic communication (GML) on this page.

BreakThrough Final Game

Sprites

When using GameMaker Studio two, all your visuals showtime off as sprites. These are just images made up of pixels that nosotros will describe to the screen to correspond what is happening, and in the case of a "Quantum" game, nosotros will need the following:

  • A bat - This will be what the actor moves to straight the ball

  • A ball - This will bounce around the screen and destroy bricks

  • Bricks - These are what you must destroy to complete the game

  • Borders - What the brawl volition bounciness off so it doesn't go out the screen

To create a sprite, go to the Resource Tree, then use the Right Mouse Push button on the Sprite resource and select Create Sprite, then proper noun the sprite "spr_Bat":

Create A Sprite

This will open up the Sprite Editor window where we show the properties for the sprite:

The Sprite Editor

The first thing we need to practice is make the sprite the correct size, as nosotros don't want a foursquare bat! For that click the Resize Sprite push and change the size of the sprite to 64 pixels wide by sixteen pixels tall:

Resize Sprite Canvas

Back in the Sprite Editor again, click on the Edit Prototype button to open up the Image Editor. The Image Editor is used in GameMaker Studio two to depict and edit all the graphical assets that make up your game. In this case we want to draw a "bat" for the game, so, utilize the different tools in the toolbox to do this at present, specifically:

Make The Bat Sprite

Yous tin can take a moment if you wish to play around with the other depict tools (you tin find full descriptions for each of them in the transmission) to run into what they can do or brand the bat image prettier, and then when y'all are finished merely shut the epitome editor and the image will be saved and assigned to the bat sprite.

Nosotros need to set up the sprite origin now. This is the point where the sprite will exist positioned within the room (call back of it similar pinning an image to a lath... the origin is where you stick the pin) and we need to set it to the Middle Center position:

Set The Sprite Origin

With that done, permit'south make the rest of the basic sprites for the game, starting with the bricks:

  • Create a new sprite and call it "spr_Brick"

  • Click the "Resize" push button and set the size to 32 width and xvi height

  • Click the "Edit Image" push button and in the image editor fill the rectangle with white and give it a black border (nosotros are going to colour the bricks programmatically so they need to exist blackness and white)

  • Close the Prototype Editor and the Sprite Editor, without changing the origin of the sprite as it's fine set to the top left

The Brick Sprite

The next sprite to make is for the ball, so go alee and do the post-obit:

The Ball Sprite

Okay, that's all the sprites for now. Let's move on to creating some objects to use them!

Objects

We are at present going to make our game objects. Objects are the resource that we use to control aspects of a game and to do specific things. They can be given a sprite, have behaviours, and they can exist instructed to react to certain game events as well as to each other.

So, let's make our get-go object! This object is going to be for the "bricks" that nosotros want our player to break. To create this, become ahead and correct-click on the Objects resource in the resource tree and select "Create Object". This will open up up the Object Editor:

The Object Editor

In the "Name" section give the object the name "obj_Brick", and so click the button labeled "No Sprite". This will open the Asset Explorer where you tin select the brick sprite that you drew previously:

Adding A Sprite To An Object

We don't need to alter any other properties for this object, and tin now move on to adding some Events. An Result is simply a moment within the game loop when something happens. The main ones are:

  • Create: This is triggered the beginning fourth dimension an case of the object is created in a room and is where you would initialise variables and set values etc...

  • Footstep: Each game pace (or frame) this upshot will be triggered, and it's where you would identify the majority of the objects logic

  • Draw: This is where things are drawn. If you have no code in this result, and then GameMaker Studio 2 will automatically draw whatever sprite is assigned to the object

  • Destroy: This issue is only triggered when an instance of the object is destroyed (removed from the room) and can exist used for a diversity of tasks like creating an explosion, or calculation score, or playing a audio, etc...

For more detailed information on all of the events that an object can have, delight see the manual.

In the example of our brick object, nosotros will need to utilize the Create Event simply (for at present), then in the Event window, click the Add Event button and select Create. This event will now be added and a DnD™ workspace volition open up ready for you to add your actions to it (actions are the individual "blocks" that volition be used to create the game code, shown by category in the Toolbox on the correct of the image below):

Brick Object Create Event

To add an activeness from the toolbox, just click and drag it into the DnD™ workspace:

Add An Action

In this instance we want to add the following:

Brick Object Create Event Actions

The first action there is the Choose activity (from the Random category in the Toolbox), which permits you to add various options from which one volition be chosen at random. The chosen option is and then stored in a temporary local variable then it can be used to set the image_blend of the example sprite (image_blend is a congenital-in variable that controls the colour to be composite with the sprite). In this example we are telling GameMaker Studio 2 to cull randomly from one of six colours, and this volition make our brick instances all unlike colours when they are created in a room.

With that washed, you can close the Object Editor for the brick, and nosotros can move on to creating the bat object...

Bat Object

Now we'll create an object for the histrion to control, and in this case information technology's going to be a "bat" that will be used to bounciness the ball back up the screen. For this, yous need to:

  • Create a new object (apply the right mouse button on the Objects resource and select "Create Object")

  • Name the object "obj_Bat"

  • Assign the object the sprite "spr_Bat" (click the button with "No Sprite" and select "spr_Bat" from the asset manager)

The Bat Object

We'll as well take a moment to create the Ball object. Nosotros won't be doing annihilation with information technology nevertheless, just we demand information technology to be because we'll be applying deportment to it later in the Bat object:

  • Create a new object (apply the right mouse button on the Objects resource and select "Create Object")

  • Proper name the object "obj_Ball"

  • Assign the object the sprite "spr_Ball" (click the push with "No Sprite" and select "spr_Ball" from the asset manager)

The Ball Object

Go back to the Bat object now, and add together a Create Effect (click the Add Issue push and select Create). In this Create event nosotros'll add the following activity to create a new example variable, spd (from the "Common" category in the toolbox):

Set A Variable For Speed

This variable will be used to control the speed of move for the bat. We likewise demand to store the 10 outset value for the bat sprite, as nosotros'll be using that to ensure that the bat doesn't go out of the screen when it moves left or right (the x commencement value is the x position of the sprite origin). There isn't a dedicated DnD™ action for this, then we'll utilize the Function Phone call action (from the "Common" category) to call some GML, like this:

Get The X Offset Value

This GML function will go the X commencement value and and so the action will store information technology in the variable xoffset for afterward use.

This object volition also need Keyboard Events to check for keyboard input. Note that there are really 3 types of Keyboard event: the Downwardly event - which is triggered continuously while a key is being held down, the Pressed event - which is triggered once when a key is initially pressed downward, and the Upwards consequence - which is triggered in one case when the key has been released. Go alee and add a Keyboard Down Consequence for the Left arrow central at present:

The Key Down Left Arrow Event

In this consequence nosotros want to add together the following deportment:

Move Left

Hither we use the If Variable action along with the Else activeness (both from the "Mutual" category) to bank check "if the ten position is greater than the spd plus the xoffset, add the spd value on to x, else set the x position to the xoffset value". This check simply ensures that the bat will simply move left equally long equally the sprite is within the room premises.

Nosotros need to do the same for moving correct now, so add together a Keyboard Down Right event to the object and give information technology the following actions:

Move Right

This does the aforementioned as the Left Downwards outcome, only at present we are checking to see if the bat, when moving to the right, will go outside the width of the room (room_width is one of many built-in variables available to get room information).

Nosotros need to add one final result into this object, and that'south a Step Outcome. The step event is called every "step", or frame, of the game, so any actions added here will be run continuously. We need to use this event to ensure the ball object "sticks" to the bat when moving left and right until the player decides to get-go the game.

Add the Step Event now (not the Begin or End events, but the general Step consequence) and give it the post-obit actions:

Move The Ball With The Bat

Here nosotros create a temporary local variable to hold the unique ID value of the Bat instance, then we employ the Apply to... activity to tell GameMaker Studio 2 that the next actions are to be applied to the Brawl object. The next deportment have the ball object chack a variable and if it is fake move the ball to the same 10 position equally the bat.

Sound Effects

Before nosotros continue on to make full in the details of our Brawl object, let's take a moment to add some audio effects to our game.

We're going to add iii sounds: a bounce sound, a interruption sound and a button sound. To start with, go to the resource tree and correct click the "Sounds" resources and select Create. This will open the Sound Editor:

The Sound Editor

Proper name the sound "snd_Bounce" and then click file explorer button and browse to an appropriate sound (information technology should be *.wav or *.ogg format) then add it. We don't demand to change any other settings here, then close this window.

At present create two more audio resource and proper noun them "snd_Break" and "snd_Click" and add appropriate sound files to them.

With that done, we can motion on to creating the brawl object.

Ball Object

We created the Brawl object previously, but we haven't added any events to it. Allow'due south remedy that now by adding a Create event, to which we'll add together the following actions to gear up some variables:

The Ball Object Create Event Actions

This object volition also demand a Keyboard Down Result targeting the Space key, then we'll add that now and give it the following actions:

The Ball Object Space Key Down Actions

All nosotros're doing hither is in the event that checks to run into if the "Infinite" key has been pressed down, we check to see if the go variable is gear up to faux, and if it is then nosotros play a audio and then set the ball in motion, setting go to true again then it only happens once.

That'll fix our ball in motion, but what almost when it reaches the edges of the game area? If we don't stop it, then it'll fly out the game room, which isn't actually much fun! To prevent this we'll detect when the brawl is in a collision with the room boundary and react accordingly. Luckily for us, in the Other event category we have the Intersect Boundary result:

The Intersect Boundary Event

First we demand to ascertain a number of local temporary variables to hold diverse different example variable values. You could just use the instance variables directly, but we'll assign them to local vars instead as it's easier to remember and continue track of:

Define Local Instance Variables

We then need to add the following to use these local variables to perform our boundary checks. First the left and right check:

Check For Left Or Right Room Boundaries

These actions check to run into if the bat bounding box has left either side of the room, and if it has it reverses the horizontal speed (making the ball "bounce") so sets the it to a position that keeps it perfectly within the room. Nosotros'll do something similar for the tiptop and bottom of the room now:

Check For Top Or Bottom Room Boundaries

These deportment will check to see if the ball is about to exit from the elevation of the room and then reverse the vertical speed accordingly, except if it goes out the lesser. In that case, the ball example volition be destroyed and a new 1 created.

The final section of DnD™ that we need to add will play the "bounce" sound we added earlier, and then besides add together a minor random amount betwixt 2 and -2 to the direction of the ball. This is to prevent the ball possibly getting "stuck" bouncing at perfect 180° angles between the wall and annihilation else. We will also increment the speed of the ball a fiddling bit, which will increase the difficulty of the game as information technology progresses:

Play Bounce Sound And Set Speed And Direction

The whole event should now expect something like this:

Full Action List For The Boundary Event

Note : The above is split into two columns to make it easier to read, only really forms a continuous chain of actions.

Collisions

The side by side piece of DnD™ code required for the ball will get in a Collision Event. A collision event is triggered when the object with the upshot comes into collision with the instance of another object which you specify. In this case, nosotros are going to add a collision for the brawl with the brick object, so click the Add Event button now and add a Standoff event with the object "obj_Brick":

Ball And Brick Collision Event

And in the DnD™ editor we will add some GML code. This is because nosotros are going to utilize the lengthdir_x() and lengthdir_y() functions along with move_bounce() to ensure a proper "bounciness" from the brick. So add together the Execute Lawmaking action with the following:

The Execute Code Action

The ball will be moving at a speed of more than 1 pixel every game frame, so when a collision is detected it will probably be when the ball and the brick sprites overlap. The code above simply moves the ball dorsum along the direction information technology came from until the sprites no longer overlap, and and so information technology calls the move_bounce() to tell GameMaker Studio ii to "bounciness" the instance (GameMaker Studio 2 will do all the maths for you lot and prepare the speed and the direction of the ball every bit appropriate).

With that done we need to increment the speed of the ball speed - ensuring information technology doesn't go over a a maximum speed value - then play a sound and destroy the brick. We'll do that using the following DnD™, which y'all should add together after the Execute Code action:

Bounce The Ball

Annotation that we change the telescopic of Destroy Example to target the "other" instance in the collision (the brick). If we don't exercise this, we'll destroy the ball instead.

Destroy The Other instance In A Collision

When a standoff event is triggered information technology is because the sprite bounding-box of one object is overlapping the sprite bounding-box of some other, which in this example ways that the brawl is overlapping the brick. We want to simulate a bounce off the edge of the brick, but if they are overlapping (even if it's only past a couple of pixels) then this bounce will exist wrong, so what this code does is information technology first moves the ball back along the direction it came from until the bounding-boxes of each instance in the collision are no longer overlapping (they could be considered "touching") and then uses the a GML function to calculate the bounciness direction, earlier destroying the brick itself and playing a sound.

Our final slice of DnD™ will go in another Collision consequence, this time with the Bat object. So, add together a Collision effect with "obj_Bat" and give it the following actions:

Bounce The Ball Off The Bat

With these actions we are setting the direction of the ball based on how far from the centre of the bat the ball hit, then in this way we are giving the role player a flake of control over the direction of the bounciness.

Information technology's time now to identify all this into a room and exam our game!

Rooms

When making a game in GameMaker Studio 2, information technology requires at least one room to run, and so GameMaker always adds a room to your new projects. In this instance it's chosen "room0" and tin be constitute in the Resource Tree Rooms section. Similar all other resources, you lot can add further rooms by using the right mouse button card and selecting "Create Room", but for our game, nosotros'll only need i so let'south simply edit that.

To start with, double click on the room in the resource tree to open the room editor:

The Room Editor

Everything that you lot identify into the room is placed onto a layer, and yous can see that in that location are two layers pre-created for you - i for "Instances" and ane for the "Groundwork". You don't need to add together any other layers as these are fine for what we're making, so click on the "Instances" layer to select it ready for adding our game objects to.

NOTE: It's important to empathize that you don't actually identify objects directly into the game rooms, but rather you place instances of these objects which are basically copies (or clones if yous prefer) of the object resource. Then, in a typical game, all the characters, monsters, balls, walls, etc... are all objects you create in the resources tree, which you then place as an instance in the Room Editor. This instance can so be changed in the room editor itself or through code, so that it tin be scaled, or coloured, or accept other details modified from the "base" object. So when we talk about something affecting or changing an case, we mean it affects that one particular copy of an object in a room, merely, when we talk most affecting or changing an object, nosotros mean that nosotros are modifying the object in the resource tree and anything we practise to information technology will be reflected in all the instances created from that indicate on too. So, the object is the template for the instance, and the case is what we place in a room to make our game.

To commencement with, lets brand the room a little smaller. The default size is a playing area of 1024x768 pixels, only we want to set it to 640x480. This is done from the Room Properties tab:

Changing The Room Size

Nosotros at present desire to identify some brick instances into the room. To make things easier, you should prepare the editor grid to be 32x16 rather than 32x32:

Grid Settings

To add a brick, simply click the object "obj_Brick" and and so drag information technology onto the room to the position y'all want:

Add Bricks To The Room

It can be time consuming to constantly drag instances of objects from the Resource Tree, and so instead you lot can simply select the brick object and then "pigment" it into the room by holding downwards and then clicking and dragging where yous want:

Paint Bricks In The Room

Add together most half dozen layers of bricks, and then add a single instance of the ball object and then a single case of the bat object. Your room should now look like this:

The Finished Room

Time to examination the game out!

Gameplay

The fourth dimension has come up to exam our game! This is the easy part as all you lot're required to do is press the "play" button at the top of the GameMaker Studio 2 IDE. If all has gone correctly, your game will run and you tin can play it using the "Space" central and the Arrow keys:

Finished Gameplay

This is a swell get-go, just it'south missing a few things to brand it a "complete" game - namely thespian lives and a score. We want the game to give you three "lives" and keep track of the score you get from each brick destroyed, so for that nosotros are going to make a controller object (A controller object is simply an object in your game that is designed to control the things that go on in the groundwork, and it ordinarily doesn't have a sprite assigned to information technology although it tin have, and information technology can besides draw stuff independently as nosotros'll meet).

Before adding the controller even so, let'southward add together a Font resource, as we'll want our controller to draw some text to the screen later. To add a font, simply right click on the Fonts resource and select Create Font. In the font window that opens, proper noun the font "fnt_Game" and and then select something that you remember will look good, setting the size to 20 (or whatsoever seems advisable for the font being used):

Adding A Font Resource

Yous tin close the Font editor now.

For our controller you will demand to create a new object, telephone call it "obj_Control", and so add a Create event with the following Activity:

Create The State Variable

We'll be using this variable to set the "state" of the controller so that it knows whether the game has to first, is being played, or is finished. Autonomously from this, we'll also use the Game Start event to initialise some global variables:

The Game Start Event

In this issue add this Action:

Initialise Global Variables

These global variables will permit us to go on rail of important values without having to worry about which case of an object is accessing them or irresolute them, and past initialising them in the Game Showtime upshot, they volition only exist initialised once even if we restart the room (but if we restart the game they will be reinitialised).

We now need to draw the score and lives of the actor to the screen so you'll need to add together a Draw event to the object. Note that - like the Step effect category - the Draw upshot has a number of different event types that tin be used, but for this game simply utilise the regular Draw event (you lot can detect out more than almost the different draw events from the manual):

Add A Draw Event

In this outcome nosotros'll need the following Actions:

Draw Global Game Variables To The Screen

Here we are cartoon the score across the tiptop of the room, and then a serial of sprites (scaled downwards) to represent the player lives at the lesser.

We need to add one terminal upshot to this controller object, a Step issue. Nosotros'll exist using this to restart the room when the player destroys all the bricks. Add this result now, and give it the following Deportment:

Check If The Game Is Over

Here nosotros'll either be restarting the room when all the bricks are gone (and because we are using global variables, this will not reset the score or lives), or we'll await for the player to restart the room if all the lives are gone, resetting the global variables.

Now that we accept that done, and earlier standing on to practise anything else, open the Room Editor and add this object into the game anywhere and then close the room editor again...

Nosotros'll test this in a moment, but first we need to add a few extra Deportment to our object "obj_Ball", so open that at present.

We demand to summate the score and tell the controller object when the histrion has a lost a life, etc... so to start with nosotros'll edit the Collision event with the brick object so that it adds to the global score variable. Y'all desire to add together the following Activeness after all the others

Add To The Player Score

We also demand to modify the Intersect Boundary event, in the department where we cheque if the ball goes out the bottom of the room, where the Destroy Case action is. Here nosotros need to aggrandize it to do a bit more than than just destroy and recreate the instance, so change information technology to look like this:

Ball Leaving Room State Check

Go ahead and play the game at present! If all has gone well, y'all should meet intro text on the screen, a score that changes as you play, lives as markers along the bottom and the game should alter according to whether you have no lives left or you lot destroy all the bricks.

Powerups

The manner our BreakThrough game is correct now, you could probably consider it finished, and it is to a certain extent, but information technology'southward missing 1 final thing to arrive into a "proper" Quantum game... powerups! So we're going to add them now.

To start with, create a new sprite and prepare the size to 32 width and sixteen height and telephone call it "spr_PowerUp". Open the sprite in the Image Editor and draw two arrows, something like this:

Expand Bat Icon

We're going to add another image to this sprite to be used in a 2nd powerup blazon. For that click the large "plus" icon to create another frame:

Add New Frame

The next frame should have two arrows pointing downward, something like this:

Slow Down Ball Icon

Now create a new object and call it "obj_PowerUp". Assign it the sprite we've just made, and then add together a Create Event with the following DnD™:

Power Up Create Event

These actions will select a sprite frame to utilise for the powerup and set it moving down the screen. We need to remove the instance from the game if it is no longer visible, and so the side by side thing to add together is an Outside Room event (from the Other category) with this activity:

Power Up Outside Room Event

Finally, we need to add a Standoff Event with the object "obj_Bat". In this upshot we're going to accept the game change based on the epitome index (frame) of the sprite that the powerup uses. In this case, frame 0 volition make the actor bat larger, and frame ane will wearisome the ball downwardly. The activity to practice this are as follows (and should be added to the event):

Power Up Collision With Bat Event

With this DnD™, if the powerup uses frame index 0 then we set the player controlled Bat object to scale forth the 10-axis (stretching it), or if the frame alphabetize is 1 we fix the ball speed back to information technology's original speed. Annotation that if it's frame 0 we also telephone call an Alert Outcome in the player object. This event won't run immediately, but will instead count down for 10 seconds before running (our game has an FPS of 60, then room_speed equals i second of time).

Permit's add that Alarm event now, so open up the object "obj_Bat" and add an Warning[0] event:

The Alarm[0] Event

And here we place this activeness:

The Alarm[0] Event Of The Bat

The final thing we need to do for the game to be finished is have the Brick objects spawn i of the powerups. So open up upwards the object "obj_Brick" and give it a Destroy Consequence with the following DnD™:

Spawn A PowerUp

Press the Play push button now to see the game running... And so give yourself a pat on the back for having created a consummate game in almost no time!

Summary

If all has gone well, you should now be playing a your own, consummate, version of Breakthrough!

BreakThrough!

Now what? Well, you lot have a firm base of operations on which to build and then why non take some time to try things out based on what you've learned so far? Here are some suggestions for y'all:

  • Add more sounds - We should have a audio for when the ball leaves the room and also for when the player gets a powerup, so why not add them?

  • Add more powerups - Adding more powerups should be fairly like shooting fish in a barrel now. Add a new image to the powerup sprite, add the frame number into the Cull action within the create event, then edit the Switch action within the collision upshot to practise something. Perhaps take 1 to make the bat smaller? Or endeavour making one which has the ball laissez passer through and destroy bricks rather than bounce off them?

  • Add a background - The game is played over a simple blackness groundwork, by why not add together your own colourful i? You'll need a 640x480 sized sprite and and then ready it in the Room Editor to the Background layer.

  • Make proper levels - At the moment the game is played on a single level that regenerates, merely why not attempt making custom levels? You'll need to make more than rooms, add the instances to them, and and so in the controller object gear up information technology to go to another room instead restarting the electric current ane when no bricks are left.

Those are just a few things that y'all can practice with a base like this, merely there is and so much more than! You could try calculation a title screen and menu, or making a pause machinery, or even adding persistent leaderboards and achievements... The listing could go along and on, but the of import thing is that you larn how to make games using GameMaker Studio ii and, above all else, you accept fun doing it!

Source: https://gamemaker.io/en/tutorials/breakthrough-dnd

Posted by: smithmonely.blogspot.com

0 Response to "How To Destroy An Object When It's Finished It's Animation Gamemaker"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel