Search code examples
cocos2d-xcocosbuilder

Unexpected property: 'max_length'! after I adding custom properties to Custom class in CocosBuilder


In CocosBuilder, I have a custom class named TextInput used for user input, then I want add a max_length value to limit the length of user input.

It looks like below:

enter image description here

But when I run, I got the following error:

Cocos2d: Unexpected property: 'max_length'!

I tried to add int max_length; into TextInput.h. But nothing changed.

Here is my relative code.

TextInput.h

#ifndef __CrossKaiser__TextInput__
#define __CrossKaiser__TextInput__

#include "cocos2d.h"
#include "cocos-ext.h"
using namespace cocos2d;
using namespace cocos2d::extension;

class TextInput : public CCTextFieldTTF
{
public:
    CREATE_FUNC(TextInput);

    TextInput();
    virtual ~TextInput();

    virtual bool init();
    virtual void onEnter();
    virtual void insertText(const char * text, int len);
    virtual void deleteBackward();
    int max_length;

};
#endif 

TextInputLoader.h

#ifndef __CrossKaiser__TextInputLoader__
#define __CrossKaiser__TextInputLoader__

#include "TextInput.h"

/* Forward declaration. */
class CCBReader;
class TextInputLoader : public cocos2d::extension::CCLabelTTFLoader{
public:
    CCB_STATIC_NEW_AUTORELEASE_OBJECT_METHOD(TextInputLoader, loader);

protected:
    CCB_VIRTUAL_NEW_AUTORELEASE_CREATECCNODE_METHOD(TextInput);
};

#endif

So my question is what is the correct way to use 'Custom Properties' feature?


Solution

  • I solve this problem by first looking at the place where did the Cocos2d: Unexpected property: 'max_length'! error message come from.

    It is in CCNodeLoader.cpp:

    #define PROPERTY_TAG "tag"
    
    void CCNodeLoader::onHandlePropTypeInteger(CCNode * pNode, CCNode * pParent, CCString * pPropertyName, int pInteger, CCBReader * pCCBReader) {
        if(pPropertyName->compare(PROPERTY_TAG) == 0) {
            pNode->setTag(pInteger);
        } else {
            ASSERT_FAIL_UNEXPECTED_PROPERTY(pPropertyName);
        }
    }
    

    From this code, we can see it only handle "tag" property, and all other property will throw an assert.

    So what I thought is that I can override this method in which I can handle my own new custom property.

    So I add an override method in my TextInputLoader.cpp :

    #define PROPERTY_MAX_LENGTH "max_length"
    
    void TextInputLoader::onHandlePropTypeInteger(CCNode * pNode, CCNode * pParent, CCString * pPropertyName, int pInteger, CCBReader * pCCBReader)
    {
        if(pPropertyName->compare(PROPERTY_TAG) == 0) {
            pNode->setTag(pInteger);
        }
        else if (pPropertyName->compare(PROPERTY_MAX_LENGTH) == 0){
            ((TextInput*)pNode)->setMaxLength(pInteger);
        }
        else {
            ASSERT_FAIL_UNEXPECTED_PROPERTY(pPropertyName);
        }
    }
    

    then I add a property in my TextInput.h:

    CC_SYNTHESIZE(unsigned int, m_max_length, MaxLength);
    

    And it did work. My problem was solved.

    By the way, here I only override onHandlePropTypeInteger, which handle integer.

    If someone want other type custom property, he can override the matched method in CCNodeLoader.cpp, such as onHandlePropTypeString, onHandlePropTypeFloat and so on.