Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "Player.h"
- #include "LevelOne.h"
- Player::~Player()
- {
- CC_SAFE_RELEASE(idleAnimate);
- CC_SAFE_RELEASE(moveAnimate);
- }
- Player * Player::create()
- {
- Player * player = new Player();
- if (player && player->initWithFile("idle1.png"))
- {
- player->autorelease();
- player->initPlayer();
- return player;
- }
- CC_SAFE_DELETE(player);
- return NULL;
- }
- void Player::initPlayer()
- {
- moving = false;
- char str[100] = { 0 };
- Vector<SpriteFrame*> idleAnimFrames(6);
- for (int i = 1; i <= 2; i++) //Iterate for the number of images you have
- {
- sprintf(str, "idle%i.png", i);
- auto frame = SpriteFrame::create(str, Rect(0, 0, 26, 32)); //The size of the images in an action should be the same
- idleAnimFrames.pushBack(frame);
- }
- auto idleAnimation = Animation::createWithSpriteFrames(idleAnimFrames, 0.20f);
- idleAnimate = Animate::create(idleAnimation);
- idleAnimate->retain(); //Retain to use it later
- this->runAction(RepeatForever::create(idleAnimate)); //This will be the starting animation
- Vector<SpriteFrame*> moveAnimFrames(6);
- for (int i = 1; i <= 3; i++)
- {
- sprintf(str, "move%i.png", i);
- auto frame = SpriteFrame::create(str, Rect(0, 0, 26, 32));
- moveAnimFrames.pushBack(frame);
- }
- auto moveAnimation = Animation::createWithSpriteFrames(moveAnimFrames, 0.2f);
- moveAnimate = Animate::create(moveAnimation);
- moveAnimate->retain();
- }
- void Player::move(int directionParam)
- {
- this->stopAllActions();
- this->runAction(RepeatForever::create(moveAnimate));
- direction = directionParam;
- moving = true;
- }
- void Player::idle()
- {
- moving = false;
- this->stopAllActions();
- this->runAction(RepeatForever::create(idleAnimate));
- }
- void Player::update()
- {
- if (moving) //check if moving
- {
- if (direction == 0) //check if going left
- {
- this->setScaleX(-1); //flip
- this->setPositionX(this->getPositionX()-3);
- }
- else
- {
- this->setScaleX(1); //flip
- this->setPositionX(this->getPositionX()+3);
- }
- }
- }
- #ifndef PLAYER_H_
- #define PLAYER_H_
- #include "cocos2d.h"
- USING_NS_CC;
- class Player : public cocos2d::Sprite {
- public:
- Animate * idleAnimate;
- Animate * moveAnimate;
- static Player * create(void);
- void move(int directionParam);
- void idle();
- void update();
- private:
- ~Player();
- bool moving;
- int direction;
- void initPlayer();
- };
- #endif /* PLAYER_H_ */
- main
- {
- ....
- player = Player::create();
- player->setAnchorPoint(Vec2(0, 0));
- player->setPosition(Vec2(brick->getBoundingBox().size.width+20, brick->getBoundingBox().size.height+10));
- auto playerbody = PhysicsBody::createBox(player->getContentSize(), PhysicsMaterial(0, 0, 0));
- player->setPhysicsBody(playerbody);
- this->addChild(player, 5);
- this->scheduleUpdate();
- return true;
- }
- void LevelOne::goBack(Ref* pSender)
- {
- Director::getInstance()->popScene();
- }
- void LevelOne::update(float dt)
- {
- player->update();
- }
- bool LevelOne::onTouchBegan(Touch *touch, Event *event)
- {
- if (touch->getLocation().x < player->getPositionX())
- {
- player->move(0); // param '0' for left
- }
- if (touch->getLocation().x > player->getPositionX())
- {
- player->move(1); // param '0' for right
- }
- return true;
- }
- void LevelOne::onTouchEnded(Touch *touch, Event *event)
- {
- player->idle();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement