Question

so I haven't been able to find a direct response to this situation. as practice with Flash Builder AS3 I'm making a text-based adventure game.

I have items, and for items I have equippable items, and within equippable items I have Head, Legs, Chest, etc.

The Head equippable items, leg equippable items, etc all share the same traits as they are all equippable items, though they may vary slightly in the future so I'd like to have them as separate classes.

When I went to do this as I'd expect (nesting the classes) I get errors about not nesting classes, and then other errors beyond that about having multiple classes in a file, etc.

What would an advisable way to do this be?!

Thanks for your time.

Here is some code...

My broken classes code:

package classes
{
    public class Item
    {
        public var iName:String;
        public var iDesc:String;
        public var isRemovable:Boolean;
        public function Item(iName:String, iDesc:String, isRemovable:Boolean )
        {
            var name:String = iName;
            var desc:String = iDesc;
            var removable:Boolean = isRemovable;
        }
    }
}
class Equippable extends Item{
    function Equippable(pName:String, pSpeedMod:Number, pAttackMod:Number, pLocation:String)
    {
        var name:String = pName;
        var speedMod:Number = pSpeedMod;
        var attackMod:Number = pAttackMod;
        var location:String = pLocation;
    }
}
class Head extends Equippable{
}
class Chest extends Equippable{
}
class Legs extends Equippable{
}
class Belt extends Equippable{
}

My MXML file (which shows how early into this project I am!):

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
        xmlns:s="library://ns.adobe.com/flex/spark" 
           xmlns:mx="library://ns.adobe.com/flex/mx">

    <fx:Script>
        <![CDATA[

        import classes.*;

        private function createRooms():void {
            var NorthRoom:Room = new Room("North Room","This is the North Room.", "None", "None", "Central Room", "None");
            var EastRoom:Room = new Room("East Room","This is the East Room.", "None", "None", "None", "Central Room");
            var SouthRoom:Room = new Room("South Room","This is the South Room.", "Central Room", "None", "None", "None");
            var WestRoom:Room = new Room("West Room","This is the West Room.", "None", "Central Room", "None", "None");
            var CentralRoom:Room = new Room("Central Room","This is the Central room.", "North Room", "East Room", "South Room", "West Room");
        }

        public function createItems():void {

        }

        private function buttonClick(event:Event):void {
            TextArea.text = "Button number " + event.target.id + " has been pressed.";
        }

        var ThePlayer:Player = new Player()

        while(1){


        }


        ]]>
    </fx:Script>
    <mx:TextArea left="20" height="250" width="600" id="TextArea"/>
    <mx:Button width="100" click="buttonClick(event)" y="270" x="520" label="Inventory" id="InvBTN"/>
    <mx:Button width="100" click="buttonClick(event)" y="310" x="520" label="Examine Area" id="ExArea"/>
    <mx:TextArea left="20" height="20" width="300" y="270" id="CEntry"/>
    <mx:Button width="100" click="buttonClick(event)" y="310" x="20" label="OK" id="SEntry"/>
</s:Application>
Was it helpful?

Solution

You should make separate AS3 files for each of the available classes. Basically it's all, but you might also think about restructurizing your class tree - say, equippable items could just have a property "slot" that will have a meaning of where this item can be equipped, say EQUIP_LEG or EQUIP_HEAD as constants (integer). Or you can split equippables into weapons, armor, magic-only stuff maybe (jewelry, talismans, floating devices, etc etc). Weapons have more in common than just equippables, armors too (they differ in slot, of course, and maybe weapons could get atached to legs too? It's up to you to decide), jewelry IMO is closer to armors, but can become a separate class.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top