Cocos2d-x学习笔记(五)调度

在init方法中增加下边的代码,建议使用schedule函数,而不是scheduleUpdate函数,因为,后者默认是调用update函数,在如果有多个函数需要调度时,不是很灵活。

 auto label = LabelTTF::create("Hello World", "Arial", 24);
    label->setTag(123);
    
    // position the label on the center of the screen
    label->setPosition(Vec2(origin.x + visibleSize.width/2,
                            origin.y + visibleSize.height - label->getContentSize().height));

    label->setAnchorPoint(Vec2(1.0, 1.0));
    // add the label as a child to this layer
    this->addChild(label, 1);

    // self-defined code
    auto sprite = Sprite::create("HelloWorld.png");
    sprite->setPosition(Vec2(visibleSize.width/2 + origin.x,
        visibleSize.height/2 + origin.y));
    this->addChild(sprite, 0);

    //this->scheduleUpdate();
    this->schedule(schedule_selector(HelloWorld::update), 1.0f/60);

新加update方法,定时改变label的位置:

void HelloWorld::update(float dt)
{
    auto label = this->getChildByTag(123);
    label->setPosition(label->getPosition() + Vec2(2, -2));
}

在menuCloseCallback回调函数中增加以下代码,在关闭菜单的时候停止调度:

//unscheduleUpdate();
    unschedule(schedule_selector(HelloWorld::update));
    Director::getInstance()->end();

 运行结果:

 

图1 运行结果

posted @ 2015-01-03 18:16  motein  阅读(219)  评论(0编辑  收藏  举报