I wrote this code to test it out. The problem is that, whenever I use getContentSize( ) for my MineButton object, it always returns Zero width and height. Why does it do that?
// Mine.H
#ifndef testGame_Mine_h
#define testGame_Mine_h
#include "cocos2d.h"
class Mine : public cocos2d::CCLayer {
private:
class MineButton : public cocos2d::CCNode {
private:
cocos2d::CCSprite * m_SpriteOne;
cocos2d::CCSprite * m_SpriteTwo;
public:
virtual ~MineButton( ) { }
void initialize( );
};
public:
static cocos2d::CCScene * scene( );
virtual bool init( );
LAYER_CREATE_FUNC( Mine );
};
#endif
// Mine.CPP
#include "Mine.h"
using namespace cocos2d;
CCScene * Mine::scene() {
CCScene * scene = CCScene::create();
Mine * layer = Mine::create();
scene -> addChild( layer );
return scene;
}
bool Mine::init() {
if ( ! CCLayer::init() ) return false;
MineButton * btn = new MineButton( );
btn -> initialize();
btn -> setPosition( CCPointMake( 100, 200 ) );
this -> addChild( btn );
CCSize size = btn -> getContentSize();
CCLOG( "%d , %d", size.width, size.height );
return true;
}
void Mine::MineButton::initialize() {
m_SpriteOne = CCSprite::create( "Icon.png" );
m_SpriteOne -> setPosition( CCPointMake( this -> getPosition().x + ( m_SpriteOne -> getContentSize( ).width * 0.5 ), this -> getPosition().y + ( m_SpriteOne -> getContentSize().height * 0.5 ) ) );
this -> addChild( m_SpriteOne );
m_SpriteTwo = CCSprite::create( "Icon.png" );
m_SpriteTwo -> setPosition( CCPointMake( m_SpriteOne -> getPosition().x + ( m_SpriteTwo -> getContentSize().width ) , m_SpriteOne -> getPosition().y ) );
this -> addChild( m_SpriteTwo );
this -> autorelease();
}