A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: Question on superclass/subclass movieclip linkage

  1. #1
    Senior Member
    Join Date
    Jul 2008
    Posts
    391

    Question on superclass/subclass movieclip linkage

    The title probably didn't make sense cause I don't know how to describe it in a short statement.

    I have a Hero class that extends a Unit class that extends a SelectableObject class.

    SelectableObject
    PHP Code:
    public class SelectableObject extends MovieClip {
        var 
    selected:Boolean false;
            
        var 
    friendly:Boolean;
            
        public function 
    SelectableObject () {
            
    this.addEventListener (Event.ADDED_TO_STAGEonObjectAddedfalse0true);
        }
            
        private function 
    onObjectAdded (e:Event) {
            
    this.addEventListener (MouseEvent.ROLL_OVERonObjectRollOverfalse0true);
        }
            
        private function 
    onObjectRollOver (e:MouseEvent) {
            if (
    selected == false) {
                if (
    friendly == true) {
                    
    //selector.gotoAndStop (2);
                
    } else {
                    
    //selector.gotoAndStop (4);
                
    }
            }
        }

    I have a movieclip in the library that links to the Hero. Inside the movieclip is another movieclip with the instance name "selector".

    In the Hero I can use selector.gotoAndStop (n) because the movieclip is linked to that class, but I can't do that for the SelectableObject because it's not linked to that.

    Is there a way to make selector.gotoAndStop (n) work for the SelectableObject? I can't even do var something:* = this.selector because the movieclip isn't linked to it.

    Or should I just remove the instance name for the selector in the movieclip so I can declare var selector:* = this.getChildAt (1) in the SelectableObject (the selector being the second child)?

  2. #2
    Senior Member Computer Dork's Avatar
    Join Date
    Mar 2001
    Location
    St. Louis
    Posts
    1,026
    If I'm understanding this correctly...

    for simplicity sake:

    SelectableObject -> Unit -> Hero right?

    and you have an MC in the library that has an attached 'hero' class to it, correct?


    You would be a lot better off simply taking your whole actionscript scope OUT of the library - and bringing the library to your actionscript. Take the class name linkage off of your MC in the library - make it extend flash.display.MovieClip only.

    In your selectable object scope, which is the super class for this whole chain, you can set up your framework so that everything in the library is pulled in - not the other way around. Set up your three classes like so:

    PHP Code:
    package
    {
        
    import flash.display.MovieClip;
        
    import flash.display.Sprite;
        
        public class 
    SelectableObject extends Sprite
        
    {
            protected var 
    _asset:MovieClip;
            
            public function 
    SelectableObject($asset:MovieClip null)
            {
                
    _asset $asset;
                
    init();
            }
            
            protected function 
    init():void
            
    {
                
    //SelectableObject functionality
            
    }
        }
        
    }


    package
    {
        
    import SelectableObject;
        
    import flash.display.Sprite;
        
    import flash.display.MovieClip;
        
        public class 
    Unit extends SelectableObject
        
    {
            
            public function 
    Unit($asset:MovieClip null)
            {
                
    super($asset);
                
    init();
            }
            
            
    override protected function init():void
            
    {
                
    //unit functionality
            
    }
            
        }
        
    }


    package
    {
        
    import Unit;
        
        
    import flash.display.Sprite;
        
    import flash.display.MovieClip;
        
        public class 
    Hero extends Unit
        
    {
            
            public function 
    Hero($asset:MovieClip null)
            {
                
    super($asset);
                
    init();
            }
            
            
    override protected function init():void
            
    {
                
    //hero functionality
            
    }
            
        }
        

    The beauty in subclassing is that all of the children have all functionality the superclass above has. So basically, you should be able to see where your functionality needs to go now - if it effects all three classes, it belongs in SelectableObject. If the Hero is the only one that gets to import that specific MC in your library, the Hero class should be the only one that calls to the selector to play.

    Does that answer your question?

  3. #3
    Senior Member cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,449
    You can create an Abstractclass, which has all the functions, which are in common to all subclassed movieclips. The Abstractclass itself will never be called directly. In your example SelectableObject would be this class. Since however you are calling SelectableObject, it cannot be the Abstractclass.
    - The right of the People to create Flash movies shall not be infringed. -

  4. #4
    Senior Member
    Join Date
    Jul 2008
    Posts
    391
    Well I'm creating a sort of RTS style unit system. The selector is the green circle that appears under the unit when you hover over it. The SelectableObject and Unit will never be called. I'll only call the subclasses of Unit like Hero or Enemy1 or Enemy2.

    There's also never going to be a movieclip in the library that links to SelectableObject or Unit. Only the movieclips linked to the subclasses of Unit will have the selector inside them. So I'm just wondering what the best way is for SelectableObject to get access to selector since it handles that part for the units.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center