首页 > 其他 > 详细

Cocos-2dx学习笔记(五)调度

时间:2015-01-03 18:35:11      阅读:173      评论:0      收藏:0      [点我收藏+]

在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 运行结果

Cocos-2dx学习笔记(五)调度

原文:http://www.cnblogs.com/AmitX-moten/p/4199769.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!