Advertisement
Guest User

Untitled

a guest
May 21st, 2018
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.32 KB | None | 0 0
  1. #include "Player.h"
  2. #include "LevelOne.h"
  3.  
  4. Player::~Player()
  5. {
  6. CC_SAFE_RELEASE(idleAnimate);
  7. CC_SAFE_RELEASE(moveAnimate);
  8. }
  9.  
  10. Player * Player::create()
  11. {
  12. Player * player = new Player();
  13. if (player && player->initWithFile("idle1.png"))
  14. {
  15. player->autorelease();
  16. player->initPlayer();
  17. return player;
  18. }
  19.  
  20. CC_SAFE_DELETE(player);
  21. return NULL;
  22. }
  23.  
  24. void Player::initPlayer()
  25. {
  26. moving = false;
  27. char str[100] = { 0 };
  28.  
  29. Vector<SpriteFrame*> idleAnimFrames(6);
  30. for (int i = 1; i <= 2; i++) //Iterate for the number of images you have
  31. {
  32. sprintf(str, "idle%i.png", i);
  33. auto frame = SpriteFrame::create(str, Rect(0, 0, 26, 32)); //The size of the images in an action should be the same
  34. idleAnimFrames.pushBack(frame);
  35. }
  36.  
  37. auto idleAnimation = Animation::createWithSpriteFrames(idleAnimFrames, 0.20f);
  38. idleAnimate = Animate::create(idleAnimation);
  39. idleAnimate->retain(); //Retain to use it later
  40. this->runAction(RepeatForever::create(idleAnimate)); //This will be the starting animation
  41.  
  42. Vector<SpriteFrame*> moveAnimFrames(6);
  43. for (int i = 1; i <= 3; i++)
  44. {
  45. sprintf(str, "move%i.png", i);
  46. auto frame = SpriteFrame::create(str, Rect(0, 0, 26, 32));
  47. moveAnimFrames.pushBack(frame);
  48. }
  49.  
  50. auto moveAnimation = Animation::createWithSpriteFrames(moveAnimFrames, 0.2f);
  51. moveAnimate = Animate::create(moveAnimation);
  52. moveAnimate->retain();
  53. }
  54.  
  55. void Player::move(int directionParam)
  56. {
  57. this->stopAllActions();
  58. this->runAction(RepeatForever::create(moveAnimate));
  59.  
  60. direction = directionParam;
  61. moving = true;
  62. }
  63.  
  64. void Player::idle()
  65. {
  66. moving = false;
  67. this->stopAllActions();
  68. this->runAction(RepeatForever::create(idleAnimate));
  69. }
  70.  
  71. void Player::update()
  72. {
  73. if (moving) //check if moving
  74. {
  75. if (direction == 0) //check if going left
  76. {
  77. this->setScaleX(-1); //flip
  78. this->setPositionX(this->getPositionX()-3);
  79. }
  80. else
  81. {
  82. this->setScaleX(1); //flip
  83. this->setPositionX(this->getPositionX()+3);
  84. }
  85. }
  86. }
  87.  
  88.  
  89.  
  90.  
  91.  
  92.  
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105. #ifndef PLAYER_H_
  106. #define PLAYER_H_
  107.  
  108. #include "cocos2d.h"
  109.  
  110. USING_NS_CC;
  111.  
  112. class Player : public cocos2d::Sprite {
  113. public:
  114. Animate * idleAnimate;
  115. Animate * moveAnimate;
  116.  
  117. static Player * create(void);
  118. void move(int directionParam);
  119. void idle();
  120.  
  121. void update();
  122.  
  123. private:
  124. ~Player();
  125.  
  126. bool moving;
  127. int direction;
  128.  
  129. void initPlayer();
  130. };
  131.  
  132. #endif /* PLAYER_H_ */
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.  
  141.  
  142.  
  143.  
  144.  
  145.  
  146.  
  147.  
  148. main
  149. {
  150. ....
  151. player = Player::create();
  152. player->setAnchorPoint(Vec2(0, 0));
  153. player->setPosition(Vec2(brick->getBoundingBox().size.width+20, brick->getBoundingBox().size.height+10));
  154. auto playerbody = PhysicsBody::createBox(player->getContentSize(), PhysicsMaterial(0, 0, 0));
  155.  
  156. player->setPhysicsBody(playerbody);
  157.  
  158.  
  159. this->addChild(player, 5);
  160.  
  161. this->scheduleUpdate();
  162.  
  163. return true;
  164. }
  165.  
  166. void LevelOne::goBack(Ref* pSender)
  167. {
  168. Director::getInstance()->popScene();
  169. }
  170.  
  171. void LevelOne::update(float dt)
  172. {
  173. player->update();
  174. }
  175.  
  176. bool LevelOne::onTouchBegan(Touch *touch, Event *event)
  177. {
  178. if (touch->getLocation().x < player->getPositionX())
  179. {
  180. player->move(0); // param '0' for left
  181. }
  182. if (touch->getLocation().x > player->getPositionX())
  183. {
  184. player->move(1); // param '0' for right
  185. }
  186.  
  187. return true;
  188. }
  189. void LevelOne::onTouchEnded(Touch *touch, Event *event)
  190. {
  191. player->idle();
  192. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement