6 Replies Latest reply on Mar 5, 2010 8:19 AM by AttaBoy2

    MovieClip won't work as component in Flex 3

    AttaBoy2 Level 3

      I have a movieclip with Actioncript only that works fine in the Flash CS3 authoring environment.

      This is the code in the MovieClip:

       

      var squares:Array = new Array;

       

      setup();

       

      function setup():void {

       

          for (var i = 0; i < 25; i++) {
              var square:Sprite = new Sprite();
              var a = this.x;
              var b = this.y;
             
              square.graphics.beginFill(Math.random() * 0xffffff);
              squares.push(square);
              squares[i].graphics.drawRect(a, b, 100, 100);
              squares[i].x = i*3;
              squares[i].y = i*3;
              squares[i].filters = [];
              stage.addChild(squares[i]);
          }
      }

       

      var lastTarget:*;

       

      for (var j = 0; j < squares.length; j++) {
          squares[j].addEventListener(MouseEvent.MOUSE_DOWN, dragMovie);
          squares[j].addEventListener(MouseEvent.MOUSE_UP, dropMovie);
          squares[j].buttonMode = true;
      }
      function dragMovie(event:MouseEvent):void {
          event.target.startDrag();
      }

       

      function dropMovie(event:MouseEvent):void {
          lastTarget = event.currentTarget;
          event.target.stopDrag();
      }

       

      When I convert to Flex 3 component and export to Flex3.  When run I get an error message telling me that setup();  is a referenct to a null object or somehing like that.  I thought maybe I could fix the problem by adding addEventListener( Event.ADDED_TO_STAGE, onAdded );  

      then running setup(); from onAdded but that didn't work.