Shin's site
_
Home
GB Studio resources

This page
_
Top
Setting up the Player and Sword sprites
Setting up the Player script
Setting up the Enemy script
Adding polish
More resources
Title
_
GB Studio 3.1+ Tutorial.
Easy sword attacks in Adventure mode.
Last updated 2/2/2023.

Introduction
_
This tutorial was written for GB Studio 3.1.0 and is subject to change! A video version of this tutorial is available here:



The addition of persistent projectiles in 3.1 made melee attacks much simpler to set up. This tutorial guides you through setting up a simple sword attack for Adventure mode, and will also work for Top-down mode. It involves four main steps:

Setting up the Player and Sword sprites
_
Set up the Player like a regular Top-down or Adventure mode player by setting the Animation Type to 'Four Directions + Movement' and adding the sprite tiles as desired. For this tutorial, the Player's canvas size should be 16px*16px, and the collision box size should be 16px*16px or less. Set up the Sword sprite by setting the Animation Type to 'Four Directions' and add the sprite tiles as desired The canvas size should be 16px*16px, and the collision box size should be 14px*14px or less. Center the collision box on the sprite, in this example by setting both its X and Y to 1. Giving the Sword a collision box of 14px*14px or less ensures that there is a 1px gap between the Player's collision box and Sword's collision box. This prevents some collision inconsistencies caused by the Sword overlapping the Player in certain circumstances.

Setting up the Player script
_
Prepare a new Top-down or Adventure type scene with the Player sprite sheet that you set up previously. Add the below script to the scene's On Init script, ensuring that the checkboxes and projectile options match the image. A simple explanation of the script is as follows: To customise the sword, consider changing the following parameters in the projectile options:

Setting up the Enemy script
_
Add an actor to the scene that can be used for the Enemy. Set the Collision Group of the Enemy to 1. The Collision Group of the Enemy should match one of the Collision Groups specified in the Collide With option of the Sword projectile event. In other words, the Sword projectile should Collide With the Enemy's Collision Group. Add the below script to the Enemy's On Hit script under the Collision Group 3 tab. This should match the Collision Group of the Sword projectile event. A simple explanation of the script is as follows: This is a very simple example of a script that deactivates the Enemy when it is hit by the Sword. You can change this script to whatever behaviour you would like to occur when the Enemy is hit by the Sword.

Adding polish
_
Finally, here are a few options to improve on the basic sword attack.

You could add a swing animation to the Sword sprite in the sprite editor.

You could add an attack animation state to the Player sprite. Add 'Set Actor Animation State' events to the Player script to change to it when attacking.

You could add health and invincibility frames to the Enemy. Below are examples of two scripts that achieve this when added to the Enemy actor. The first should be added to the Enemy's On Init script. A simple explanation of the On Init script is as follows: The second should be added to the Enemy's On Hit script that matches the Collision Group of the Sword projectile event. A simple explanation of the On Hit script is as follows:

More resources
_
Now you can make a simple sword attack in GB Studio, but you will need more than that to make a game. The following tutorial will show you how to make better enemies in Adventure mode:



If you have any queries about the tutorial contents (NOT general questions about GB Studio) contact me via my Twitter. Constructive feedback is appreciated.