sprite_create_from_surface(index, x, y, w, h, removeback, smooth, xorig, yorig);
|index||The index of the surface to create from.|
|x||The x position to copy from.|
|y||The y position to copy from.|
|w||The width of the area to be copied (from the x position).|
|h||The height of the area to be copied (from the y position).|
|removeback||Indicates whether to make all pixels with the background colour (left-bottom pixel) transparent.|
|smooth||Indicates whether to smooth the edges.|
|xorig||Indicates the x position of the origin in the sprite.|
|yorig||Indicates the y position of the origin in the sprite.|
With this function you can create a sprite from a previously
initialised surface. The x and y coordinates that you input are
relative to the (0,0) position of the surface (the top left
corner) and not the game window, nor the view if you have one
active. The width and height arguments are in pixels and define the
width and height of the part of the surface to use.
Setting the "removeback" argument to true will remove a colour from the sprite, by checking the bottom left pixel of the sprite for the colour there and then using that as the colour to be removed.
If you choose the "removeback" option, you may also want GameMaker: Studio to smooth the edges of the sprite by setting the "smooth" argument to true. All this does is create a semi-transparent border around the edges of the sprite after it has had its background removed.
Finally you can also specify the x and y origin for the sprite. This is the point where the sprite is "fixed" onto the instance that uses it, and is always calculated as relative to the 0,0 top left corner of one sprite sub-image. So, for example, a sprite that is 32 x 32 pixels with these values set to (16,16) will have its origin in the center.
By default all new sprites have their bounding boxes calculated automatically (the exact bbox will depend on the size and transparency of the sprite), however you may wish to customise this, in which case you should also use the function sprite_collision_mask.
NOTE: When you create a sprite in GameMaker: Studio with this method you must remember to remove it again (with sprite_delete) when no longer needed, otherwise there is risk of a memory leak which will slow down and eventually crash your game.
surf = surface_create(32, 32);
draw_sprite(spr_Body, 0, 0, 0);
draw_sprite(spr_Clothes, 0, 0, 0);
draw_sprite(spr_Hair, 0, 0, 0);
spr_custom = sprite_create_from_surface(surf, 0, 0, 32, 32, true, true, 16, 16);
The above code creates a surface and stores its index in the local variable "surf". It then targets that surface, clears it and draws several sprites on top of each other. Finally it creates a new sprite from the composite image drawn on the surface and assigns its index to "spr_Custom" before freeing up the memory used by the surface.