Premium Features
Every level needs an exit. In this lesson, we'll add animated doors that the player can enter to progress to the next area.
Doors are Sprites with animation, stored in an array for easy management:
const doors = [ new Sprite({ position: { x: 767, y: 270 }, imageSrc: './img/doorOpen.png', frameRate: 5, frameBuffer: 10, loop: false }) ]Setting
loop: falsemakes the door animation play once and stop—perfect for an opening door.
Render doors in the animation loop, behind the player:
function animate() { backgroundLevel1.draw() doors.forEach(door => { door.draw() }) player.update() player.draw() }Check if the player overlaps a door and presses the interaction key:
// In keydown event listener case 'w': for (let i = 0; i < doors.length; i++) { const door = doors[i] if ( player.hitbox.position.x + player.hitbox.width >= door.position.x && player.hitbox.position.x <= door.position.x + door.width && player.hitbox.position.y + player.hitbox.height >= door.position.y && player.hitbox.position.y <= door.position.y + door.height ) { player.velocity.x = 0 player.velocity.y = 0 player.switchSprite('enterDoor') door.play() // Start door animation } } breakAdd a play() method to Sprite for triggerable animations:
play() { this.autoplay = true }Doors are the gateway between game areas. Getting them right—with proper animation timing and player interaction—creates satisfying level progression.
Comments
Want to participate?
Create a free Chris Courses account to begin
No comments yet, be the first to add one