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宏定义

 

posted @ 2018-01-24 18:36  喵小喵~  阅读(202)  评论(0)    收藏  举报