Skip navigation
Currently Being Moderated

How to inherit base class properties ?

Mar 26, 2012 1:34 AM

Tags: #problem #flash #movieclip #actionscript3 #function

Hi All,

 

I have one doubt.Can anyone clear this doubt?

 

Steps:

 

1)I have created One movieClip & named as MyComp

2)I export this movieclip to actionscript.Class name for this movieclip is MyComp.

3)After that,Dynamically I have created 5 instance for this class & added to stage.

4)Now i need to change width of MyComp. it should reflect on previous 5 instance too.

 

I tried like,

I have created MyComp.as file in same location. There I tried to inherite DisplayObject properties Width.

 

Example :

 

In authoring time,I have one movieclip in my library which contains one rectangle shape..Im adding 5 instance of that movieclip in stage.

After that i get into one movieclip,and im increasing width of that movieclip by increasing Shape width. it ll get reflect in all instance..

 

i need this thing in actionscript.... is it possible?

 

 

Thanks...

 
Replies
  • Currently Being Moderated
    Mar 26, 2012 3:55 AM   in reply to ShanSun88

    you can't do what you are trying to do the way you are trying to do it.

     

    I have included some code here (pure AS3, no timeline or library) which you might be able to adapt to your needs

     

    Basically I have created a static property (_rectWidth which controls the rectangles width) on the MovieClip class that contains the rectangle and a render() function that redraws the rectangle tot he correct width. Note you will have to call render() after setting a new width to see the rectangle drawn at the new  width.

     

    Then in the main class I create 5 rectangles with random colours and positions.

    The main class also listens for the "arrow up" and "arrow down" keys to change the rectangle's size. Note: I change the size of the rectangle and then run through all of the Main classes children (which are all RectMC's) and call render();

     

    package 
    {
              import flash.display.Sprite;
              import flash.events.Event;
              import flash.events.KeyboardEvent;
     
              public class Main extends Sprite 
              {
     
                        public function Main():void 
                        {
                                  if (stage) init();
                                  else addEventListener(Event.ADDED_TO_STAGE, init);
                        }
     
                        private function init(e:Event = null):void 
                        {
                                  removeEventListener(Event.ADDED_TO_STAGE, init);
                                  // entry point
     
                                  for (var i:int = 0; i < 5; i++)
                                  {
                                            var col:uint = Math.floor(Math.random() * 0xffffff);
                                            var rectX:int = Math.floor(Math.random() * stage.stageWidth);
                                            var rectY:int = Math.floor(Math.random() * stage.stageHeight);
                                            var rectMC:RectMC = new RectMC(col);
                                            rectMC.x = rectX;
                                            rectMC.y = rectY;
                                            addChild(rectMC);
                                  }
     
                                  stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
                        }
     
                        private function onKeyDown(e:KeyboardEvent):void 
                        {
                                  switch (e.keyCode)
                                  {
                                            case 38:
                                                      RectMC.rectWidth += 20;
                                                      break;
     
                                            case 40:
                                                      RectMC.rectWidth -= 20;
                                                      break;
                                  }
     
                                  for (var i:int = 0; i < numChildren; i++)
                                  {
                                            var rectMC:RectMC = getChildAt(i) as RectMC;
                                            rectMC.render();
                                  }
                        }
     
              }
     
    }
     
    

     

     

    package  
    {
              import flash.display.MovieClip;
              import flash.events.Event;
     
              /**
               * ...
               * @author steven O'Boyle
               */
              public class RectMC extends MovieClip 
              {
                        protected static var _rectWidth:int = 100;
                        protected static var _rectHeight:int = 50;
     
                        protected var _rectColour:uint;
     
                        public function RectMC(col:uint) 
                        {
                                  _rectColour = col;
                                  render();
                        }
     
                        public function render():void
                        {
                                  graphics.clear();
     
                                  graphics.beginFill(_rectColour);
                                  graphics.drawRect(0, 0, _rectWidth, _rectHeight);
                                  graphics.endFill();
                        }
     
                        public static function set rectWidth(value:int):void
                        {
                                  _rectWidth = value;
                        }
     
                        public static function get rectWidth():int
                        {
                                  return _rectWidth;
                        }
              }
     
     
    }
     
    
     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points