Go Back   kirupaForum > Flash > ActionScript 1.0/2.0

Reply
 
Thread Tools Display Modes
Old 11-30-2009, 04:15 AM   #1
clc2007
Registered User
[CS3 & AS2] Using Data Instead of Derivation to Refine Behavior in AS2?

I'm working on my first AS2 project and could use some advice. I'm trying to create two types of objects, a soldier and a villager, using a shared NPC class:

Code:
 
class ObjNpc extends MovieClip
{
 public function onLoad()
 {
  trace(_level.legalStateNames); // this is the list of legal states
 }
 // state machine code follows...
}
The ObjNpc class will know how to handle three states -- "Idle," "Fight," and "Run" -- but I want soldiers to never enter the "Run" state and villagers to never enter the "Fight" state.

Also, I want to create the soldier and villager objects in separate flash files, which will be loaded into the scene at runtime.

So, the Soldier.FLA and Villager.FLA files will each contain a movie clip that derives from the ObjNpc class, but how can I specify their list of "legal" states without deriving a separate class for each one?

This seems like a reasonable place to use component properties, using strings like "Idle;Fight" and "Idle;Run," but for various reasons I can't use components on this project. I also can't use anything but AS2.

I tried adding a line to each object's flash file in frame 0, like this:

// Soldier.FLA contains this line of code in frame 0:
_level.legalStateNames = [ "Idle", "Fight" ];

// Villager.FLA contains this line of code in frame 0:
_level.legalStateNames = [ "Idle", "Run" ];

This approach won't work if a scene contains both a soldier and a villager, because the second object to load will overwrite _level.legalStateNames, so both NPCs will end up sharing the same state table.

Is there any way to specify data like this in a FLA file? I really don't want to make separate classes for each type, since their behaviors are so close. Component properties seem like a natual fit, except I can't use them for other reasons. Are there any other options?

Thanks!
clc2007 is offline   Reply With Quote

Sponsored Links (Guests Only) - Register | Need Help?
 

Old 12-04-2009, 05:26 AM   #2
scottgroovez
Registered User
You said you don't want separate classes as they are so close, this is where inheritance would fit in. You'd build the main class with the common properties/methods and then extend from this your soldier and villager class. These two classes would overwrite or add properties & methods for the differing functionality.

If you don't want to sub-class then you could add Boolean properties to your ObjNpc class that switch on/off available states. Something like canFight, canIdle, canRun. Then when you initialise the instance you can activate what ever states that particular NPC has.

First method is better though.
scottgroovez is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 03:35 PM.

SUPPORTERS:

kirupa.com's fast and reliable hosting provided by Media Temple. flash components
Creative web apps. Make your own free flash banners and photo slideshows.
Check out the great, high-quality flash extensions. Buy or sell stock flash, video, audio and fonts for as little as 50 cents at FlashDen.

Flash Transition Effects

Flash Effect Tutorials

Digicrafts Components
Flash effects. Art without coding. Upload, publish, deliver. Secure hosting for your professional or academic video, presentations & more. Screencast.com
Streamsolutions Content Delivery Networks Flipping Book - page flip flash component.
Flash-Gallery.com - Get your flash photo gallery (flash component or swf gallery Learn how to advertise on kirupa.com
 

cdn
content delivery network (cdn)

Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd. Copyright 2010 - kirupa.com Copyright 2010 - kirupa.com