zoukankan      html  css  js  c++  java
  • 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宏定义

  • 相关阅读:
    刚学的 activity 之间的两种(带数据)跳转
    SharePreference 把少量数据写到 文件中
    Android 使用资源
    SQLite 初步测试
    android 操作 sd 卡上的文件
    android 操作文件
    002android 布局(layout)
    Activity_liftCycle — 生命周期
    android 创建、删除 文件和文件夹 测试
    android 实现粗糙的文件浏览器
  • 原文地址:https://www.cnblogs.com/xiaochi/p/8343030.html
Copyright © 2011-2022 走看看