S
Shadow Gamer
Guest
GM Version: Studio (recent versions)
Target Platform: Windows
Download: see code below
This is a simple little tutorial on how to get parts of a sprite as another sprite that you can use elsewhere inside your game. All credit goes to Kepons, who basically just gave me the code.
In this example, we'll be dividing a enemy up into 4 sections for use in, say, a special effect, like shattering. Please note this code splits sprites into an even amount of sections horizontally and vertically, however, it can easily be modified to split unevenly.
The script outputs a 2 dimensional array called spr, which contains a sprite that corresponds a place on a "grid" of the original sprite, known as xp and yp. For example, in the code above, spr[1,1] would be the bottom-right chunk. I highly recommend doing whatever you want to be doing inside the innermost FOR loop, since you can easily use xp and yp when placing and using chunks/objects in your game.
I know it seems like a lot of code, but you really only have to modify the variables in the Player Defined variables section if you don't want to go too deep into it.
Note: Clean up your sprite when you are done with them using sprite_delete()! Failure to do so will result in a memory leak.
Anyway, enjoy! If you have any comments or questions, just say them.
EDIT: As many users have pointed out, this is a rather inefficient method if you don't absolutely have to use it. In many cases, you can simply create a template sprite for collisions and then use draw_sprite_part to draw the chunk you desire.
Target Platform: Windows
Download: see code below
This is a simple little tutorial on how to get parts of a sprite as another sprite that you can use elsewhere inside your game. All credit goes to Kepons, who basically just gave me the code.
In this example, we'll be dividing a enemy up into 4 sections for use in, say, a special effect, like shattering. Please note this code splits sprites into an even amount of sections horizontally and vertically, however, it can easily be modified to split unevenly.
Code:
//PLAYER DEFINED VARIABLES (Just change these)
startingSprite=spr_enemy; //Starting sprite is an enemy
startingIndex=0; //Starting image_index is 0
sectionCount=4; //Number of even sections to divide into
newOriginX=-1000 //Use -1000 for center, otherwise just use the new chunk origin you want
newOriginY=-1000 //Use -1000 for center, otherwise just use the new chunk origin you want
//Setting/Changing variables for use below
sectionCount*=0.5; //We need to do this because we are splitting both horizontally and vertically
startingOriginX=sprite_get_xoffset(startingSprite); //X Origin of starting sprite
startingOriginY=sprite_get_yoffset(startingSprite); //Y Origin of starting sprite
ChunkW=sprite_get_width(startingSprite)/sectionCount; //Width of new sprite (width of sprite/2)
ChunkH=sprite_get_height(startingSprite)/sectionCount; //Height of new sprite (height of sprite/2)
if (newOriginX==-1000) {
newOriginX=ChunkW*0.5; //Center
newOriginY=ChunkH*0.5; //Center
}
var surf=surface_create(ChunkW,ChunkH);
surface_set_target(surf);
for (var xp=0;xp<sectionCount;xp++) {
for (var yp=0;yp<sectionCount;yp++) {
//Drawing to surface
draw_clear(c_white);
draw_sprite(startingSprite,startingIndex,(xp*-ChunkW)+startingOriginX,(yp*-ChunkH)+startingOriginY);
//Creating the actual chunk
spr[xp,yp] = sprite_create_from_surface(surf,0,0,ChunkW,ChunkH,false,false,newOriginX,newOriginY);
}
}
//Setting the Target back to the application surface and destroying the surface
surface_reset_target();
surface_free(surf);
I know it seems like a lot of code, but you really only have to modify the variables in the Player Defined variables section if you don't want to go too deep into it.
Note: Clean up your sprite when you are done with them using sprite_delete()! Failure to do so will result in a memory leak.
Anyway, enjoy! If you have any comments or questions, just say them.
EDIT: As many users have pointed out, this is a rather inefficient method if you don't absolutely have to use it. In many cases, you can simply create a template sprite for collisions and then use draw_sprite_part to draw the chunk you desire.
Last edited by a moderator: