|
Automatic sprite group assignmentGM Studio update: The technique described here has been tested in GM Studio 1.0 as of June 2015. A programming technique for GM Studio 1.0 that allows a group of sprites to be assigned to an object by assigning the first sprite of the group to the object and giving the following sprites in the set special namesIn certain types of game (platform game) an object may use one of a number of sprites depending on its movement direction, or other game events. A player in Super Bouncy Ball consists of nine sprites. These are standing still, moving, jumping and squashed facing right and left, plus an additional facing forward sprite used to indicate the active player. There are multiple player objects each needing a different set of sprites. In games where an object only has one sprite it is easy to create other copies or descendants of the object with a different sprite. Where an object uses multiple sprites the sprite names are usually hard-coded into events, making them difficult to change. Other game types benefit as well as platform games. Many games feature objects that can rotate. It used to be considered difficult to have rotation and animation in the same object (although now that sprites can be drawn rotated it is not quite such an issue). It has been demonstrated that rotation and animation can be combined by using an array of sprites such that the multiple images of the sprites contain various rotations and the series of sprites contain successive frames of animation (used in water panic), or each sprite may be fully animated at one rotation so that the series of sprites contains a full rotation (used in the curve platform demo). This technique may not be compatible with Studio 2.0 when it appears as it depends on the sprite name string being available at runtime, and this is information that is usually discarded when a program is compiled. The function "asset_get_index" is not available in the older GM HTML5 version that preceded Studio. In GM8.1 the same functionality may be achieved by "execute_string". Automating sprite group assignment:Firstly the sprites need to be named in a specific way.
In the
In the object information dialog box we set the sprite to the master sprite for that player. The "execute_string" commands fill all the variables with the appropriate sub-sprites. To assign a different player sprite set we only have to change the master sprite to an alternative. Note that each master sprite must be accompanied by a set of correctly named sub sprites. This is about automatic sprite assignment, not automatic sprite generation (though I may cover this someday). Assigning a sprite group to an arrayThings get more fun with arrays. In my "curve_platform" demo I had a number of sprites representing the player in different orientations. If the player runs at a curving surface they stay on it and can even run up walls and along the underside of platforms. In this demo I use arrays to store the sprites. By using arrays I avoid the need to have lots of "If" or "switch" constructs to determine which sprite to use. The arrays still need to be filled though and the brute force method is not fun:
A better way would be to fill the arrays using loops. To allow this I rename the sprites using numbers to represent the actions and orientations as follows:
The code to fill arrays with that lot is as follows:
That's it, it sets up the array walk_sp with the regular sprites and run_sp with all the rotated sprites. I can substitute the sprite-set easily provided the replacement set is suffixed the same way as the original set. Footnote:I am aware that GM Studio has made it a lot easier to do top down games with rotation, however you may want to use pre-rendered lighting effects on your sprites or a projection that isn't totally top down, in which case you will still need rotated sprites. Further footnoteOften it is possible to think of the create and destroy events as being equivalent to the constructor and destructor in object oriented programming. Under GM Studio they are called reliably in almost all cases with the exception of the save and load game keys.
|