This article is a stub. It may be incomplete, unfinished, or have missing parts/sections. If the article can be expanded, please do so! There may be suggestions on its talk page. (May 2018) |
A hitbox is a shape that detects collision with other objects. The purpose of a hitbox is to detect collision with a simpler shape such as a circle or a rectangle, as more detailed images can affect how smoothly the game runs and can cause glitches with collision physics. Like the name suggests, hitboxes are often simple shapes, being the same general size as the sprite it is sensing collisions for.
Uses of a Hitbox
Hitboxes can be used to avoid movement bugs in platformers and other similar games, and are also used for more detailed characters/player avatars. They generally make projects run smoother. When making a hitbox, it is essential that it is roughly the same size as the player; otherwise the hitbox can make games very inaccurate.[citation needed] Here are some methods in which they can be programmed:
Different Sprite Method
This method uses two sprites.
Drawing a Hitbox
To start, create a new sprite for the hitbox and open the "Costumes" tab. If you haven't already, backpack (or download onto your computer if you are using the offline editor) the first costume for your character, then upload it into a blank vector costume in your hitbox sprite. Using the rectangle tool, draw a rectangle over your character and make it as small possible without any part of the character showing. When you remove the character from behind, you will have your hitbox.
Programming the Hitbox
After you have made the sprite, you will need a platformer script inside the scripts area. If you don't know how to make one, a tutorial can be found here.
Linking the Two Sprites
Now that you have made the hitbox, you need to connect the character to it. Put this script in the character sprite:
when gf clicked forever go to (Hitbox v) end
Different Costume Method
This method uses one sprite with two different costumes.
Drawing the Costume
Duplicate the original costume. Rename it to "Hitbox". Then, create a rectangle that is roughly the same size and dimensions as the character. You can now delete the original character in the "Hitbox".
Using the Costume
The sprite will need a platformer script. After it has those scripts, put these blocks inside the main loop:
when gf clicked forever switch costume to (Hitbox v) ... //main loop switch costume to (Original Costume v) end
When you run your project, everything should be smoother.