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

转载于:https://www.cnblogs.com/AmitX-moten/p/4199769.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值