导演类无法读取

nikodevil
Mon, 26 May 2014 18:34:51 +0000

/////////////////////////////////////////头文件
#ifndef ACCELEROMETER_TEST_H
#define ACCELEROMETER_TEST_H
#include “cocos2d.h”
USING_NS_CC;

class AccelerometerTest : public CCLayer
{
protected:
CCSprite* m_pBall;
double m_fLastTime;

public:
AccelerometerTest(void);
~AccelerometerTest(void);

virtual void didAccelerate(CCAcceleration* pAccelerationValue);

virtual std::string title();
virtual void onEnter();
CREATE_FUNC(AccelerometerTest);
static CCScene * scene();

};

// class AccelerometerTestScene : public TestScene
// {
// public:
// virtual void runThisTest();
// };

#endif

///////////////////////////////////////////////源文件
#include “AccelerometerTest.h”
USING_NS_CC;

CCSize size = CCDirector::sharedDirector()->getVisibleSize();
CCPoint Originpoint = CCDirector::sharedDirector()->getVisibleOrigin();
CCPoint centerPoint = CCPoint(size.width / 2, size.height / 2);
#define FIX_POS(_pos, _min, _max)
if (_pos < _min)
_pos = _min;
else if (_pos > _max)
_pos = _max; \

//------------------------------------------------------------------
//
// AccelerometerTest
//
//------------------------------------------------------------------
AccelerometerTest::AccelerometerTest(void)
: m_fLastTime(0.0)
{
}

AccelerometerTest::~AccelerometerTest(void)
{
m_pBall->release();
}

CCScene * AccelerometerTest::scene()
{
CCScene * scene = CCScene::create();
AccelerometerTest * layer = new AccelerometerTest();
scene->addChild(layer);
layer->retain();
return scene;
}

std::string AccelerometerTest::title()
{
return “AccelerometerTest”;
}

void AccelerometerTest::onEnter()
{
CCLayer::onEnter();

setAccelerometerEnabled(true);

CCLabelTTF* label = CCLabelTTF::create(title().c_str(), "Arial", 32);
addChild(label, 1);

label->setPosition(ccp(centerPoint.x, size.height - 50));

m_pBall = CCSprite::create("ball.png");
m_pBall->setPosition(ccp(centerPoint.x, centerPoint.y));
addChild(m_pBall);

m_pBall->retain();

}

void AccelerometerTest::didAccelerate(CCAcceleration* pAccelerationValue)
{
// double fNow = pAccelerationValue->timestamp;
//
// if (m_fLastTime > 0.0)
// {
// CCPoint ptNow = convertToUI
// }
//
// m_fLastTime = fNow;
CCDirector* pDir = CCDirector::sharedDirector();

/*FIXME: Testing on the Nexus S sometimes m_pBall is NULL */
if (m_pBall == NULL) {
    return;
}

CCSize ballSize = m_pBall->getContentSize();

CCPoint ptNow = m_pBall->getPosition();
CCPoint ptTemp = pDir->convertToUI(ptNow);

ptTemp.x += pAccelerationValue->x * 9.81f;
ptTemp.y -= pAccelerationValue->y * 9.81f;

CCPoint ptNext = pDir->convertToGL(ptTemp);
FIX_POS(ptNext.x, (0 + ballSize.width / 2.0), (size.width - ballSize.width / 2.0));
FIX_POS(ptNext.y, (0 + ballSize.height / 2.0), (size.height - ballSize.height / 2.0));
m_pBall->setPosition(ptNext);

}

就是全局变量size无法得到导演类的getiVisibleSize函数的返回值。。
被这个问题困扰很久，知道的大大帮帮忙吧~~

阅读整个主题