2014. 11. 25. 17:17
[D_011] 애니메이션/스크롤/커스텀 파티클 예제 DATA_OLD/Cocos2d-x 기능성 게임 개발과정2014. 11. 25. 17:17
해상도 640*480
HelloWorldScene.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | #ifndef __HELLOWORLD_SCENE_H__ #define __HELLOWORLD_SCENE_H__ #include "cocos2d.h" #define TAG_SPRITE_PLAYER 1 #define TAG_SPRITE_ENEMY 2 USING_NS_CC; class HelloWorld : public cocos2d::Layer { public: static cocos2d::Scene* createScene(); bool isJump; EventListenerTouchOneByOne* listener; virtual bool init(); void initBG(); //배경 초기화 void initPlayer(); //주인공 초기화 void initEnemy(float delta); //적 초기화 void setJump(bool flag); void update(float delta); bool onTouchBegan(Touch* touch, Event* unused_event); CREATE_FUNC(HelloWorld); }; #endif // __HELLOWORLD_SCENE_H__ |
HelloWorldScene.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 | #include "HelloWorldScene.h" USING_NS_CC; Scene* HelloWorld::createScene() { // 'scene' is an autorelease object auto scene = Scene::create(); auto layer = HelloWorld::create(); scene->addChild(layer); return scene; } bool HelloWorld::init() { if ( !Layer::init() ) { return false; } listener = EventListenerTouchOneByOne::create(); listener->onTouchBegan = CC_CALLBACK_2(HelloWorld::onTouchBegan, this); Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener, this); initBG(); initPlayer(); this->scheduleUpdate(); this->schedule(schedule_selector(HelloWorld::initEnemy), 3); return true; } void HelloWorld::initBG() { auto node = ParallaxNode::create(); this->addChild(node); node->runAction(RepeatForever::create(Sequence::create( MoveBy::create(20.0f, Vec2(-1280, 0)), Place::create(Vec2::ZERO), NULL))); //하늘 - 정지 auto bg0_1 = Sprite::create("BG/bg_0.png"); bg0_1->setAnchorPoint(Vec2::ZERO); node->addChild(bg0_1, 0, Vec2(0, 0), Vec2::ZERO); //산 - 천천히 for (int i = 0; i < 2; i++) { auto bg2 = Sprite::create("BG/bg_1.png"); bg2->setAnchorPoint(Vec2::ZERO); node->addChild(bg2, 0, Vec2(1.0f, 0), Vec2(1280 * i, 0)); } //땅 - 평범히 for (int i = 0; i < 3; i++) { auto bg2 = Sprite::create("BG/bg_3.png"); bg2->setAnchorPoint(Vec2::ZERO); node->addChild(bg2, 0, Vec2(2, 0), Vec2(1280*i, 0)); } //강 for (int i = 0; i < 3; i++) { auto bg3 = Sprite::create("BG/bg_2.png"); bg3->setAnchorPoint(Vec2::ZERO); node->addChild(bg3, 0, Vec2(2.5, 0), Vec2(1280*i, 0)); } } void HelloWorld::initPlayer() { CCLOG("initHero()"); isJump = false; SpriteFrameCache::getInstance()->addSpriteFramesWithFile("Player/player.plist"); //캐시에 plist 등록 auto player = Sprite::createWithSpriteFrameName("walk01.png"); player->setTag(TAG_SPRITE_PLAYER); player->setAnchorPoint(Vec2(0.5, 0)); player->setPosition(Vec2(100, 140)); this->addChild(player); auto walking = Animation::create(); walking->setDelayPerUnit(0.05f); for (int i = 0; i < 10; i++) { walking->addSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName(StringUtils::format("walk%02d.png", i+1))); } player->runAction( RepeatForever::create( Animate::create(walking)) ); } void HelloWorld::initEnemy(float delta) { CCLOG("initEnemy()"); auto enemy = Sprite::create("flyFly1.png"); enemy->setTag(TAG_SPRITE_ENEMY); enemy->setAnchorPoint(Vec2(0.5, 0)); enemy->setPosition(Vec2(680, 140)); this->addChild(enemy); auto ani = Animation::create(); ani->setDelayPerUnit(0.1f); ani->addSpriteFrameWithFile("flyFly1.png"); ani->addSpriteFrameWithFile("flyFly2.png"); auto walking = Animate::create(ani); enemy->runAction(RepeatForever::create(walking)); enemy->runAction(Sequence::create( MoveBy::create(2, Vec2(-700, 0)), RemoveSelf::create(), NULL)); } void HelloWorld::update(float delta) { CCLOG("update()"); auto player = (Sprite*)this->getChildByTag(TAG_SPRITE_PLAYER); auto enemy = (Sprite*)this->getChildByTag(TAG_SPRITE_ENEMY); if (enemy) { if (enemy->getBoundingBox().intersectsRect(player->getBoundingBox())) { CCLOG("paused"); Director::getInstance()->pause(); enemy->removeFromParent(); } } } bool HelloWorld::onTouchBegan(Touch* touch, Event* unused_event) { auto player = (Sprite*)this->getChildByTag(TAG_SPRITE_PLAYER); auto enemy = (Sprite*)this->getChildByTag(TAG_SPRITE_ENEMY); auto pos = touch->getLocation(); if (enemy && enemy->getBoundingBox().containsPoint(pos)) { //커스텀 파티클 예제 auto particle = ParticleSystemQuad::create("ParticleNoteBomb.plist"); particle->setPosition(enemy->getPosition()); particle->setScale(0.5f); this->addChild(particle); enemy->removeFromParent(); } else if ( !isJump ) { setJump(true); player->runAction( Sequence::create( JumpBy::create(0.5f, Vec2(0, 0), 100, 1), CallFunc::create(CC_CALLBACK_0(HelloWorld::setJump, this, false)), NULL)); } else if (Director::getInstance()->isPaused()) { Director::getInstance()->resume(); } return true; } void HelloWorld::setJump(bool flag) { isJump = flag; } |
소스 파일
+ 파티클 효과는 눈에는 사라진 것처럼 보이지만 보이지않을 뿐, 메모리는 가지고 있다.
그렇기 때문에 지속적으로 생기는 효과들은 효과 종료 후 메모리 해제(remove)를 해주어야 한다.
'DATA_OLD > Cocos2d-x 기능성 게임 개발과정' 카테고리의 다른 글
[D_012] Vector를 이용한 데이터 관리 예제 (0) | 2014.11.26 |
---|---|
[D_012] Sound Effect (0) | 2014.11.26 |
[D_011] 배경 스크롤 - Layer (0) | 2014.11.25 |
[D_010] Schedule (0) | 2014.11.24 |
[D_010] 애니메이션 - Sprite packing, Animation, stopAction (0) | 2014.11.24 |