0
votes

I'm in the process of creating a game. I'd like my main character to be able to programmatically equip weapons/armor whatnot. He has a hand that if I manually edit a sword into, the animation works fine, however I don't want him to start with a sword equipped.

Unfortunately I can't access an instance of this hand because it's only present for 10 frames during the attack animation. If I set an instance name for the hand in the timeline (Frames 110-120) I can't refer to it from the character class.

Is there a way to access my "arm" class and programmatically add my sword sprite to the associated movieclip without having an accessible instance of the arm class? I've considered swapping out the arm movieclip for one with a sword already added - but I run into the same problem of not being able to access an instance which is only present during some of the frames.

I've tried to make my question as clear as possible, hopefully it worked. Thank you very much for any insight you can provide.

1
Just keep the hand and sword present all the time, and change their .visible property as needed.BadFeelingAboutThis
Is this the best way to do it if I end up having 100+ different weapons? Not that I'm planning on it, but I'm curious as to whether that's the way it's traditionally done.Aclwitt
You need to show your code of how you're currently "uquipping" for anyone to really help you.BadFeelingAboutThis
How one would "traditionally" do this is with class files and not timeline code - as timeline code is clumsy. Best way would be to have code in your arm/hand class (or timeline) that runs when it's created to know whether it should have a sword (and which one) and create it (or not) as needed.BadFeelingAboutThis
RE: Visibility with 100+ weapons - it would be fine if had a master sword MovieClip and kept your individual swords as different frames of that timeline, then you only have once instance loaded at any time.BadFeelingAboutThis

1 Answers

0
votes

Here is the easiest (though not best) way you could do this.

On your main (root) timeline, put the following code:

var weaponId:int = 1;  //never modify this directly, use the function below so the event gets dispatached. - This value will correspond to frame in your weapon movieClip that will contain the correct graphic of the selected weapon.

function changeWeapon(id:int):void {
    weaponId = id;
    this.dispatchEvent(new Event("WeaponChange")); //this will dispatch an event, so anything listening for this event will know it's been changed
}

On your hand timeline, put the following code:

MovieClip(root).addEventListener("WeaponChange",updateWeapon,false,0,true); //listen for when the weapon changes

updateWeapon(); //update the weapon immediately to the current value

function updateWeapon(e:Event = null):void {
    weapon.gotoAndStop(MovieClip(root).weaponId); //assuming your hand timeline has a movieClip on it called weapon and each frame of weapon corresponds to the weaponId
}

Here is what's happening:

Whenever you change weapons (however that's done), call the function changeWeapon() on the root timeline. This will set the variable and dispatch an event. If the hand is present, it will see the event and update itself. If the hand is not present, when the hand get instantiated next it will look at the global variable and change accordingly.