zoukankan      html  css  js  c++  java
  • 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 运行结果

  • 相关阅读:
    Educational Codeforces Round 23E
    Educational Codeforces Round 23D
    Codeforces Round #461 (Div. 2)
    HYSBZ
    HDU
    HYSBZ
    HYSBZ
    SPOJ
    点击搜索条件提交form表单
    HTML颜色获取工具,colorpicker
  • 原文地址:https://www.cnblogs.com/AmitX-moten/p/4199769.html
Copyright © 2011-2022 走看看