Cocos2d-x-custom blood bar

Source: Internet
Author: User

Run:



Resource image:

Foreground graph of blood Strips:

Blood background image:


<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + release/uxL/W0MztvNPS1M/release + CjwvcD4KPHA + release = "brush: java;" ># 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 Merge (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 objectCCScene * scene = CCScene: create (); // 'player' is an autorelpolicobjecthelloworld * layer = HelloWorld: create (); // add layer as a child to scenescene-> addChild (layer ); // return the scenereturn scene;} // on "init" you need to initialize your instancebool HelloWorld: init (){/// /// // 1. super init firstif (! CCLayer: init () {return false;} CCSize visibleSize = CCDirector: sharedDirector ()-> getVisibleSize (); CCPoint origin = CCDirector: shareddire () -> 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 autorelpolicobjectccmenuitemimage * 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 objectCCMenu * 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 labelCCLabelTTF * pLabel = CCLabelTTF: create ("Hello World", "Arial", TITLE_FONT_SIZE ); // position the label on the center of the screenpLabel-> setPosition (ccp (origin. x + visibleSize. width/2, origin. y + visibleSize. height-pLabel-> getContentSize (). height); // add the label as a child to this layerthis-> addChild (pLabel, 1); // add "HelloWorld" splash screen "CCSprite * pSprite = CCSprite :: create ("HelloWorld.png"); // position the sprite on the center of the screenpSprite-> setPosition (ccp (visibleSize. width/2 + origin. x, visibleSize. height/2 + origin. y); // add the sprite as a child to this layerthis-> addChild (pSprite, 0); // initialize ProgressView m_pProgressView = new ProgressView; m_pProgressView-> setPosition (ccp (100,100); m_pProgressView-> setScale (2.2f); m_pProgressView-> setBackgroundTexture ("background.png"); m_pProgressView-> progress ("progress "); m_pProgressView-> setTotalProgress (1200000f); m_pProgressView-> setCurrentProgress (22.0f); this-> addChild (Progress, 2); return true;} void HelloWorld: complete (CCObject * pSender) {float progress = m_pProgressView-> getCurrentProgress () + 12.0f; m_pProgressView-> setCurrentProgress (progress); CCLOG ("% f", m_pProgressView-> getTotalProgress ()); CCLOG ("% f", m_pProgressView-> getCurrentProgress ());}





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.