views:

478

answers:

5

I made a movieclip symbol called "SwordHolder" then went through ALL my animations and put the sword holder in the right spot on another movieclip (took me HOURS). I figured I could change the image in the SwordHolder movieclip symbol so I did all that.

Well come to find out I'm having a really hard time figuring out how to change/add/remove an image with the swordholder movie clip symbol without creating a new instance of it (obviously thats not what I want).

How do I access a movieclip (in my library) directly without making a new instance of it? I figured I could just do like [var refSwordHolder: SwordHolder; refSwordHolder.addChild(sword);] but it doesnt seem to allow me to do that.

Also the quicker the help the better... I need to figure this out within a few hours :(

Thanks!

A: 

A MovieClip symbol is a subclass of MovieClip. You would need to define public static variables for this class and possibly public static methods as well to be able to manipulate the class itself, rather than its instances.

You cannot do addChild() to a class since the class is just a blueprint. It doesn't exist on your display list.

As far as I know, you would need to write the class entirely in AS3 to allow for such behavior.

Hope this helps.

Lior Cohen
Thanks Lior, this is a real bummer and means I will have to redo my whole code!
CodeJustin.com
Again, as far as I know. Others may be able to offer some more information :)
Lior Cohen
A: 

If you are using Flash CS3 (since you using MovieClips you must be) why don't you just edit the symbol in the editor? If you look in the Flash Library you will see the Symbol with the name "SwordHolder" and you can double click it to edit it. This is the easiest option.

If you right click the Symbol in the Library, go to Properties then you can check the box labelled Export for ActionScript. One of the fields below is a Class field where you can specify a class to associate with the Symbol. So you could create a class called SwordHolder (place it in the class path of the file) and associate it this way with the symbol and do whatever code you want for each instance in the constructor. By default the class name is the same as the symbol name.

e.g.

package
{
    import flash.display.MovieClip;

    public class SwordHolder extends MovieClip
    {
        public function SwordHolder()
        {
            super();

            // do what needs to be done
        }
    }
}
James Fassett
A: 

how about making that SwordHolder class a Singleton...this sounds like a good candidate.

Singleton is probably the easiest design pattern out there. The idea is that by following this pattern you make sure there will be only one instance of your class created through out your whole app.

The SharedObject is an example of that. You don't create and instance using new SharedObject, you use SharedObject.getLocal() or getRemote().

here's a rough idea:

package yourcoolgame.assets{
   import flash.display.MovieClip;

   public class SwordHolder extends MovieClip{
      private static var instance:SwordHolder;

      public static function getInstance():SwordHolder {
         if (instance == null) {
            instance = new SwordHolder(new SingletonLock());
          }
         return instance;
       }

      public function SwordHolder(lock:SingletonLock):void {
         if (lock == null) {
            throw new Error("Error: Instantiation failed: Use SwordHolder.getInstance() instead of new.");
          }
       }
    }
}
internal class SingletonLock {}

The class should throw and error when you try to create and instance using new. You create and instance anywhere in your application using SwordHolder.getInstance() and it will always be a reference to the same instance. you can add children etc. then

If this might be more than what you need and you're looking for something simple, since you're using the IDE...you could add you symbols( other swords ) in different frames of that clip and use labels...than you would have something like

public function setSword(swordID:String):void{
this.gotoAndStop(swordID);
}

Hope it helps.

George Profenza
A: 

Dont you have a reference to the character? Then you should be able to get a reference to the sword through the hieraki.

If not you can make a SwordHolder class that saves a reference of the instance in a static variable. Like this:

package MyGame
{
    import flash.display.MovieClip;

    public class SwordHolder extends MovieClip
    {
        private static var _Instance:SwordHolder = null;

        public function SwordHolder()
        {
            if(_Instance == null)
                _Instance = this;
            else
                throw new Error("Only one SwordHolder instance allowed");
        }

        public static function get Instance():SwordHolder
        {
            return _Instance;
        }
    }
}
Lillemanden
A: 

So if i understood you right, you placed instances of the SwordHolder movieclip with the IDE and now you would like to change the image/sprite that your SwordHolder movieclip instances display?

You could do the Singleton thing (that a few others have suggested as well) and then just load and display a different image/sprite in your SwordHolder movieclip. So you would need to have a way in your SwordHolder class to change the image that it displays.

JHollanti