Cocos2d-x--自定义血条

运行效果图:

Cocos2d-x--自定义血条_第1张图片


资源图片:

血条前景图:

血条背景图:


在HelloCpp项目中添加以下代码:


Progress.h:

#ifndef __PROGRESSVIEW_H__
#define __PROGRESSVIEW_H__

#include "cocos2d.h"
using namespace cocos2d;

class ProgressView : public CCNode
{
public:
    ProgressView();

public:
    void setBackgroundTexture(const char *pName);
    void setForegroundTexture(const char *pName);
    void setTotalProgress(float total);
    void setCurrentProgress(float progress);
    float getCurrentProgress() const;
    float getTotalProgress() const;

private:
    void setForegroundTextureRect(const CCRect &rect);

private:
    CCSprite *m_progressBackground;
    CCSprite *m_progressForeground;
    float m_totalProgress;
    float m_currentProgress;
    float m_scale;
};

#endif


Progress.cpp:

#include "ProgressView.h"

void ProgressView::setBackgroundTexture( const char *pName )
{
    m_progressBackground = CCSprite::create(pName);
    this->addChild(m_progressBackground);
}

void ProgressView::setForegroundTexture( const char *pName )
{
    m_progressForeground = CCSprite::create(pName);
    m_progressForeground->setAnchorPoint(ccp(0.0f, 0.5f));
    m_progressForeground->setPosition(ccp(-m_progressForeground->getContentSize().width * 0.5f, 0));
    this->addChild(m_progressForeground);
}

void ProgressView::setTotalProgress( float total )
{
    if (m_progressForeground == NULL) {return;}
    m_scale = m_progressForeground->getContentSize().width / total;
    m_totalProgress = total;
}

void ProgressView::setCurrentProgress( float progress )
{
    if (m_progressForeground == NULL) {return;}
    if (progress < 0.0f) {progress = 0.0f;}
    if (progress > m_totalProgress) {progress = m_totalProgress;}
    m_currentProgress = progress;
    float rectWidth = progress * m_scale;
    const CCPoint from = m_progressForeground->getTextureRect().origin;
    const CCRect rect = CCRectMake(from.x, from.y, rectWidth, m_progressForeground->getContentSize().height);
    setForegroundTextureRect(rect);
}

void ProgressView::setForegroundTextureRect( const CCRect &rect )
{
    m_progressForeground->setTextureRect(rect);
}

ProgressView::ProgressView()
    : m_progressBackground(NULL)
    , m_progressForeground(NULL)
    , m_totalProgress(0.0f)
    , m_currentProgress(0.0f)
    , m_scale(1.0f)
{}

float ProgressView::getCurrentProgress() const
{
    return m_currentProgress;
}

float ProgressView::getTotalProgress() const
{
    return m_totalProgress;
}


HelloScene.h:

#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__

#include "cocos2d.h"
#include "ProgressView.h"

class HelloWorld : public cocos2d::CCLayer
{
public:
    // Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
    virtual bool init();  

    // there's no 'id' in cpp, so we recommend returning the class instance pointer
    static cocos2d::CCScene* scene();
    
    // a selector callback
    void menuCloseCallback(CCObject* pSender);
    
    // implement the "static node()" method manually
    CREATE_FUNC(HelloWorld);

private:
    ProgressView *m_pProgressView;
};

#endif // __HELLOWORLD_SCENE_H__


HelloScene.cpp:

#include "HelloWorldScene.h"
#include "AppMacros.h"
USING_NS_CC;


CCScene* HelloWorld::scene()
{
	// 'scene' is an autorelease object
	CCScene *scene = CCScene::create();

	// 'layer' is an autorelease object
	HelloWorld *layer = HelloWorld::create();

	// add layer as a child to scene
	scene->addChild(layer);
	// return the scene
	return scene;
}

// on "init" you need to initialize your instance
bool HelloWorld::init()
{
	//
	// 1. super init first
	if ( !CCLayer::init() )
	{
		return false;
	}

	CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
	CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();

	/
	// 2. add a menu item with "X" image, which is clicked to quit the program
	//    you may modify it.

	// add a "close" icon to exit the progress. it's an autorelease object
	CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
		"CloseNormal.png",
		"CloseSelected.png",
		this,
		menu_selector(HelloWorld::menuCloseCallback));

	pCloseItem->setPosition(ccp(origin.x + visibleSize.width - pCloseItem->getContentSize().width/2 ,
		origin.y + pCloseItem->getContentSize().height/2));

	// create menu, it's an autorelease object
	CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);
	pMenu->setPosition(CCPointZero);
	this->addChild(pMenu, 1);

	/
	// 3. add your codes below...

	// add a label shows "Hello World"
	// create and initialize a label

	CCLabelTTF* pLabel = CCLabelTTF::create("Hello World", "Arial", TITLE_FONT_SIZE);

	// position the label on the center of the screen
	pLabel->setPosition(ccp(origin.x + visibleSize.width/2,
		origin.y + visibleSize.height - pLabel->getContentSize().height));

	// add the label as a child to this layer
	this->addChild(pLabel, 1);

	// add "HelloWorld" splash screen"
	CCSprite* pSprite = CCSprite::create("HelloWorld.png");

	// position the sprite on the center of the screen
	pSprite->setPosition(ccp(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));

	// add the sprite as a child to this layer
	this->addChild(pSprite, 0);

    //初始化ProgressView
    m_pProgressView = new ProgressView;
    m_pProgressView->setPosition(ccp(100, 100));
    m_pProgressView->setScale(2.2f);
    m_pProgressView->setBackgroundTexture("background.png");
    m_pProgressView->setForegroundTexture("foreground.png");
    m_pProgressView->setTotalProgress(120.0f);
    m_pProgressView->setCurrentProgress(22.0f);
    this->addChild(m_pProgressView, 2);
	return true;
}


void HelloWorld::menuCloseCallback(CCObject* pSender)
{
    float progress = m_pProgressView->getCurrentProgress() + 12.0f;
    m_pProgressView->setCurrentProgress(progress);
    CCLOG("%f", m_pProgressView->getTotalProgress());
    CCLOG("%f", m_pProgressView->getCurrentProgress());
}





你可能感兴趣的:(Cocos2d-x)