Reset this lesson?

Your edits to this lesson are deleted and its starting files load again.

Zelda-3 gives Link his sword. Press Z for one swing in the direction he’s facing. There’s nothing to hit yet, so this lesson also adds a debug mode that draws the sword’s reach, along with Link’s body and the room’s walls.

Hitboxes and Hurtboxes

A fighting game needs two kinds of rectangle:

  • A hurtbox is where something can be hurt. Link already has one, the 8 × 8 box at his feet from Zelda-1.
  • A hitbox is where something deals damage. The sword’s hitbox is the tile in front of Link.

Keeping them separate is what makes combat feel fair. Link’s hurtbox is small, so a near miss misses. The sword’s hitbox is a full tile, so a swing in roughly the right place lands.

PlayerState gains a getHitbox() that returns undefined, meaning “this state can’t hurt anything”. Only states that attack override it.

The Sword State

Pressing Z in Idle or Walk changes to PlayerSwordState. Both states share the check through tryStartAction() on PlayerState, and call it before they handle movement:

src/states/player/PlayerState.js
92
tryStartAction() {
93
const { input } = this.game;
94
95
if (input.isKeyPressed(Input.KEYS.Z)) {
96
this.player.stateMachine.change(PlayerStateName.Sword);
97
return true;
98
}
99
100
return false;
101
}

The sword state plays one swing and schedules everything else on its own Timer:

src/states/player/PlayerSwordState.js
78
this.animation = new Animation(
79
frames,
80
PLAYER_SWORD_DURATION / frames.length,
81
1,
82
);
83
this.game.sounds.play(SoundName.Sword);
84
85
this.timer.after(PLAYER_SWORD_ACTIVE_START, () => {
86
this.isHitboxActive = true;
87
});
88
this.timer.after(PLAYER_SWORD_ACTIVE_END, () => {
89
this.isHitboxActive = false;
90
});
91
this.timer.after(PLAYER_SWORD_DURATION, () => {
92
this.player.stateMachine.change(PlayerStateName.Idle);
93
});

The 1 passed to Animation makes it play once instead of looping. Link stands still during the swing, since only Walk moves him, and he can’t leave the room mid-swing because the sword state doesn’t override canLeaveRoom.

Frame-timed Damage

The swing is 0.3 seconds long, but the blade is only out from 0.1 to 0.2 seconds. That window is the only time getHitbox() returns a rectangle:

src/states/player/PlayerSwordState.js
107
getHitbox() {
108
return this.isHitboxActive ? this.player.getTileInFront() : undefined;
109
}

The wind-up and follow-through are drawn but harmless. Timing damage to the animation this way is how action games make attacks readable so that you see the blade before it can hurt.

Lining Up the Frames

The swing sprites are wider or taller than Link, because they include the blade. Each frame carries an offset so Link’s body stays still while the sword sweeps around him:

src/states/player/PlayerSwordState.js
43
down: down.map((sprite) => ({
44
sprite,
45
isMirrored: false,
46
offsetX: TILE_SIZE - sprite.width,
47
})),

Actor.renderFrame() adds offsetX and offsetY when it draws. Without them, Link would jump sideways on every frame of the swing. The strip at the top of the demo is the swing straight from link.png: the frames are 16, 29, and 32 pixels wide, and the sheet puts Link’s body at a different spot in each. Pick a direction and let the swing loop, or step through its frames one at a time. Each frame is drawn at Link’s position plus its offset, and the readout shows the arithmetic:

Debug Mode

Press Q to toggle debug mode. DEBUG_TOGGLE_KEY and DebugSettings live in src/config/game.js, and PlayState.updateDebug() flips DebugSettings.isEnabled when the key is pressed. With it on, you’ll see:

  • Solid tiles in red and exit passages in green, from renderCollisionDebug() in roomCollision.js.
  • Link’s hurtbox in blue, and the sword’s hitbox in orange, only while it’s active.
  • A watch panel from lib/Debug.js with Link’s state, position, facing, and health, and the room’s name and whether it’s panning.

The panel reads its values from functions listed in PlayState.watchDebugValues():

src/states/PlayState.js
50
debugPanel.watch("Link", {
51
state: () => player.state.constructor.name,
52
position: () =>
53
`(${player.position.x.toFixed(2)}, ${player.position.y.toFixed(2)})`,
54
facing: () => player.facing,
55
health: () => `${player.health} / ${player.maxHealth}`,
56
});

Each function runs every frame while the panel is visible, so you can add your own while you work: anything you can write as () => value can go in the panel.

Put the sword in front

Turn on debug mode and swing: the sword’s hitbox sits on Link instead of in front of him, because getTileInFront() in src/objects/Actor.js returns his own tile.

Make it return the tile one step ahead:

  1. Store that tile’s corner: const front = getPositionInDirection(this.position, this.facing, TILE_SIZE);. The helper is already imported, and One Pixel at a Time in Zelda-1 explains it.
  2. Return a rectangle at that corner: { x: front.x, y: front.y, width: TILE_SIZE, height: TILE_SIZE }.

Then swing the sword. The Challenge tab gets a ✓ once you’ve seen it work in the game.

📚 References

Powered by WebContainers
Files
Preparing Environment