Creating Custom Events In Flash AS3 (ActionScript 3)
I've only been working with Flash AS3 for a very short time, but I'm already very happy with it. Even though it is not as accessible as AS2, the power of the new language features are unmistakable. One very powerful feature of AS3 is the built-in event handling. Event handing has existed in Flash as far back as Flash 5, but with AS3 Adobe has forced the issue to the point where using events is really no longer a choice. If you want to create any sort of efficient AS3 application, you will have to learn event handling.
Basic event handling is fairly straight-forward. We'll start-out by creating a simple game that uses events. This "game" will randomly place circles on the screen that need to be clicked-on. We will start by creating a Game class. The first EventListener required for this Game class to work is an an Event that is fired for onEnterFrame. The code addEventListener(Event.ENTER_FRAME, run) makes the Ball class listen for ENTER_FRAME events and when one is fired, it calls the run() function.
The run() function is used to create new balls on the screen. There is a 10% chance a new ball will be created every frame. After a ball is created, we want to have our game class listen for a CLICKED event from the ball. This event will fire when the mouse is clicked on ball. We will use this event to remove the ball from our Balls array, and score points for the player. The line of code tempBall.addEventListener(Ball.CLICKED, ballClicked); makes sure that our Game class will listen for a CLICKED event. The code below illustrates the complete Game class.
| [cc lang="javascript" width="550"]/** * ... * @author Default * @version 0.1 */ package { } public function run(e:Event):void { for (var i:int = 0; i < balls.length; i++) { } public function ballClicked(e:ScoreEvent) { } } } |
To shed more light on what the ballClicked() function is doing in the Game class, I must first show you the code for the Ball class:
| [cc lang="javascript" width="550"]package { import flash.display.MovieClip; import flash.events.Event; import flash.events.MouseEvent; public class Ball extends MovieClip{ static public var CLICKED:String = "clicked"; public var down:Boolean; public var speed:int =3; public function Ball(tx:Number, ty:Number) { x = tx; y = ty; addEventListener(MouseEvent.CLICK, mouseClicked); down=true; } public function mouseClicked(e:MouseEvent):void { public function run():void { } else { } } |
This class starts out a lot like the Game class. In the constructor we add this line of code to listen for a mouse CLICK event: addEventListener(MouseEvent.CLICK, mouseClicked); To facilitate this we add a variable named CLICKED as String -"clicked". This will function our our event identifier. The code looks like this: static public var CLICKED:String = "clicked"; We also need to add a function named mouseClicked that will be called when the Ball hears a MouseEvent.CLICKED event. The run() method of this class (called from the Game class' run() method) move the Ball up and down the screen as the ball "bounces" off the top or bottom, it's speed increases. This "speed" is what we use for "score". The faster the Ball, the more points the player scores.
This is where our problem arises. We need to dispatch an event back to our Game class that does two things: tells Game that a ball was clicked, and provide a score based on the speed of the Ball. We cannot do this with a regular Event, because we need to attach the score to message. Instead, we create our own Event that extends Event named "ScoreEvent" and send that event instead. Here is what what class looks like:
| [cc lang="javascript" width="550"]package { import flash.events.Event; public class ScoreEvent extends Event{ public var score:int; public function ScoreEvent(scr:int, type:String, bubbles:Boolean = false, cancelable:Boolean = false){ super(type, bubbles,cancelable); score = scr; } } } |
ScoreEvent takes the same parameters as an Event class, but adds a scr which is used to set its score property. Before we do that, we call the Base Class with super(type, bubbles,cancelable); to make sure we initialize the MouseEvent as if it was a standard Event. Now Let's go back and look at the code for the Game class's ballClicked function;:
| [cc lang="javascript" width="550"] public function ballClicked(e:ScoreEvent) { trace("Clicked:"+ Ball(e.target)); for (var i:int = balls.length-1; i >= 0; i ) { if (balls[i] == e.target) { removeChild(balls[i]); balls.splice(i,1); score+=e.score; ScoreText.text = score.toString(); ; } } } |
Recall that we set-up the game class to call ballClicked when a CLICKED event was fired from Ball: tempBall.addEventListener(Ball.CLICKED, ballClicked); This function simply finds the Ball that was clicked on my looping through the balls array and trying to match the ScoreEvent's target property to the Ball that fired it. When we find one, we increase score by the score set in ScoreEvent's score property.
There are probably many other ways to solve this problem with the score, and other Events that could be used to listen to mouse clicks and fire events, but this tutorial simply illustrates how to create a custom Event and use it in your code.
Download the sample code here.
Required Playing – Atari 2600 Dragonstomper.
In 1981 a small company named Arcadia was formed with the purpose of cashing in on the craze that was home video games. The company was quickly renamed Starpath when it was learned that at least one copyright was already registered with an association to video games (Emersion Arcadia) and was in use. This small group of bright individuals set out not, like so many others, to just slap retread game carts on the video game world , but to create something new, and maybe something brilliant. This team of industry veterans and young hot shots spent most of a year, fueled by caffeine and junk food, reverse engineering the Atari 2600. The result was a keen knowledge of the inner workings of the hardware and an invention that would result in some of the finest 8-bit games ever made. The Starpath Supercharger allowed the Atari 2600 to access 6K of ram as opposed to the 128 bytes that others had to work with. Also, it made use of cheap cassette tapes to store and load game rom and data as necessary. Unfortunately, the game industry died well before the Supercharger could completely revolutionize the industry. In its wake though, the code warriors who crafted this device and its fine software library, would go on to make a huge impact on the 80's video game industry. The Supercharger is an item that is sought after and coveted by collectors today. The games remain some of the very few Atari 2600 games to never have a public free release, but were released as a CD Rom called Stella Gets a new Brain in 1999.
Steve and I grabbed an Arcadia Supercharger in the first allotment sold to stores in 1982. I'm sure if we hadn't sold it to get a Vectrex a few years later it could possibly be worth some good money on Ebay today. We had Phaser Patrol and Communist Mutants from Space originally, and also purchased Escape From the Mind Master, Dragonstomper, and Frogger not long after. These games were as good as any computer games we had played, and some, like Escape and Dragonstomper had a level of depth we had yet to experience. Also they were inexpensive! A quality Starpath game could be had for under 20 bucks at the time. Other, inferior games, were selling for almost twice that price.
Stephen Landum was the designer and programmer for Dragonstomper. He also created Communist Mutants from Space, Frogger, and Suicide Mission for the Supercharger. He would go on to survive the video game crash as Epyx purchased Starpath in its wake. There, he was a principal designer and game programmer on the Handy portable (Later bought or stolen by Atari and renamed the Lynx - stories abound on the subject). His Blue Lightening is still considered to be one of the great Lynx games. He is also credited with programming and or creating such classics as Summer Games, Pit Stop II, and the Temple of Apshai Trilogy. Stephen was also instrumental in creating the 3DO game console.

(Box Cover and back)
Dragonstomper, originally titled Excalibur, was in the first wave of games released by Starpath. Other notable first-wave titles were Phaser Patrol (Star Raiders like game), Fireball (an interesting take on Breakout), Communist Mutants From Space (Galxian like game), and Suicide Mission (Asteroids). Dragonstomper was one of the first true role playing games modeled on Dungeons and Dragons released for the 2600. I remember Steve and I spending hours in front of the living room TV patiently collecting enough gold and magic items to make it to the town. We persevered and eventually beat the game in what turned out to be one of our first true video game triumphs.
Dragonstomper is probably best appreciated for its innovative music, easy to use interface, and multiple solutions to problems. For example, you can win the game in more than one way - kill dragon or just steal the amulet back. You can also traverse the cave to the dragon with rope, or by jumping down and then healing yourself (if you forgot to buy rope). The simple menu interface using just a 4-way stick and a single button was as elegant as anything today, while the hilarious music tracks (i'm in the money!) that trigger during game play are create a perfect atmosphere. It was also the first taste of real roleplaying on a home console, and an amazing achievment forther 2600.
Dragonstomper takes place in three different locations, each needing a separate load from the cassette media. You begin the game by traveling the Enchanted Countryside in an attempt to obtain the magic, items, and gold necessary to make it across the bridge and into the Oppressed Village. Once in the village, your task is to buy weapons, items and magic necessary to defeat the dragon. You can also attempt to recruit warriors from the village to join you in your quest. The final stage of the game is the journey through the Dragon's cave and eventual final battle.
The provided story is seems pretty standard today, but was ahead of its time in comparison to other early 80's console titles: You are the Dragonstomper, the only hero capable of defeating a dragon obsessed with evil. This dragon obtained his power by tricking a power hungry druid into dropping a powerful magic amulet in his cave. The dragon used the amulet to kill, oppress, scare the villagers into submission. The once powerful king's brave knights have become the dragon's evil henchmen, so no one, not even the King's powerful wizard can stop him.
You begin the game with 400 gold, and enough strength (23) and dexterity (23) to put up a fight against a few of the evil creatures inhabiting the land. You must fight your way to better items, weapons, and magic, while keeping a careful watch on your strength. If it reaches 0 you must reset the game to resurrect yourself. This section of the game is huge by Atari 2600 standards. There are 20 screens full of landscape to explore.

Your on-screen avatar is little more than a simple rectangle, but what a powerful rectangle he turns out to be. You begin your quest with a few gold and no weapons. As you fight your way around the landscape, you will find many items of use and collect much gold in the process. Some of these items (charms, crosses, potions, rings and staffs) are enchanted with magic that help to increase (or in some cases decrease) your two basic stats (strength and dexterity). The effect is random until you use an item for the first time. Once the effect is set (increase or mildly decrease you stat)s, it stays the same until you reset the game. You start the game with 23 in each basic stat. You don't gain experience points (ala DnD) but your simple goal is to increase your strength (basically hit points combined with DnD strength) and dexterity as much has possible. . This will allow you to become the fierce warrior needed to defeat the evil dragon.
You can visit a church and donate gold to increase your strength. There are many other places to visit such as castles, huts. temples, grass lands and more. Most buildings will be locked until you find a suitable key (a very important, but scarce resource), or have an axe to break down the door.You must battle snakes, demons, beetles, monkeys slime, maniacs, ghouls, spiders and more. There are 9 different places to visit, 11 items and weapons, and 12 different creatures to do battle with. The hand axe is the only hand to hand weapon you can find and it greatly improves you ability in close combat. Visiting places basically results in you being attacked or receiving one of the game objects. Also, there are various traps scattered around the countryside and you must avoid them or hope you have a high enough dexterity to get out quickly.
The higher your dexterity, the harder it is for you to be hit by monsters, and the easier they will be able to be hit by you. The higher your strength, the better you will do in battle, and the longer you will live. You will be repeatedly attacked while on your quest. The fighting action takes places via a simple menu interface. You can run from most battles without too much trouble and this will be extremely important early on as your strength will be sapped and it replenishes very slowly without cleric or magical help.

To make it across the bridge and into the Oppressed Village, you must have 1500 in gold with a strength and dexterity at max (52 each). Again, there is an alternative method of gaining access, if you can figure out what the bridge guard will take as a bribe. In town you will be able to spend your acquired gold on a number of items that will increase your chances of defeating the dragon and stealing back the amulet crystal. You can purchase magical scrolls such as BLAST, FLASH, PROTECT, UNLOCK, VISION, and STUN in the magic shop. Blast will take some serious damage from an enemy combatant, while the others will aide you in various ways. Flash will light up an area of the cave, protect will help confuse the enemy into missing their strike on your avatar, unlock will remove magic barriers and help emancipate the amulet crystal.The stun spell will freeze your enemy, while the vision scroll will allow you to see traps.
You can also equip yourself with healing elixirs (increase dexterity), medicine (cure poisons), vitamins (increase strength) at the hospital, and even buy a longbow, rope, lanterns and other equipment at the trade shop. You have the ability to sell off all of your unneeded equipment from part one and use the money on these and other important items. This is good because only the hand axe really is of any use in the third part of the game. If you have enough gold (or precious gems) left, you can attempt to recruit at least one of the three warriors in town to join you in the quest.

When you have finished shopping and recruiting, it is time to load part three and head into the Dragon's cave. Before you make it to the dragon, you will have to avoid guardians, pits, poison darts, and more. Make sure you have enough medicine, rope and other items to help you through this section. Once in the dragons lair, it's a fight to the death. Ranged weapons, magic, and a lot of healing potions are needed to succeed. You don't even have to kill the dragon if you can some how get to the locked away amulet crystal (the unlock scroll should help), the game will be won.

Dragionstomper is an excellent game. It holds interest even today. Casual and hardcore game designers of today should pay reverence to titles such as these. Not only was it innovative in every way, but it has that virtually unknown and almost impossible to create quality that makes you want to keep playing until you finish, no matter what your real world responsibilities may be.
Besides Stephen Landrum, here is a brief bio on some of the other brillant people who worked at Starpath:
Craig Nelson was a hardware engineer at Starpath. His knowledge of the 2600, from having worked at Atari, was pivotal for the small company. He was the principal designer of the Supercharger hardware. He is also credited with creating the legendary Rogue title for Epyx.
Scott Nelson was Craig's brother and as a game programmer he created Fireball and Survival Island for the Supercharger. Scott also moved on to Epyx (like some of his fellow Starpath brothers) and programmed versions of Summer Games I &II, The Games Winter and Summer, as well as Chips Challenge (Lynx), and the Secret of Monkey Island. His name has shown up on some more recent Midway titles such as Gauntlet 7 Sorrows for the PS2.
Dennis Caswell created Escape From the Mindmaster, Labyrinth, Party Mix and Phaser Patrol for the Supercharger. At Epyx, Dennis created one of the all-time great 8bit classics, Impossible Mission.
Steve Hales was 19 when he became a programmer for Starpath. He created the Asteroids-like game, Suicide mission. He went on to program such classics as Fort Apocalypse, California Games and Sim City, as well as many other games up until the mid '90s.
Halcyon Days Interview with Steve Hales
8bitrocket article on Steve Hales' Fort Apocalypse
The Unofficial Epyx & Summer Games Homepage (Interview with Stephen Landrum)
Forbes magazine comparing the 2600 and the xbox 360 - with a discussion Dragonstomper