Risorse – Sprite

Le funzioni seguenti forniscono informazioni su una sprite:

sprite_exists() ind Dato l’indice di una sprite restituisce: esiste?, il nome, il numero di sottoimmagini, la larghezza, l’altezza, trasparente?, smoothed?, preloaded?, x-offset, y-offset
sprite_get_name() ind
sprite_get_number() ind
sprite_get_width() ind
sprite_get_height() ind
sprite_get_transparent() ind
sprite_get_smooth() ind
sprite_get_preload() ind
sprite_get_xoffset() ind
sprite_get_yoffset() ind
sprite_get_bbox_left() ind Dato l’indice di una sprite restituisce informazioni sul bounding box: left, right, top, bottom, modalità (0=automatic, 1=full image, 2=manual)
sprite_get_bbox_right() ind
sprite_get_bbox_top() ind
sprite_get_bbox_bottom() ind
sprite_get_bbox_mode() ind
sprite_get_precise() ind Dato l’indice di una sprite restituisce se utilizza precise collision checking.

In alcune situazioni potresti voler salvare su file la bitmap corrispondente a una sottoimmagine della sprite.

Per questo puoi utilizzare le funzioni seguenti:

sprite_save() ind
subimg
fname
Standard Edition
Salva la sottoimmagine subimg della sprite ind nel file fname.
Deve essere un file .png.
sprite_save_strip() ind
fname
… tutte le sottoimmagini … come una striscia …

Modificare sprite

The following routines are available for changing sprite properties:

sprite_set_offset(ind,xoff,yoff) Sets the offset of the sprite with the given index.

The following routines can be used to create new sprites and to remove them.

sprite_duplicate(ind) Creates a duplicate of the sprite with the given index. It returns the index of the new sprite. When an error occurs -1 is returned.
sprite_assign(ind,spr) Assigns the indicated sprite to sprite ind. So this makes a copy of the sprite. In this way you can easily set an existing sprite to a different, e.g. new sprite.
sprite_merge(ind1,ind2) Merges the images from sprite ind2 into sprite ind1, adding them at the end. If the sizes don’t match the sprites are stretched to fit. Sprite ind2 is not deleted!
sprite_add(fname,imgnumb,removeback,smooth,xorig,yorig) Adds the image stored in the file fname to the set of sprite resources. Many different image file types can be dealt with. When the image is not a gif image it can be a strip containing a number of subimages for the sprite next to each other. Use imgnumb to indicate their number (1 for a single image). For gif images, this argument is not used; the number of images in the gif file is used instead. removeback indicates whether to make all pixels with the background color (left-bottom pixel) transparent. smooth indicates whether to smooth the edges. xorig and yorig indicate the position of the origin in the sprite. The function returns the index of the new sprite that you can then use to draw it or to assign it to the variable sprite_index of an instance. When an error occurs -1 is returned.
sprite_replace(ind,fname,imgnumb,removeback,smooth,xorig,yorig) Same as above but in this case the sprite with index ind is replaced. The function returns whether it is successful.
sprite_add_sprite(fname) Adds the sprite stored the file fname to the set of sprite resources. The file must be a .gmspr file that is saved in the sprite property form in GameMaker. As this file contains all sprite settings, no further arguments are required. The function returns the index of the new sprite that you can then use to draw it or to assign it to the variable sprite_index of an instance. When an error occurs -1 is returned.
sprite_replace_sprite(ind,fname) Same as above but in this case the sprite with index ind is replaced. The function returns whether it is successful.
sprite_create_from_screen(x,y,w,h,removeback,smooth,xorig,yorig) Creates a sprite by copying the given area from the screen. removeback indicates whether to make all pixels with the background color (left-bottom pixel) transparent. smooth indicates whether to smooth the boundaries. This makes it possible to create any sprite you want. Draw the image on the screen using the drawing functions and next create a sprite from it. (If you don’t do this in the drawing event you can even do it in such a way that it is not visible on the screen by not refreshing the screen.) The other parameters are as above. The function returns the index of the new sprite. A work of caution is required here. Even though we speak about the screen, it is actually the drawing region that matters. The fact that there is a window on the screen and that the image might be scaled in this window does not matter.
sprite_add_from_screen(ind,x,y,w,h,removeback,smooth) Adds an area of the screen as a next subimage to the sprite with index ind.
sprite_create_from_surface(id,x,y,w,h,removeback,smooth,xorig,yorig) Creates a sprite by copying the given area from the surface with the given id. removeback indicates whether to make all pixels with the background color (left-bottom pixel) transparent. smooth indicates whether to smooth the boundaries. This makes it possible to create any sprite you want. Draw the image on the surface using the drawing functions and next create a sprite from it. The function returns the index of the new sprite. Note that alpha values are maintained in the sprite.
sprite_add_from_surface(ind,id,x,y,w,h,removeback,smooth) Adds an area of the surface id as a next subimage to the sprite with index ind.
sprite_delete(ind) Deletes the sprite from memory, freeing the memory used.

The following routine exists to change the appearance of a sprite.

sprite_set_alpha_from_sprite(ind,spr) Changes the alpha (transparancy) values in the sprite with index ind using the intensity values in the sprite spr. This cannot be undone.

Finally, you can change the collision checking mask for the sprite with the following routine. Please realize that this does not work correctly when an instance with that sprite is already present in the room.

sprite_collision_mask(ind,sepmasks,bboxmode,bbleft,bbtop,bbright,bbbottom,kind,tolerance) Changes the collision mask for the sprite with index ind. sepmasks indicates whether there must be separate collision masks for all subimages. bboxmode is the bounding box mode (0=automatic, 1=full image, 2=user defined). bbleft, bbright, bbtop, bbbottom indicate the bounding box in case of user defined bounding box. You can use 0 for all these values in the other cases. kind is the kind of mask (0=precise, 1=bounding box, 2=disk, 3=diamond). tolerance indicates the tolerance in the transparency value (0=no tolerance, 255=full tolerance).