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

  • 相关阅读:
    HDU 5744
    HDU 5815
    POJ 1269
    HDU 5742
    HDU 4609
    fzu 1150 Farmer Bill's Problem
    fzu 1002 HangOver
    fzu 1001 Duplicate Pair
    fzu 1150 Farmer Bill's Problem
    fzu 1182 Argus 优先队列
  • 原文地址:https://www.cnblogs.com/AmitX-moten/p/4199769.html
Copyright © 2020-2023  润新知