cocos2d-x 利用精灵的可见区域制作字幕滚动效果

.h

#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__

#include "cocos2d.h"

class HelloWorld : public cocos2d::CCLayer
{
public:
    virtual bool init();
    static cocos2d::CCScene* scene();
    CREATE_FUNC(HelloWorld);
    
    //更新函数
    void update(float time);
    //声明一个全局变量,用于标识字幕是否滚动完毕
    float isOver;
};

#endif // __HELLOWORLD_SCENE_H__

.cpp

#include "HelloWorldScene.h"
#include "SimpleAudioEngine.h"

using namespace cocos2d;
using namespace CocosDenshion;

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

bool HelloWorld::init()
{

    if ( !CCLayer::init() )
    {
        return false;
    }
    
    isOver = 0;
    
    //得到模拟器宽和高
    float SW = CCDirector::sharedDirector()->getWinSize().width;
    float SH = CCDirector::sharedDirector()->getWinSize().height;

    //首先创建一个精灵,设置可见区域为宽240,高320,模拟器横屏显示  资源图片:zimu.png(240*500)
    CCSprite* sp = CCSprite::create("zimu.png", CCRect(0, 0, 240, 320));
    
    //精灵水平方向居中于模拟器,精灵顶部与模拟器顶部平齐。
    sp->setPosition(ccp(SW*0.5, SH - sp->getContentSize().height*0.5));
    addChild(sp,0,99);
    
    scheduleUpdate();
    return true;
}

void HelloWorld::update(float time)
{
    CCSprite* sp = (CCSprite*)getChildByTag(99);
    //字幕滚动速度
    isOver+=0.5;
    sp->setTextureRect(CCRect(0, isOver, 240, 320));
    //循环滚动字幕
    if (isOver >= 420) {
        isOver = 0;
    }
}

举一反三,我们以上是设置 CCRectMake(x, y, width, height)中y的变化来得到字幕滚动效果,如果我们只改变width值或height值不就能得到进度条的效果了吗!!

zimu.png



cocos2d-x 利用精灵的可见区域制作字幕滚动效果_第1张图片

你可能感兴趣的:(cocos2d-x 利用精灵的可见区域制作字幕滚动效果)