ZGFrameActionSprite.cpp 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. //
  2. // ZGFrameActionSprite.cpp
  3. // Billiards
  4. //
  5. // Created by zhuge on 2018/9/6.
  6. //
  7. #include "ZGFrameActionSprite.h"
  8. #include "base/ccUTF8.h"
  9. #include "2d/CCSpriteFrameCache.h"
  10. USING_NS_CC;
  11. ZGFrameActionSprite* ZGFrameActionSprite::create() {
  12. ZGFrameActionSprite *ptr = new (std::nothrow) ZGFrameActionSprite();
  13. if(ptr != NULL && ptr->init()) {
  14. ptr->autorelease();
  15. return ptr;
  16. }
  17. CC_SAFE_DELETE(ptr);
  18. return NULL;
  19. }
  20. ZGFrameActionSprite::ZGFrameActionSprite()
  21. : _frameIndex (-1)
  22. {
  23. }
  24. ZGFrameActionSprite::~ZGFrameActionSprite(){
  25. }
  26. bool ZGFrameActionSprite::init() {
  27. Sprite::init();
  28. return true;
  29. }
  30. void ZGFrameActionSprite::setFrameNamePrefix(string prefix) {
  31. _frameNamePrefix = prefix;
  32. // updateFramePrefixAndIndex();
  33. }
  34. void ZGFrameActionSprite::setFrameIndex(int frameIndex) {
  35. _frameIndex = frameIndex;
  36. updateFramePrefixAndIndex();
  37. }
  38. void ZGFrameActionSprite::updateFramePrefixAndIndex() {
  39. auto name = StringUtils::format("%s%d.png", _frameNamePrefix.c_str(), _frameIndex);
  40. auto frame = SpriteFrameCache::getInstance()->getSpriteFrameByName(name);
  41. if (!frame) {
  42. // 没有找到,如果
  43. CCLOG("ZGFrameActionSprite: updateFramePrefixAndIndex no frame named: %s", name.c_str());
  44. } else {
  45. setSpriteFrame(name);
  46. }
  47. }