3 Replies Latest reply on Jul 6, 2007 11:59 AM by SymTsb

    Setting width/height too early??

      I noticed if I set width and/or height before adding it to displayList, the displayObject simply won't show up. The following code is a cut&paste from livedoc (SimpleButton), except that I add a line of "button.width=20".
      If, however, I move the line to after addChild(), things work normally.
      Why?:confused: :confused: :confused:

      package {
      import flash.display.*;
      import flash.events.*;

      public class SimpleButtonDemo extends Sprite {
      public function SimpleButtonDemo( ) {
      // Create a simple button and configure its location
      var button:SimpleButton = new SimpleButton( );
      button.x = 20;
      button.y = 20;

      //// ################# Added by me. Button won't show up any more.
      button.width = 80;

      // Create the different states of the button, using the
      // helper method to create different colors circles
      button.upState = createCircle( 0x00FF00, 15 );
      button.overState = createCircle( 0xFFFFFF, 16 );
      button.downState = createCircle( 0xCCCCCC, 15 );
      button.hitTestState = button.upState;

      // Add an event listener for the click event to be notified
      // when the user clicks the mouse on the button
      button.addEventListener( MouseEvent.CLICK, handleClick );

      // Finally, add the button to the display list
      addChild( button );

      //// ############### This works, if added here!!!!
      // button.width = 20;

      // Helper function to create a circle shape with a given color
      // and radius
      private function createCircle( color:uint, radius:Number ):Shape {
      var circle:Shape = new Shape( );
      circle.graphics.lineStyle( 1, 0x000000 );
      circle.graphics.beginFill( color );
      circle.graphics.drawCircle( 0, 0, radius );
      circle.graphics.endFill( );
      return circle;

      // Event handler invoked whenever the user presses the button
      private function handleClick( event:MouseEvent ):void {
      trace( "Mouse clicked on the button" );
        • 1. Re: Setting width/height too early??
          SymTsb Level 2
          Try putting your width setting directly after you set your hit states. Prior to this point, your button doesn't contain a single pixel of content. As a result, width and height can't be adjusted.
          • 2. Re: Setting width/height too early??
            jkok2000 Level 1
            What confuses me is why the button does NOT show up at all. I thought the width set too early would be ignored and newly calculated width (by flash) take effect.

            Setting it after addChild() works - that make me think flash play has some assumptions on w/h when creating a new displayObject.
            • 3. Re: Setting width/height too early??
              SymTsb Level 2
              You're using the simple button class. Just because you instantiate an object of this class doesn't mean you've added anything to the button.

              What I'm trying to tell you is that addChild has nothing to do with it. Simple Button just happens to be dependent on what is defined inside the button states themselves.

              For instance, comment out your hitStates leaving your width call after the addChild and you should notice the same behavior as before. By defining the button states, you are actually adding content to the container that will be drawn to the display when addChild is called.

              You can define your height and width anytime after you have defined the states for the button. At this point, Flash will then have content to manipulate. Until you have drawable content to push to the display list, nothing will happen when width and height are called.