cocos2dx源码:相框PhotoFrame

前端之家收集整理的这篇文章主要介绍了cocos2dx源码:相框PhotoFrame前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

文件PhotoFrame.h

  1. #ifndef _PHOTOFRAME_H_
  2. #define _PHOTOFRAME_H_
  3.  
  4. #include "cocos2d.h"
  5. #include "GmbsCocos.h"
  6. #include "extensions/cocos-ext.h"
  7.  
  8. using namespace cocos2d::ui;
  9.  
  10. NS_CC_BEGIN
  11.  
  12. class PhotoFrame : public LayerColor
  13. {
  14. public:
  15. ~PhotoFrame();
  16.  
  17. static PhotoFrame* create(const char* filename,const Color4B& color = Color4B(255,255,0));
  18. virtual bool init(const char* filename);
  19.  
  20. void decorate(Node* photo,float horiz = 0,float vert = 0);
  21.  
  22. void inflate(float wInf,float hInf);
  23.  
  24. protected:
  25. ui::Scale9Sprite* m_frameSprite;
  26.  
  27. };
  28.  
  29. NS_CC_END
  30.  
  31. #endif

cpp文件PhotoFrame.cpp

  1. #include "PhotoFrame.h"
  2.  
  3. NS_CC_BEGIN
  4.  
  5. PhotoFrame::~PhotoFrame()
  6. {
  7.  
  8. }
  9.  
  10. PhotoFrame* PhotoFrame::create(const char* filename,const Color4B& color)
  11. {
  12. PhotoFrame* pobLayer = new PhotoFrame();
  13. if (pobLayer && pobLayer->initWithColor(color) && pobLayer->init(filename))
  14. {
  15. pobLayer->autorelease();
  16. return pobLayer;
  17. }
  18. else
  19. {
  20. CC_SAFE_DELETE(pobLayer);
  21. return NULL;
  22. }
  23. }
  24.  
  25. bool PhotoFrame::init(const char* filename)
  26. {
  27. m_frameSprite = ui::Scale9Sprite::create(filename);
  28. this->addChild(m_frameSprite);
  29.  
  30. return true;
  31. }
  32.  
  33. void PhotoFrame::decorate(Node* photo,float wInf,float hInf)
  34. {
  35. photo->addChild(this);
  36.  
  37. Size size = photo->getContentSize();
  38. size.width += wInf;
  39. size.height += hInf;
  40. this->setContentSize(size);
  41. m_frameSprite->setContentSize(size);
  42.  
  43. GmbsPoint pt;
  44. pt.reset(m_frameSprite);
  45. pt.xMiddleAlign(this).yMiddleAlign(this);
  46. m_frameSprite->setPosition(pt);
  47.  
  48. pt.reset(this);
  49. pt.xMiddleAlign(photo).yMiddleAlign(photo);
  50. this->setPosition(pt);
  51. }
  52.  
  53. void PhotoFrame::inflate(float wInf,float hInf)
  54. {
  55. Size size = this->getContentSize();
  56. size.width += wInf;
  57. size.height += hInf;
  58. this->setContentSize(size);
  59. m_frameSprite->setContentSize(size);
  60.  
  61. GmbsPoint pt;
  62. Node* photo = getParent();
  63. pt.reset(m_frameSprite);
  64. pt.xMiddleAlign(photo).yMiddleAlign(photo);
  65. m_frameSprite->setPosition(pt);
  66.  
  67. pt.reset(this);
  68. pt.xMiddleAlign(photo).yMiddleAlign(photo);
  69. this->setPosition(pt);
  70. }
  71.  
  72. NS_CC_END

猜你在找的Cocos2d-x相关文章