CCLayer can't getConentSize ?

test.h

`class test : public cocos2d::CCLayer
{

public:
test();
~test();
};`

test.cpp::::::

`#include
#include “test.h”

test::test()
{
cocos2d::CCSprite * sp = cocos2d::CCSprite::create(“closeNormal.png”);
this->addChild(sp);
}

test::~test()
{

}`

HelloWorldScene.cpp::::::

@ test * t = new test();
this->addChild(t);
CCLog(“%f”, t->getContentSize().width);@

I always got 0. why? It works in CCSprite.

You need use “t~~>getContentSize.width” only after layer~~>init().
ССLayer~~>init~~ sets the values ​​for variables.
or you can use CCDirector::sharedDirector()->getWinSize().width.

It’s better to use CCDirector::sharedDirector( ) -> getWinSize( ) if you’re trying to get the window size.

Misha Philypchuk wrote:

You need use “t~~>getContentSize.width" only after layer~~>init().
ССLayer~~>init~~ sets the values ​​for variables.
or you can use CCDirector::sharedDirector()->getWinSize().width.
thank you for your reply.
but I want to get”test" actual size, not Winsize.

“CCSprite * sp” is a child of “test”, I thought the size of Test = the size of sp.

The size of CCLayer always = WinSize?

The way you’re doing is you are accessing a local variable called sp which is only accessed by the test constructor, not the whole class itself. What you want to do is to access the sp object from another class. To do this, you need to make sp a member variable.

First, you need to set sp as public.

// Test.h
#include 
#include "cocos2d.h"

class Test : public cocos2d::CCLayer {
public:
   Test( );
   ~Test( );

   // Make sp a member variable.
   cocos2d::CCSprite * sp;
}

You need to make sp a public member variable ( or make it private and have a public getSprite( ) function to return it.
On the Test constructor, create the CCSprite and addChild it to the Test Layer.

// Test.cpp
#include 
#include "Test.h"

using namespace cocos2d;

Test::Test( ) {
   sp = CCSprite::create( "closeNormal.png" );
   this -> addChild( sp );
}

Then on your HelloWorldScene.cpp, use object.member to access the CCSprite.

Test * testLayer( );
this -> addChild( testLayer );
CCLog( "%d", testLayer.sp -> getContentSize( ).width );

Lance Gray wrote:

The way you’re doing is you are accessing a local variable called sp which is only accessed by the test constructor, not the whole class itself. What you want to do is to access the sp object from another class. To do this, you need to make sp a member variable.
>
First, you need to set sp as public.
[…]
>
You need to make sp a public member variable ( or make it private and have a public getSprite( ) function to return it.
On the Test constructor, create the CCSprite and addChild it to the Test Layer.
[…]
>
Then on your HelloWorldScene.cpp, use object.member to access the CCSprite.
[…]

Thanks again.

If there are many CCSprites in TEST.cpp, and CCsprites have random position, get these CCSprite’s actual size is a little more complicated.

In Flash Action Script 3 the parent’s size = The size of all child nodes overlap or Ordering. So I think cclayer~~>getcontenSize is the same.
Will be used in what circumstances this method if cclayer~~>getcontenSize = CCDirector::sharedDirector( ) -> getWinSize( ) ?

I’m not sure as to what’s the use of getContentSize( ) does for a CCLayer. Even the docs don’t explain its use.

As this size CCLayer not (initialization CCLayer is the window size).
If you need to find an area occupied by CCSprite, this must be done manually.
Size CCLayer not change with addition of or change position CCSprite.
I think so!
Sorry for my bad English