cocos2d-x 3.0 绘制图形DrawNode与调用周期性回调函数schedule

cocos  绘制图形与调用回调函数

     time1--;

    char  str[100];

    //auto label1 = (Label* )this->getChildByTag(100);

    sprintf(str,"Time:%d",time1);     //  设置变化Label的值

    label->setString(str);

绘制图形

绘制图形的话采用的是引擎封装好的DrawNode类来实现

auto node = DrawNode::create();

    node->drawDot(Vec2(100, 100), 5, Color4F::GREEN);

    this->addChild(node);


如果需要的话可以重写父类提供的create()函数

DrawNodeTest * DrawNodeTest:: create(){

    DrawNodeTest * rec = new DrawNodeTest();

    if (rec && rec->init()) {

        rec->autorelease();

        return rec;

    }else{

        CC_SAFE_DELETE(rec);

        return NULL;

    }

}

bool DrawNodeTest:: init(){

    if (!Layer::init()) {

        return false;

    }

    node = DrawNode::create();

    node->drawLine(…) // 画一条线

调用周期回调函数

    // 使用系统默认的时间调度,这样会在每一帧调用重写的父类的update函数

    //this->scheduleUpdate();

    

    // 自定义时间调度  2.0f <=> 2秒为一个周期进行回调

    //this->schedule(CC_CALLBACK_1(DrawNodeTest::myUpdate, this), "myupdate");

    this->schedule(CC_CALLBACK_1(DrawNodeTest::myUpdate, this),2.0f,"key1");

    // this->unschedule("myupdate");  // 停止调用

    

    // 只调用一次时间调度

    this->scheduleOnce(CC_CALLBACK_1(DrawNodeTest::myUpdate, this),2.0f,"once");

    return true;

}

void DrawNodeTest:: update(float dt){

    log("update!!!");

}

void DrawNodeTest::myUpdate(float dt){

    log("my update!!");

    t--;

    if (t<90) {

       this->unschedule("myupdate");  // 停止调用名字为myupdate的函数

        this->unscheduleAllCallbacks();// 停止所有回调函数

    }

    auto label = (Label*)this->getChildByTag(100);

    char str[100];

    sprintf(str, "time%d”,t);// 融合字符串

    label->setString(str);

}

数组、字典、数据结构

__Array  __Dictionary   OC中用法基本一样

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值