views:

657

answers:

3

Here's the Class:

package fnc {
    import mx.containers.Canvas;

    public class Deck extends Canvas {

        protected var _chipCount:int;

        public function Deck(chipCount:int) {
            /* Associate some chips with this deck */
            _chipCount = chipCount;
        }

        public function get chipCount():int {
            return _chipCount;
        }
    }
}

Here's the MXML:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" xmlns:ns1="fnc.*">
    <ns1:Deck horizontalCenter="0" verticalCenter="0">
    </ns1:Deck>
</mx:Application>

Running this application gets this error:

ArgumentError: Error #1063: Argument count mismatch on fnc::Deck(). Expected 1, got 0. at mx.core::Container/createComponentFromDescriptor()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\core\Container.as:3579] at mx.core::Container/createComponentsFromDescriptors()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\core\Container.as:3493] at mx.core::Container/createChildren()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\core\Container.as:2589] at mx.core::UIComponent/initialize()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\core\UIComponent.as:5370] at mx.core::Container/initialize()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\core\Container.as:2526] at mx.core::Application/initialize()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\core\Application.as:846] at Practice/initialize()[C:\Documents and Settings\LocalService\My Documents\Flex Builder 3\Practice\src\Practice.mxml:0] at mx.managers::SystemManager/http://www.adobe.com/2006/flex/mx/internal::childAdded()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\managers\SystemManager.as:2009] at mx.managers::SystemManager/initializeTopLevelWindow()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\managers\SystemManager.as:3234] at mx.managers::SystemManager/http://www.adobe.com/2006/flex/mx/internal::docFrameHandler()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\managers\SystemManager.as:3064] at mx.managers::SystemManager/docFrameListener()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\managers\SystemManager.as:2916]

Adding chipCount="0" to the MXML like this:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" xmlns:ns1="fnc.*">
    <ns1:Deck chipCount="0" horizontalCenter="0" verticalCenter="0">
    </ns1:Deck>
</mx:Application>

Gets this compile error:

Severity and Description Path Resource Location Creation Time Id Property 'chipCount' is read-only. Practice/src Practice.mxml line 3 1242656555328 26

How do I specify the initial chip count?

+4  A: 

You're not able to pass parameters to the constructor of an element when you declare it in MXML. You'll need an empty contructor and then have a property called ChipCount. Your code will also have to be updated to handle ChipCount not being set (or set to 0).

My recommendation would be to change Deck to something like this:

public class Deck extends Canvas {

    protected var _chipCount:int;

    public function Deck() {
        _chipCount = 0; // Default ChipCount and wait for it to be set.
    }

    public function get chipCount():int {
        return _chipCount;
    }

    public function set chipCount(value:int):int {
        // Add logic here to validate ChipCount before setting.
        _chipCount = value;

    }
}
Justin Niessner
gah, you beat me to it!
quoo
You can take this a step further to use the UIComponent invalidation scheme too. Add a "chipCountChanged" property, set it to true in the setter for chipCount, then invoke invalidateProperties(). Then override commitProperties() if chipCountChanged == true, process the change at that time. That's how most of the built-in UIComponents work.
cliff.meyers
I wish I understood what you mean - but I can't without the complete example code.
Dave Babbitt
Dave: Changing the constructor as I mentioned would allow you to declare the element in your MXML using the exact code in the first half of your post to default the chipCount value to 0. You would also then be able to set the chipCount property to values other than zero in MXML rather than having to call a setChipCount() method.
Justin Niessner
+1  A: 

I believe that if you're extending a UIComponent you cannot pass arguments to the constructor - you'd have to find another way of setting chip count. I'd suggest listening for the initialize event, and set it then:

<?xml version="1.0" encoding="utf-8"?>
<mx:Script>
   public function setChipCount():void{
     myDeck.chipCount = 0;
   }
</mx:Script>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" xmlns:ns1="fnc.*">
    <ns1:Deck id="myDeck" initalize="setChipCount()" horizontalCenter="0" verticalCenter="0">
    </ns1:Deck>
</mx:Application>
quoo
+2  A: 

In answer to brd6644 comment :

package
{
    import mx.containers.Canvas;

    public class Deck extends Canvas
    {
     protected var _chipCount:int;
     private var chipCountChanged:Boolean;

     public function Deck()
     {
      super();
     }

     public function set chipCount(value:int):void
     {
      if (chipCount != value)
      {
       _chipCount = value;
       chipCountChanged = true;
       invalidateProperties();
       //call invalidateSize() if changing chipCount value may change the size of your component
       //call invalidateDisplayList() if changing chipCount value need a redraw of your component
      }
     }

     public function get chipCount():int
     {
      return _chipCount;
     }

     override protected function commitProperties():void
     {
      super.commitProperties();

      if (chipCountChanged)
      {
       chipCountChanged = false;
       //here update properties that change because of chipCount new value.
      }
     }

    }
}
PeZ