Creating enemy death and disappearing mechanics in top-down games
💡 Struggling with enemy AI and combat mechanics in your games? 🚀 Get Help Now
CombatCoder_Pro
Posted on January 24, 2024 • Intermediate
⚔️ Can’t make enemies die/disappear in my top-down sword fighting game!
Hey everyone! I’m working on a top-down sword fighting game and I’m having trouble making enemies disappear when they get hit by the player’s weapon.
The issues I’m facing:
- Enemies don’t disappear when touched by the sword
- Sometimes all enemies disappear at once
- Collision detection seems inconsistent
- Want to add death animations and effects
I’m using clones for the enemies. Any help would be amazing! 🙏
GameMechanics_Expert
Replied 1 hour later • ⭐ Best Answer
I know exactly what you’re dealing with @CombatCoder_Pro! Enemy death mechanics can be tricky. Here’s the complete solution:
⚔️ Enemy Death System Architecture
Here’s how a proper enemy death system works:
🎯 Problem Analysis
The main issues are usually:
- Broadcast messages affecting all clones instead of just one
- Collision detection happening too frequently
- Missing proper clone identification
✅ Proper Enemy Death System
Here’s the correct way to handle enemy deaths:
Method 1: Direct Clone Detection (Recommended)
// Enemy clone code when I start as a clone set [enemy_health v] to [3] set [enemy_id v] to (pick random [1000] to [9999]) forever // Enemy AI and movement move [2] steps if on edge, bounce // Check for weapon collision if <touching [weapon v]?> then if <(weapon_active) = [1]> then change [enemy_health v] by [-1] set [weapon_active v] to [0] // Prevent multiple hits // Visual feedback set [color v] effect to [25] wait [0.1] seconds set [color v] effect to [0] // Check if dead if <(enemy_health) < [1]> then // Death animation repeat [5] change [ghost v] effect by [20] change size by [-10] wait [0.1] seconds end // Drop items or add score change [score v] by [10] delete this clone end end end end
Weapon Sprite Code:
// Weapon sprite when flag clicked set [weapon_active v] to [0] forever // Follow player or mouse go to [mouse-pointer v] // Attack on click if <mouse down?> then if <(weapon_active) = [0]> then set [weapon_active v] to [1] // Attack animation repeat [3] turn right [30] degrees wait [0.05] seconds end turn right [-90] degrees wait [0.3] seconds set [weapon_active v] to [0] end end end
🚀 Advanced Enemy System
For more sophisticated combat:
// Advanced enemy with different types when I start as a clone set [enemy_type v] to (pick random [1] to [3]) if <(enemy_type) = [1]> then // Fast enemy set [enemy_health v] to [1] set [enemy_speed v] to [4] switch costume to [fast_enemy v] else if <(enemy_type) = [2]> then // Tank enemy set [enemy_health v] to [5] set [enemy_speed v] to [1] switch costume to [tank_enemy v] else // Normal enemy set [enemy_health v] to [3] set [enemy_speed v] to [2] switch costume to [normal_enemy v] end end forever // AI behavior based on type if <(enemy_type) = [1]> then point towards [player v] move (enemy_speed) steps else if <(enemy_type) = [2]> then // Tank moves slowly but shoots move (enemy_speed) steps if <(timer) mod [60] = [0]> then create clone of [enemy_bullet v] end else // Normal enemy wanders move (enemy_speed) steps if <(pick random [1] to [30]) = [1]> then turn right (pick random [-45] to [45]) degrees end end end // Universal collision check if <touching [weapon v]?> then if <(weapon_active) = [1]> then // Damage calculation set [damage v] to [1] if <(enemy_type) = [1]> then set [damage v] to [2] // Fast enemies take more damage end change [enemy_health v] by (0 - (damage)) // Knockback effect point away from [weapon v] repeat [5] move [3] steps wait [0.02] seconds end if <(enemy_health) < [1]> then // Different death effects per type if <(enemy_type) = [1]> then // Fast enemy explodes repeat [8] create clone of [particle v] end else if <(enemy_type) = [2]> then // Tank enemy drops power-up create clone of [power_up v] end end delete this clone end end end end
💥 Adding Visual Effects
Make deaths more satisfying:
// Particle effect for enemy death when I start as a clone set [particle_speed v] to (pick random [3] to [8]) point in direction (pick random [1] to [360]) set size to (pick random [20] to [50])% set [color v] effect to (pick random [0] to [100]) repeat [15] move (particle_speed) steps change [particle_speed v] by [-0.3] change [ghost v] effect by [7] turn right [5] degrees end delete this clone // Screen shake effect define screen_shake repeat [5] change x by (pick random [-3] to [3]) change y by (pick random [-3] to [3]) wait [0.05] seconds end go to x: [0] y: [0]
🛡️ Preventing Common Issues
Important tips to avoid problems:
- Use weapon_active variable to prevent multiple hits per attack
- Give each clone a unique ID if you need to target specific enemies
- Use “delete this clone” not broadcasts for individual deaths
- Add invincibility frames after taking damage
- Test collision detection with different sprite sizes
This system will give you reliable, satisfying enemy death mechanics! ⚔️
CombatCoder_Pro
Replied 25 minutes later
@GameMechanics_Expert This is incredible! 🤩 The enemy death system works perfectly now and the visual effects are amazing!
One question - how can I make enemies drop different items based on their type when they die?
LootDesigner_Sam
Replied 45 minutes later
@CombatCoder_Pro Great question! Here’s how to create a smart loot system:
// Loot drop system define drop_loot (enemy_type) (x_pos) (y_pos) if <(enemy_type) = [1]> then // Fast enemy drops coins if <(pick random [1] to [100]) < [80]> then create clone of [coin v] set [drop_x v] to (x_pos) set [drop_y v] to (y_pos) end else if <(enemy_type) = [2]> then // Tank enemy drops rare items if <(pick random [1] to [100]) < [30]> then create clone of [rare_gem v] set [drop_x v] to (x_pos) set [drop_y v] to (y_pos) else if <(pick random [1] to [100]) < [60]> then create clone of [health_potion v] set [drop_x v] to (x_pos) set [drop_y v] to (y_pos) end end else // Normal enemy drops common items if <(pick random [1] to [100]) < [50]> then create clone of [small_coin v] set [drop_x v] to (x_pos) set [drop_y v] to (y_pos) end end end // Call this before deleting enemy clone drop_loot (enemy_type) (x position) (y position) delete this clone
This creates a realistic loot system where different enemies drop appropriate rewards! 💎
AIExpert_Luna
Replied 1 hour later
Excellent work everyone! 🎮 Here are some advanced tips for even better enemy systems:
- Boss enemies: Multiple phases with different attack patterns
- Enemy formations: Groups that work together
- Adaptive difficulty: Enemies get stronger as player progresses
- Status effects: Poison, freeze, stun mechanics
- Smart AI: Enemies that learn player patterns
Remember to balance challenge with fun - players should feel accomplished when defeating enemies!
Vibelf_Community
Pinned Message • Moderator
🚀 Master Combat Game Development!
Amazing discussion everyone! For those looking to create even more advanced combat systems, our community can help you implement:
- ⚔️ Complex weapon systems and combos
- 🤖 Advanced enemy AI behaviors
- 🎯 Precise hitbox and collision systems
- 🎨 Professional combat animations
📚 Related Topics
- How to create weapon upgrade systems?
- Building boss fight mechanics
- Advanced AI pathfinding techniques
Ready to create epic combat experiences? Get personalized guidance from our expert game development tutors!