banner



How To Draw A Sprite In Game Maker Studio

Cartoon

This section (and the following department on Movement and Controls) is aimed at giving you applied examples of GML or DnD™ to enable you to get started every bit quickly as possible making your outset game projects. We won't be explaining things in too much depth equally we want you to get started making stuff equally rapidly as possible, so we encourage you lot to explore whatsoever links every bit you get along and to use the "search" office of the manual to look for boosted data on annihilation you aren't sure near.

In this section we're going to concentrate on simply cartoon information to the screen, both as text and every bit images, and also explain a bit more about the different Draw Events, specifically, the principal Depict issue and the Draw GUI consequence (note that in some of the examples you will be required to add other events, merely we'll explicate these equally nosotros come up to them).

The Draw Events

Before going any further, you might want to make a new project (either GML or DnD™) from the Start Page, and add (or create) a few sprites besides as an object or two, as we'll be giving you some lawmaking that yous tin test using these. Even a white square will piece of work for now as the sprite for our object!

Now, as mentioned in the section on Objects And Instances, if y'all don't add together a Depict Outcome to the object, then GameMaker Studio two will default draw, pregnant that if the object has a sprite assigned to it this sprite will be drawn, complete with any transforms that accept been added. What do we mean by transforms? Well, each object has a number of born variable that will control how an instance of the object draws its sprite when default drawing, and y'all can change these variables equally the game runs to change the fashion the sprite is drawn.

Annotation: Y'all can notice a listing of all the built in variables that tin be used for transforming instance sprites hither. Elevate and Drop™ users have some dedicated actions that bear on these variables, which you lot can find hither, and you can likewise utilize the actual variables themselves along with the Get Case Variable and Ready Instance Variable deportment.

Let's look at some examples:

Changing Blastoff (Transparency) Irresolute Alpha (Transparency)

The alpha value is what controls the transparency of what's being fatigued, and in GameMaker Studio two, y'all can use the image_alpha congenital-in variable to alter how transparent the assigned sprite is. To run across how this works, open up (or create) an object, assign it a sprite, and and so give the object a Create Event. In the Create Event, simply add together the following DnD™ or GML:

DnD Draw Example For Transparency

var _val = random(1);
image_alpha = _val;

Image alpha is calculated as a value from 0 to one, where 0 is fully transparent and 1 is fully opaque (by default it's set to ane). So in this example, all nosotros're doing is setting the epitome alpha to a random decimal value from 0 to 1. Identify a few instances of this object in a room, and and so click the Play button Play Icon at the summit of the IDE.

You should come across that each instance of the object draws its sprite with a dissimilar transparency, eg:

Example Of Sprites Drawn With Different Alpha Values

Changing Colour Blend (Tinting) Changing Colour Blend (Tinting)

When your object is default drawing a sprite, this sprite is actually being fatigued blended (or tinted) with a colour, and this colour value is stored in the image_blend congenital-in variable. By default this colour is white, which essentially means that no color will be added to the sprite when information technology is shown on the screen. However, you lot tin can utilise other colours to achieve special effects, for example, apply red to show the instance has received some damage.

In this example, we're going to blend different colours with the sprite while a primal is pressed and held downward, and so y'all'll need to open (or create) an object, assign it a sprite, and then give the object a Key Downward <Space> Event.

Add Key Down Event

In this Central Downwards Event, add the post-obit DnD™ or GML:

DnD Example Using Key Down Event

var _col = cull(c_red, c_green, c_blue, c_yellow, c_fuchsia, c_orange);
image_blend = _col;

Place a few instances of this object in a room, and so click the Play button Play Icon at the top of the IDE, and examination holding downwardly and releasing the Space primal. Y'all should see that each instance will change it's colour apace while the central is held down, and end irresolute when it is released:

Example Showing Colour Blending

Irresolute Scale Changing Scale

Another of the backdrop that we tin can change for our sprite is the scale value, permitting us to depict it bigger or smaller whenever we want. Scale is caculated independently along the 10 and Y axis by two seperate variables, the image_xscale variable and the image_yscale variable. By default these are set to ane, and they act similar multipliers, and so a value of 0.5 would be half scale and a value of 2 would be double the scale.

IMPORTANT! Changing the assigned sprite scale using these variables volition besides alter the size of the bounding box to lucifer, which means that the standoff detection area for the sprite will also scale.

In this example, nosotros're going to use some simple maths to make an case scale the sprite up and downwards in a loop. To outset with, open (or create) an object, assign it a sprite, then give the object a Create Event. In this event add the following:

DnD Set Variable Example

timer = 0;

Now add a Stride Outcome to the object with this:

DnD Actions To Change Sprite Scale

timer = timer + 1;
var _val = dsin(timer);
image_xscale = one + _val;
image_yscale = 1 + _val;

Here nosotros're using the maths function dsin() to generate a value between -i and 1 using the timer variable, and and so applying that to the scale variables. Later placing some instances into a room and pressing the Play button Play Icon, you should see how the instances calibration up and down from a scale of 0 to a scale of two and then dorsum again.

One final matter... change the " image_yscale" department to " 1 - _val" and see what happens!

The in a higher place examples illustrate just some of the many ways that you lot tin manipulate the object sprite when GameMaker Studio 2 is default drawing, but what virtually if you lot want to draw more than one thing for an object? In those cases you lot need to use the Draw Consequence to explicitly tell GameMaker Studio 2 what to draw, which is what we'll do in the following examples.

Drawing Two (or more) Sprites Together Cartoon 2 (or more than) Sprites Together

For this instance, you'll need two sprites and one object. Call the sprites " spr_One" and " spr_Two", and then set the " spr_One" origin to the center and for " spr_Two" ready its origin to the middle-left:

Show Origins For SpritesAssign the first sprite (" spr_One" with the heart origin) to the object yous have created and then add a Create Event. In the Create Event add the following DnD™ or GML:

Set Draw Angle Variable In DnD

draw_angle = 0;

We are going to use this variable to rotate " spr_Two" over fourth dimension, and describe it overlayed on the sprite assigned to the object (" spr_One"). To practice this we need to add a Draw Event to the object. By doing this we are telling GameMaker Studio 2 that we want to take over what the case draws, which means that our code will include a call to the draw_self() function or Depict Self activity. This activeness merely replicates what the object does when no Draw Event is present and information technology is default drawing the assigned sprite. We'll and so depict the 2nd sprite that we want to use every bit the overlay sprite that is rotating. The DnD™ and GML looks like this:

Draw Two Sprites With DnD

draw_self();
draw_angle = draw_angle + 0.5;
draw_sprite_ext(spr_Two, 0, ten, y, 1, ane, draw_angle, c_red, 1);

Add a number of instances of the object into the room editor and and then printing the Play button Play Icon at the summit of the IDE. If all has gone correctly you should encounter something similar this now:

Animation Drawing Two Sprites Together

Before nosotros leave this example, let's simply tweak it a lilliputian bit and instead of having " spr_Two" merely rotate, we'll make it point towards the mouse position. For that we need to change the Describe Effect DnD™ or GML to look like this:

Edited Draw Code For Drawing Two Sprites With DnD

draw_self();
draw_angle = point_direction(x, y, mouse_x, mouse_y);
draw_sprite_ext(spr_Two, 0, x, y, 1, one, draw_angle, c_red, i);

Run the project again and this time you'll come across something very different!

Animation Drawing Two Sprites Pointing At MouseThe sprite now points towards the mouse regardless of where you lot movement it to! Every bit you tin can see, layering sprites is a great way to add details to an object or to have something move independently of the "base" sprite assigned to the object, and it's a powerful tool that you'll probably apply a lot in your own projects.

Drawing Things Other Than Sprites Drawing Things Other Than Sprites

You lot can draw things in the Draw Event other than sprites too, like text, or shapes. In this example, we'll utilise the DnD™ or GML draw_self() function to draw the object sprite, simply we'll also draw some other stuff, starting with some text. For this case, you'll need a sprite and an object (with the sprite assigned to it). In the object, first add together a Create Event with this DnD™ or GML:

Setting Variables Using DnD

name = choose("Fred", "Jonas", "Sharon", "Kate", "Frank", "John", "Monica", "Amanda");
number = irandom(100);

All this does is tell GameMaker Studio 2 to choose one of the listed names and assign it to a variable, as well as generate a random number from 0 to 100 for each case of the object. We want to draw these values to the screen, then for that y'all demand to now add a Describe Event and in information technology add the following DnD™ or GML:

Drawing Text Using DnD

draw_self();
draw_set_halign(fa_center);
draw_text(x, y + 32, "My name is " + proper noun);
draw_text(ten, y + 48, "My number is " + string(number));

You'll notice in the above code that we utilize the string() function or Number To String action on the "number " variable that we want to depict. This is because all text has to be fabricated upwardly of characters, not values, and then we need to utilise this office/activeness to convert the number value into those characters that nosotros desire to draw. In this case we are taking the random number nosotros generated and turning it into a "string" of characters that can be drawn. Likewise annotation that we set the text alignment. This but tells GameMaker Studio 2 where to start drawing the text relative to the given position, and in this case we want the text to be centered forth the 10-centrality.

Add a number of instances of the object into the room editor then press the Play push button Play Icon  at the top of the IDE. You should come across something like this:

Sprites Being Drawn Along With Text

In all the examples so far we've been drawing the sprite assigned to the instance, but that doesn't e'er have to be the case. Y'all can depict anything you lot want in the describe effect, regardless of the sprite assigned. To illustrate this indicate, nosotros'll alter the code that we accept currently past removeing the draw_self() phone call and replace with a office to describe a coloured ellipse, like this:

Drawing Shapes Along With Text Using DnD

draw_ellipse_colour(x - fifty, y - 32, x + fifty, y + 32, c_fuchsia, c_lime, false);
draw_set_halign(fa_center);
draw_text(x, y + 32, "My proper name is " + proper name);
draw_text(x, y + 48, "My number is " + string(number));

Run the project again and you should run into this:

Example Of Drawing Shapes And Text Together

One important thing to note well-nigh this, is that even though we aren't cartoon the assigned sprite, it will still be used for collision detection. So, while you may be cartoon one thing, collisions will withal be calculated based on the assigned sprite every bit if it was placed in the room along with the instance, even if information technology's not visible. This is quite handy actually, as it means you can depict different sprites, but maintain a single standoff mask based on the assigned sprite. As well note that yous can nonetheless apply the dissimilar transforms like Ten/Y scale, and collisions will be based on the inverse size, even though there is zero being drawn to show this.

The GUI Layer

We mentioned at the height of the page that we'd exist talking nearly the Describe GUI Event as well as the Draw Consequence, so let'south look at that now. The Draw GUI Upshot works on something chosen the GUI Layer, which is a special cartoon layer of a fixed width and height that is drawn over the instances in the room. The dandy thing about the GUI layer is that it doesn't movement with the room camera, so it'south the platonic place to add static GUI items, like scores, healthbars and other data that your game requires to communicate to the user. You tin can find out more information on the GUI layer from the Draw Events section of the manual.

Note: Rooms can be larger than the screen size, so you tin can have large levels for the player to motion around in. This means that in the Room Editor (or in code) you demand to define a camera that follows the action of your game. This is basically a way of setting upwards a stock-still expanse of the screen to display different parts of the larger room based on - for case - the histrion position in the room, and is used in a lot of games. Call up of the style that the view ever follows the main grapheme in archetype games like Mario or Zelda. That's done with cameras. For more data see the section Room Backdrop on the Room Editor section of the manual.

The post-obit examples are all going to be using the Describe GUI result, so y'all'll need to create an object and add that event to information technology. Note that the object doesn't need a sprite assigned, as we are not wanting to default draw anything, nor do we need it to observe collisions. Objects like this, that are only designed for cartoon things or controlling certain aspects of the game are often chosen Controller Objects. Besides note that we will exist using the same object for all the examples, and so we recommend that you become through them one after the other (although this is non strictly necessary).

Draw GUI Event In An Object Drawing Text Drawing Text

When drawing to the GUI layer, the pinnacle-left corner is the origin position, and to the right is +X and downwards is +Y. This makes positioning text and graphics very easy, as you'll come across in this instance. All we're going to do here is draw a value that represents the thespian score, and so in our object we'll need to add a Create Result to initialise a variable to agree this value, similar this:

Defining A Variable Using DnD

player_score = 0;

We also desire to add a Keyboard Down <Infinite> Consequence to the object, equally we'll be using that to increase the score every fourth dimension you press the Space key.

Adding A Space Key Pressed Event

In this event add the following:

DnD Actions In The Space Key Event

var _val = irandom(100);
player_score = player_score + _val;

Finally, allow'southward draw the score value in the Describe GUI event, similar this:

In this event add the following:

Drawing Text To The GUI Layer Using DnD

draw_set_halign(fa_left);
draw_set_colour(c_yellow);
draw_text(32, 32, "SCORE:");
draw_set_colour(c_white);
var _str = string(player_score);
draw_text_transformed(32, 48, _str, ii, 2, 0);

You'll notice how we've used hard-coded (or fixed) values for the ten/y position of the text to be drawn, since we don't demand it to be relative to any instance equally we are drawing to the GUI layer. We've too used the "ready colour" function to change the colour of the text, as well equally the "transformed" fucntion to make the actual score value larger, which illustrates how y'all can get well-nigh customising text elements in your own games.

Add together a unmarried instance of this object to your room now and and then printing the Play button Play Icon. When the game runs printing and release the <Infinite> key and you should encounter the score value increase.

Animation Showing Text Being Drawn To The GUI Layer

Drawing Sprites Drawing Sprites

In this example, we are going to use the GUI layer to draw some sprites. The well-nigh obvious use for this is to describe the players lives, and so lets go ahead and practice simply that! You'll demand a sprite for this case - which should be about 64x64 pixels - but information technology shouldn't exist assigned to the object, as we'll be cartoon it ourselves.

To offset with, we need to add some new variables to the object in the Create Consequence (if y'all've washed the previous example, add together the following below what'southward already there):

Defining Variables Using DnD

player_lives = 3;
gui_w = display_get_gui_width();

In this code we initialise a variable for the player lives, only we too create a variable to hold the width of the GUI layer, and then that we tin position things correctly relative to the right of the screen. We could only hardcode a value into the code and use that, but that would hateful that if nosotros make any changes to the size of the room, or if we add cameras etc... after, then we'd need to go through the code and change the value everywhere. Using the display_get_gui_width() office instead means that nosotros don't need to worry about any hereafter changes like that as the code will adapt automatically to any size the GUI layer ends upwardly.

Next we desire to add together a Keyboard Pressed <Enter> Issue to the object, as we'll be using that to modify the number of lives every time the Enter fundamental is pressed:

Adding An Enter Key Pressed Event

In this event add the following:

DnD In The Enter Key Down Event

player_lives = player_lives - i;

if player_lives < 0
{
player_lives = 3;
}

Finally, we demand to draw the sprites to the display. For this we'll be using a " for" loop (data using GML here and for DnD™ here), along with the GUI width variable to position everything in the top right corner of the screen. And so, add together this into the Draw Gui Effect (after any other actions that it may have from previous examples):

DnD Actions To Draw Sprites To The GUI Layer

for (var i = 0; i < player_lives; i += 1)
{
var _xx = gui_w - 48 - (i * 70);
draw_sprite(spr_Heart, 0, _xx, 48);
}

If yous haven't already added an instance of this object to a room, go alee and add information technology at present (only one!), then press the Play button Play Icon. Once the game is running press the <Enter> cardinal diverse times to see the lives change.

Animation Drawing Sprites To The GUI Layer

Earlier you leave this example, you lot should experiment with the number of lives and see what happens. At the moment it'southward set to iii, but change the Create Event and the Key Pressed event to fix the value to 5, or 10... if y'all've done everything right, then the code should adapt and draw them all correctly!

Drawing A Healthbar Drawing A Healthbar

This concluding instance covers drawing a healthbar to the GUI layer. There are a number of ways that this can be done, but GameMaker Studio two has a congenital in function specifically for doing healthbars, then that's what we'll be using here, although you can create your own using sprites or shapes too. To beginning with, as before, we need to initialise a varaible to hold the health value, and so add together the following DnD™ or GML into the Create Outcome of the object (after any other lawmaking that may already be at that place):

Defining A Variable Using DnD

player_health = 100;

We want to use the arrow keys to change the health value up or down depending on which arrow key is pressed, and we could exercise that past adding in two Keyboard Pressed <Arrow> events, however it's probably easier to utilize a Step Event and some code to check for the keys, and then get ahead and add a Pace Event now with the post-obit DnD™ or GML:

Step Event DnD Actions

if keyboard_check(vk_up)
{
if player_health < 100
{
player_health = player_health + 1;
}
}

if keyboard_check(vk_down)
{
if player_health > 0
{
player_health = player_health - 1;
}
}

With that done, we can actually get around to drawing the healthbar, which is done in the Depict GUI event, calculation the following (after anything else that is already there):

DnD For The Draw GUI Event To Draw A Healthbar

var _xx = gui_w / two;
draw_healthbar(_xx - 50, 24, _xx + fifty, 40, player_health, c_black, c_red, c_lime, 0, true, true);

Add an instance of this object to a room if yous oasis't already done so (simply one, though!), and then press the Play button Play Icon. Once the game is running press the <Upwardly Pointer> and <Downward Pointer> keys diverse times to encounter the health change.

Animation Of Drawing A Healthbar

Nosotros promise that afterwards doing these examples y'all have a bit more conviction when using GameMaker Studio 2 and a bit more understanding of how it all works. The side by side section will explore how to get these things you've been cartoon to move effectually the room also as accept - and respond to - user input.

Source: https://manual.yoyogames.com/Quick_Start_Guide/Drawing.htm

Posted by: welchnotheeptist.blogspot.com

0 Response to "How To Draw A Sprite In Game Maker Studio"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel