HScript: Creating and adding sprites - SlightlySimple/FNF-Restructure-Engine GitHub Wiki

A "sprite" is any type of graphic that appears on the screen, whether it be a simple image, an animation, or a solid block of color. If you want to make something appear in one or a few songs only, make an in-game cutscene like the ones in Week 7, or make your own custom menus, you will be working with sprites.

Sprite Adding Functions

  • CreateSprite(asset) will create a sprite for you to use. asset is the path, starting in the "images" folder, to an image or spritesheet to be used for your sprite. You will want to assign your sprite to a variable, for example mySprite = CreateSprite("myImage"). This function also takes optional x and y arguments after the asset argument. These control the position on screen that the sprite starts at.
  • add(sprite) will place the sprite in the stage. sprite is the variable name of the sprite you want to add, so for the example above, you would type add(mySprite). By default, this will place the sprite on top of the entire stage including the characters, so you may want to use the next function instead.
  • insert(index, sprite) will place the sprite in the stage, at a specific location in the order of sprites. If, for example, you wanted the sprite to be behind Boyfriend, you would type insert(game.members.indexOf(game.player1), mySprite).
  • remove(sprite) will remove the sprite from the stage. Usually, this will leave the slot the sprite was in vacant, so you may want to do remove(mySprite, true) to ensure the slot is completely cleared. Otherwise, add may try to fill this slot the next time you use it.

These functions won't always use the variable game. They may use this for scripted sprite groups (such as the dialogue box) or FlxG.state for custom menus.

Sprite Variables and Functions

These variables and functions are meant to be ran on the sprites themselves. For example, by doing mySprite.alpha = 0.5 or mySprite.updateHitbox()

  • x and y are the top-left corner of the sprite. They can both be set at once with the function setPosition(x, y).
  • width and height are how wide and tall the sprite is, in pixels. They should not be set directly, but can be controlled with the function setGraphicSize(width, height).
  • scale is how much the sprite is stretched or squashed. scale.x controls the horizontal stretching, and scale.y controls the vertical stretching. They can both be set at once with the function scale.set(x, y).
  • updateHitbox() updates the sprite's hitbox, fixing it's position and size to account for scaling. It is highly recommended to run this after changing a sprite's scale.
  • scrollFactor is how closely the sprite follows the camera's movement. scrollFactor.x controls the horizontal factor, and scrollFactor.y controls the vertical factor. They can both be set at once with the function scrollFactor.set(x, y). You can also type this function as just scrollFactor.set() if you want the sprite to follow the camera perfectly.
  • screenCenter will place the sprite in the center of the stage. This can also be screenCenter(X) or screenCenter(Y) to only center the sprite along one axis.
  • angle is how much a sprite is rotated. Usually, a sprite will be rotated around it's center.
  • color is what color a sprite is. This can either be a FlxColor entry such as FlxColor.RED or a hexadecimal number such as oxFFFF0000.
  • alpha is how transparent, or see-through, a sprite is. It is a range between 0 and 1. 0 means the sprite is invisible.
  • visible is whether or not the sprite can be seen. It can either be true or false.

Animated Sprite Variables and Functions

These variables and functions pertain specifically to spritesheets that are meant to be animated.

  • addAnim(name, prefix, fps, loop) will add an animation to the sprite. name is the animation name as the game recognizes it. prefix is the animation name as it's written in the xml file. fps is the speed of the animation, in frames per second. loop controls whether or not the animation loops infinitely. This function also takes an optional fifth argument, which is a list of numbers controlling which frames of the animation to use.
  • addOffsets(name, offsets) will tell the sprite to use the specific offsets when playing the animation. name is the name of the animation to affect. offsets is an array of two numbers that control the x and y offsets of the sprite when playing this animation.
  • playAnim(name) will make the sprite play an animation as defined earlier. This function has the three optional arguments force, reverse, frame. If force is true, the animation will return to the beginning even if the sprite is already in the middle of playing it. If reverse is true, the animation will play backwards. If frame is present, it is a number controlling which frame of the animation to start on.
  • idles is a list of animations to cycle through on every beat of the song. danceSpeed is how many beats to wait between idles. If danceSpeed is 2, the sprite will idle every other beat, for example.