HelloWorld.h文件部分内容:
class HelloWorld : public cocos2d::Layer
{
private:
LabelTTF * label; //定时去移动一个Label
public:
static cocos2d::Scene* createScene();
virtual bool init();
CREATE_FUNC(HelloWorld);
virtual void update(float dt); //重写update方法
};
HelloWorld.cpp文件部分内容:
bool HelloWorld::init()
{
//
// 1. super init first
if ( !Layer::init() )
{
return false;
}
label = LabelTTF::create("ttf", "Courier", 30);
this->addChild(label);
//启动update方法
scheduleUpdate();
return true;
}
void HelloWorld::update(float dt){ //实现update方法
if (label->getPosition().x > 500) unscheduleUpdate(); //停止update
label->setPosition(label->getPosition()+Point(1, 1)); //移动Label
}
自定义时间的计时器:
//设置计时器
schedule(schedule_selector(HelloWorld::timerHandler), 1); //每隔一秒,调用这个方法