加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Cocos2d-x_定时器(更新函数)

发布时间:2020-12-14 19:09:25 所属栏目:百科 来源:网络整理
导读://// HelloWorldScene.h//#ifndef __HELLOWORLD_SCENE_H__#define __HELLOWORLD_SCENE_H__#include "cocos2d.h"#include "cocos-ext.h"USING_NS_CC;USING_NS_CC_EXT;class HelloWorld : public cocos2d::CCLayer{public: virtual bool init(); static cocos2
//
// HelloWorldScene.h
//

#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__

#include "cocos2d.h"
#include "cocos-ext.h"
USING_NS_CC;
USING_NS_CC_EXT;

class HelloWorld : public cocos2d::CCLayer
{
public:
    virtual bool init();
    static cocos2d::CCScene* scene();

    CREATE_FUNC(HelloWorld);

    virtual void update(float dt);
    virtual void myUpdate(float dt);
    virtual void moreUpdate(float dt);
    
};

#endif

//
// HelloWorldScene.cpp
//

#include "HelloWorldScene.h"

USING_NS_CC;

CCScene* HelloWorld::scene()
{
    CCScene *scene = CCScene::create();
    HelloWorld *layer = HelloWorld::create();
    scene->addChild(layer);

    return scene;
}

bool HelloWorld::init()
{
    if ( !CCLayer::init() )
    {
        return false;
    }

    CCSize winSize = CCDirector::sharedDirector()->getWinSize();
    
    CCSprite *spr = CCSprite::create("Icon-57.png");
    spr->setPosition(ccp(winSize.width*0.5,winSize.height*0.5));
    this->addChild(spr,290);
    
    this->scheduleUpdate();
    this->schedule(schedule_selector(HelloWorld::myUpdate));
    this->schedule(schedule_selector(HelloWorld::moreUpdate),2.0f);
    
    return true;
}

void HelloWorld::update(float dt)
{
    CCLog("HelloWorld::update");

    CCSprite *spr = (CCSprite *)this->getChildByTag(290);

    if (spr->getPositionX() < 260)
    {
        spr->setPosition(ccpAdd(spr->getPosition(),ccp(1,1)));
    }
    else
    {
        spr->setPosition(ccpAdd(spr->getPosition(),-1)));
    }
}

void HelloWorld::myUpdate(float dt)
{
    CCLog("HelloWorld::myUpdate");
}

void HelloWorld::moreUpdate(float dt)
{
    CCLog("HelloWorld::moreUpdate");
    
    this->unscheduleUpdate();  // 停止默认的update更新函数
    this->unschedule(schedule_selector(HelloWorld::myUpdate));  // 停止自定义的更新函数
    this->unscheduleAllSelectors();  // 停止所有的更新函数
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读