6.cocos2d设置定时器

  • T1LayerAnchorPoint.h
     1 #pragma once
     2 #include "cocos2d.h"
     3 USING_NS_CC;
     4 
     5 class T1LayerAnchorPoint:public CCLayer
     6 {
     7 public:
     8     //create->init
     9     static T1LayerAnchorPoint*create();
    10     bool init();
    11     static CCScene *scene();
    12 
    13     //画线
    14     virtual void draw(cocos2d::Renderer *renderer, const cocos2d::Mat4& transform, uint32_t flags);
    15     //定时器函数
    16     void mySchedule(float dt);
    17 
    18     CCSprite *m_spr;
    19 };

     

  • T1LayerAnchorPoint.cpp
     1 #include "T1LayerAnchorPoint.h"
     2 
     3 //创建层
     4 T1LayerAnchorPoint*T1LayerAnchorPoint::create()
     5 {
     6     T1LayerAnchorPoint *pRet = new T1LayerAnchorPoint();
     7     if (pRet && pRet->init())
     8     {
     9         pRet->autorelease();
    10     }
    11     else
    12     {
    13         delete pRet;
    14         pRet = NULL;
    15     }
    16     return pRet;
    17 
    18 }
    19 
    20 //初始化层
    21 bool T1LayerAnchorPoint::init()
    22 {
    23     CCLayer::init();
    24 
    25     CCSize winSize = CCDirector::sharedDirector()->getWinSize();
    26     m_spr = CCSprite::create("anchor1.png");
    27     //设置锚点
    28     m_spr->setAnchorPoint(ccp(0.5, 0.5));
    29     //设置放大大小
    30     m_spr->setScale(5.0f);
    31     m_spr->setPosition(ccp(winSize.width / 2, winSize.height / 2));
    32     addChild(m_spr);
    33     //设置定时器,每隔0.1秒调用一次mySchedule函数 TILayerAnchorPoint是一个宏定义,见下
    34     schedule(schedule_se  lector(T1LayerAnchorPoint::mySchedule), 0.1);
    35 
    36     return true;
    37 }
    38 
    39 CCScene *T1LayerAnchorPoint::scene()
    40 {
    41     CCScene *scene = CCScene::create();
    42     T1LayerAnchorPoint *layer = T1LayerAnchorPoint::create();
    43     scene->addChild(layer);
    44     return scene;
    45 }
    46 
    47 void T1LayerAnchorPoint::draw(cocos2d::Renderer *renderer, const cocos2d::Mat4& transform, uint32_t flags)
    48 {
    49     CCSize winSize = CCDirector::sharedDirector()->getWinSize();
    50     ccDrawColor4B(255, 0, 0, 255);
    51 
    52     ccDrawLine(ccp(0, winSize.height / 2), ccp(winSize.width, winSize.height / 2));
    53     ccDrawLine(ccp(winSize.width / 2, 0), ccp(winSize.width / 2, winSize.height));
    54 }
    55 
    56 void T1LayerAnchorPoint::mySchedule(float dt)
    57 {
    58     static float ro = 0;
    59     ro += 1;//每隔0.1秒度数加1
    60     m_spr->setRotation(ro);
    61 }

     

详解schedule_selector宏定义

 

转载于:https://www.cnblogs.com/xiaochi/p/8343030.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值