This is part of a series of articles that go over the steps I’m taking to learn Flash CS3. The project’s goal is to develop an RPG similar to those published by Square in the mid/late 1990’s. (Secret of Mana, Final Fantasy VI, etc..)
So we now have a hero, a world, and some pretty dim-witted NPCs. Watching those NPCs bumble around, walking into walls, and pushing our Hero around is getting dull rather quickly. Let’s give them some teeth.
Milestone: Battle Engine
User Story: Enemies will attack me and cause damage. I will be able to attack them back and kill them.
Acceptance Criteria:
- There should be the concept of “hit points” on both the hero and the enemies
- Attacks should be range-constrained
- Attacks should have variable damage
- Attacks will be subject to a wait timer
I’m modeling my battle engine off those found in the Secret of Mana or Seiken Densetsu series. Everything is in real-time but attacks can only be performed when a cool-down period elapses.
Prototype 1
If you remember from my last post, there was a line of code in the Enemy.as file that called the function attack(). Basically what I’m doing is testing the Enemy worldX and worldY coordinates to see if it is within 50 pixels of our hero. If it is, the Enemy stops all movement and launches its attack() function.
The enemy attack() function is quite small. It does a quick Math.random() to determine an attack power then calls ourHero.hitMe(damage). The Hero class figures out what it needs to do with that damage.
Within the Hero class there are a few things I’ve added:
- public variables: maxHp, hp
- functions: hitMe()
When the enemy calls hitMe(), the Hero can do something with that incoming damage. This would be a good place to add defense modifying formulas but in my example I’m just letting that damage pass on through. The hero’s hp-=damage (duh). And finally I’m calling a test TextField class I wrote to show the damage on screen.
Within my Engine class, I’ve added a check every frame to update my HeroPanel, which contains the health meter.
Ok great, now my Hero has the defensive skills of a Q-Tip.
Prototype 2
Similar to my Enemy class, I’ve added an attack() function within my Hero class. When the user presses “space”, this function will be called.
The attack() function then cycles through all of the enemyList[] objects testing for a collision. If one is found, enemyList[i].hitMe(damage) is called (basically telling the target enemy to accept some damage). On a successful attack, wait=maxWait and the hero isn’t allowed to attack until wait=0. I also went ahead and added a gainXP() function and xp variable to the hero.
Within my Enemy class I add the same variables and functions as I did with my Hero in prototype 1. (maxHP, hp, hitMe()). Within hitMe() there is also a test to see if hp<=0. If it is, then we call removeSelf(). This function removes all active event listeners within the enemy, calls Hero.gainXP(xp), and finally removes itself from the stage and all arrays.
And that’s basically it. An incredibly simplistic battle engine. There’s obviously a lot missing here including:
- damage/defense modifiers based on stats
- attack animations
- better battle logic
- death of the Hero
But these will have to wait.






