1. 程式人生 > >Cocos2d-x:學習筆記(2017.05.12更新)

Cocos2d-x:學習筆記(2017.05.12更新)

1.參考連結彙總

2.建立Sprite

auto bg = Sprite::create("level-background-0.jpg");
bg->setPosition(Vec2(visibleSize.width / 2 + origin.x, visibleSize.height / 2 + origin.y));
this->addChild(bg, 0);

使用plist:

    auto spritecache = SpriteFrameCache::getInstance
(); spritecache->addSpriteFramesWithFile("level-sheet.plist"); mouse = Sprite::createWithSpriteFrameName("gem-mouse-0.png"); mouse->setPosition(Vec2(origin.x + visibleSize.width / 2, 0));

使用動畫第一幀:

    // 建立一張貼圖
    auto texture = Director::getInstance()->getTextureCache()->addImage("$lucia_2.png"
); // 從貼圖中以畫素單位切割,建立關鍵幀 auto frame0 = SpriteFrame::createWithTexture(texture, CC_RECT_PIXELS_TO_POINTS(Rect(0, 0, 113, 113))); // 使用第一幀建立精靈 player = Sprite::createWithSpriteFrame(frame0); player->setPosition(Vec2(origin.x + visibleSize.width / 2, origin.y + visibleSize.
height/2)); addChild(player, 3);

3.建立MenuItem

auto closeItem = MenuItemImage::create(
                                       "CloseNormal.png",
                                       "CloseSelected.png",
                                       CC_CALLBACK_1(HelloWorld::menuCloseCallback, this));

closeItem->setPosition(Vec2(origin.x + visibleSize.width - closeItem->getContentSize().width/2 ,
                            origin.y + closeItem->getContentSize().height/2));
auto menu = Menu::create(closeItem, NULL);
menu->setPosition(Vec2::ZERO);
this->addChild(menu, 1);
void HelloWorld::menuCloseCallback(Ref* pSender)
{
   Director::getInstance()->end();

#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
    exit(0);
#endif
}

4.建立Layer

stoneLayer = Layer::create();
stoneLayer->setPosition(Vec2(0, 0));
stoneLayer->setAnchorPoint(Vec2(0, 0));
stoneLayer->ignoreAnchorPointForPosition(false);
stoneLayer->addChild(stone);


mouseLayer = Layer::create();
mouseLayer->setPosition(Vec2(0, origin.y + visibleSize.height / 2));
mouseLayer->setAnchorPoint(Vec2(0, 0));
mouseLayer->ignoreAnchorPointForPosition(false);
mouseLayer->addChild(mouse);

this->addChild(stoneLayer, 1);
this->addChild(mouseLayer, 1);

5.顯示中文字元

    CCDictionary* message = CCDictionary::createWithContentsOfFile("Chinese.xml");
    auto nameKey = message->valueForKey("Name");
    const char* Name = nameKey->getCString();
    auto IDKey = message->valueForKey("ID");
    const char* ID = IDKey->getCString();
    auto label = Label::createWithTTF(Name, "fonts/FZSTK.ttf", 24);
    auto label1 = Label::createWithTTF(ID, "fonts/Marker Felt.ttf", 24);
    // position the label on the center of the screen
    label->setPosition(Vec2(origin.x + visibleSize.width/2,
                            origin.y + visibleSize.height - label->getContentSize().height));
    label1->setPosition(Vec2(origin.x + visibleSize.width / 2,
                            origin.y + visibleSize.height
                             - label->getContentSize().height-label1->getContentSize().height));
<?xml version="1.0" encoding="UTF-8"?>

<dict>

<key>Name</key>

<string>名字</string>

<key>ID</key>

<string>學號</string>

</dict>

6. 播放背景英語

#include <SimpleAudioEngine.h>
#define MUSIC_FILE "666.mp3"

以下是menuItem的觸發函式,點選一下播放音樂,再點選一下停止音樂

void HelloWorld::display_music(Ref* pSender) {
    CocosDenshion::SimpleAudioEngine::sharedEngine()->preloadBackgroundMusic(MUSIC_FILE);
    CocosDenshion::SimpleAudioEngine::sharedEngine()->setBackgroundMusicVolume(0.5);
    if (count % 2 == 0)
      CocosDenshion::SimpleAudioEngine::sharedEngine()->playBackgroundMusic(MUSIC_FILE, true);
    else
      CocosDenshion::SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();
    count++;
}

7.定義動畫(使用plist)

在AppDelegate.cpp中,

SpriteFrameCache::getInstance()->addSpriteFramesWithFile("level-sheet.plist");
char _totalFrames = 7;
char _frameName[40];
Animation* diamondAnimation = Animation::create();

for (int i = 0; i < _totalFrames; i++) {
    sprintf(_frameName, "pulled-diamond-%d.png", i);
    diamondAnimation->addSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName(_frameName));
}
diamondAnimation->setDelayPerUnit(0.1);
AnimationCache::getInstance()->addAnimation(diamondAnimation, "diamondAnimation");

在GameSence.cpp中,

Animate* diamondAnimate = Animate::create(AnimationCache::getInstance()->getAnimation("diamondAnimation"));
diamond->runAction(RepeatForever::create(diamondAnimate));

通過texture定義動畫:

// 定義儲存動畫幀的vector
cocos2d::Vector<SpriteFrame*> attack;
// 建立一張貼圖
auto texture = Director::getInstance()->getTextureCache()->addImage("$lucia_2.png");
// 攻擊動畫
attack.reserve(17);
for (int i = 0; i < 17; i++) {
    auto frame = SpriteFrame::createWithTexture(texture, CC_RECT_PIXELS_TO_POINTS(Rect(113*i,0,113,113)));
    attack.pushBack(frame);
}
auto attack_animation = Animation::createWithSpriteFrames(attack, 0.1f);
attack_animation->setRestoreOriginalFrame(true);
AnimationCache::getInstance()->addAnimation(attack_animation, "attackAnimation");

8.觸控監聽器

EventListenerTouchOneByOne* listener = EventListenerTouchOneByOne::create();
listener->setSwallowTouches(true);
listener->onTouchBegan = CC_CALLBACK_2(GameSence::onTouchBegan, this);
Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener, this);
bool GameSence::onTouchBegan(Touch *touch, Event *unused_event) {}

9.獲得visibleSize與origin

Size visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin();

10.設定錨點

mouseLayer->setAnchorPoint(Vec2(0, 0));
mouseLayer->ignoreAnchorPointForPosition(false);

11.MoveTo使用

auto moveTo = MoveTo::create(2, mouseLayer->convertToNodeSpace(location));
mouse->runAction(moveTo);

12.世界座標系與本地座標系的轉換

auto location = mouse->getPosition();
location = mouseLayer->convertToWorldSpace(location);
auto moveTo = MoveTo::create(1, stoneLayer->convertToNodeSpace(location));
stone->runAction(moveTo);

13.延遲函式的實現(非同步操作)

auto _delayTime = DelayTime::create(0.5);
auto _func = CallFunc::create([this]() {
    stoneLayer->removeChild(stone);
    stone = Sprite::create("stone.png");
    stone->setPosition(Vec2(560, 480));
    stoneLayer->addChild(stone);
});
auto _seq = Sequence::create(_delayTime, _func, NULL);
this->runAction(_seq);

14.設定背景

// 設定背景
auto bg = Sprite::create("background.jpg");
bg->setPosition(Vec2(visibleSize.width / 2 + origin.x, visibleSize.height / 2 + origin.y));
this->addChild(bg, 0);

使用TileMap

// 設定背景
TMXTiledMap* tmx = TMXTiledMap::create("map.tmx");
tmx->setPosition(Vec2(visibleSize.width / 2 + origin.x, visibleSize.height / 2 + origin.y));
tmx->setAnchorPoint(Vec2(0.5, 0.5));
tmx->setScale(Director::getInstance()->getContentScaleFactor());
this->addChild(tmx, 0);

15.建立倒計時

// 建立倒計時
time = Label::createWithTTF("120", "fonts/arial.ttf", 36);
time->setPosition(Vec2(origin.x+visibleSize.width/2, origin.y+visibleSize.height-30));
this->addChild(time);
schedule(schedule_selector(HelloWorld::updateTime), 1);
dtime = 120;
// 事件更新函式
void HelloWorld::updateTime(float delta) {
    dtime--;
    if (dtime < 0) dtime = 0;
    char str[10];
    sprintf(str, "%d", dtime); // 將int型別轉化為字串char*型別
    time->setString(str);
}

16.遊戲的hp條

// 進度條定義
cocos2d::ProgressTimer* pT;
// hp條
Sprite* sp0 = Sprite::create("hp.png", CC_RECT_PIXELS_TO_POINTS(Rect(0, 320, 420, 47))); // 進度條框
Sprite* sp = Sprite::create("hp.png", CC_RECT_PIXELS_TO_POINTS(Rect(610, 362, 4, 16))); // 進度條

// 使用hp條設定progressBar
pT = ProgressTimer::create(sp);
pT->setScaleX(90);
pT->setAnchorPoint(Vec2(0, 0));
pT->setType(ProgressTimerType::BAR); // 定義進度條型別
pT->setBarChangeRate(Point(1, 0));
pT->setMidpoint(Point(0, 1));
pT->setPercentage(100);
pT->setPosition(Vec2(origin.x+14*pT->getContentSize().width,origin.y + visibleSize.height - 2*pT->getContentSize().height));
addChild(pT,1);
sp0->setAnchorPoint(Vec2(0, 0));
sp0->setPosition(Vec2(origin.x + pT->getContentSize().width, origin.y + visibleSize.height - sp0->getContentSize().height));
addChild(sp0,0);

17.遊戲方向鍵WSAD定義

// 方向鍵
auto buttonW = Button::create("W.png", "W.png");
auto buttonS = Button::create("S.png", "S.png");
auto buttonA = Button::create("A.png", "A.png");
auto buttonD = Button::create("D.png", "D.png");
buttonW->setPosition(Vec2(origin.x + 60, origin.y + 60));
buttonS->setPosition(Vec2(origin.x + 60, origin.y + 20));
buttonA->setPosition(Vec2(origin.x + 20, origin.y + 20));
buttonD->setPosition(Vec2(origin.x + 100, origin.y + 20));

// W鍵事件處理函式
buttonW->addTouchEventListener([&](Ref* sender, Widget::TouchEventType type) {
    switch (type) {
    case ui::Widget::TouchEventType::BEGAN:
        schedule(schedule_selector(HelloWorld::moveW), 0.3f);
        break;
    case ui::Widget::TouchEventType::ENDED:
        unschedule(schedule_selector(HelloWorld::moveW));
        break;
    }
});

// S鍵事件處理函式
buttonS->addTouchEventListener([&](Ref* sender, Widget::TouchEventType type) {
    switch (type) {
    case ui::Widget::TouchEventType::BEGAN:
        schedule(schedule_selector(HelloWorld::moveS), 0.3f);
        break;
    case ui::Widget::TouchEventType::ENDED:
        unschedule(schedule_selector(HelloWorld::moveS));
        break;
    }
});

// A鍵事件處理函式
buttonA->addTouchEventListener([&](Ref* sender, Widget::TouchEventType type) {
    switch (type) {
    case ui::Widget::TouchEventType::BEGAN:
        schedule(schedule_selector(HelloWorld::moveA), 0.3f);
        break;
    case ui::Widget::TouchEventType::ENDED:
        unschedule(schedule_selector(HelloWorld::moveA));
        break;
    }
});

// D鍵事件處理函式
buttonD->addTouchEventListener([&](Ref* sender, Widget::TouchEventType type) {
    switch (type) {
    case ui::Widget::TouchEventType::BEGAN:
        schedule(schedule_selector(HelloWorld::moveD), 0.3f);
        break;
    case ui::Widget::TouchEventType::ENDED:
        unschedule(schedule_selector(HelloWorld::moveD));
        break;
    }
});

this->addChild(buttonW);
this->addChild(buttonS);
this->addChild(buttonA);
this->addChild(buttonD);

// 方向鍵D的移動
void HelloWorld::moveD(float dt) {
//auto seq = Sequence::createWithTwoActions(Animate::create(AnimationCache::getInstance()->getAnimation("runAnimation")),
    //                                      MoveBy::create(0.15f, Vec2(10, 0)));
auto location = player->getPosition();
if (location.x + 20 >= visibleSize.width) {
    auto seq = Sequence::createWithTwoActions(MoveBy::create(0.6f, Vec2(visibleSize.width-location.x-10, 0)),
        Animate::create(AnimationCache::getInstance()->getAnimation("runAnimation")));
    player->runAction(seq);
    return;
}
auto seq = Sequence::createWithTwoActions(MoveBy::create(0.6f, Vec2(20, 0)),
    Animate::create(AnimationCache::getInstance()->getAnimation("runAnimation")));
player->runAction(seq);
}

// 方向鍵A的移動
void HelloWorld::moveA(float dt) {
//auto seq = Sequence::createWithTwoActions(Animate::create(AnimationCache::getInstance()->getAnimation("runAnimation")),
    //MoveBy::create(0.15f, Vec2(-10, 0)));
auto location = player->getPosition();
if (location.x - 20 <= 0) {
    auto seq = Sequence::createWithTwoActions(MoveBy::create(0.6f, Vec2(-location.x+10, 0)),
        Animate::create(AnimationCache::getInstance()->getAnimation("runAnimation")));
    player->runAction(seq);
    return;
}
auto seq = Sequence::createWithTwoActions(MoveBy::create(0.6f, Vec2(-20, 0)),
    Animate::create(AnimationCache::getInstance()->getAnimation("runAnimation")));
player->runAction(seq);
}

// 方向鍵W的移動
void HelloWorld::moveW(float dt) {
//auto seq = Sequence::createWithTwoActions(Animate::create(AnimationCache::getInstance()->getAnimation("runAnimation")),
    //MoveBy::create(0.15f, Vec2(0, 10)));
auto location = player->getPosition();
if (location.y + 20 >= visibleSize.height) {
    auto seq = Sequence::createWithTwoActions(MoveBy::create(0.6f, Vec2(0, visibleSize.height-location.y-10)),
        Animate::create(AnimationCache::getInstance()->getAnimation("runAnimation")));
    player->runAction(seq);
    return;
}
auto seq = Sequence::createWithTwoActions(MoveBy::create(0.6f, Vec2(0, 20)),
    Animate::create(AnimationCache::getInstance()->getAnimation("runAnimation")));
player->runAction(seq);
}

// 方向鍵S的移動
void HelloWorld::moveS(float dt) {
//auto seq = Sequence::createWithTwoActions(Animate::create(AnimationCache::getInstance()->getAnimation("runAnimation")),
    //MoveBy::create(0.15f, Vec2(0, -10)));
auto location = player->getPosition();
if (location.y - 20 <= 0) {
    auto seq = Sequence::createWithTwoActions(MoveBy::create(0.6f, Vec2(0, -location.y+10)),
        Animate::create(AnimationCache::getInstance()->getAnimation("runAnimation")));
    player->runAction(seq);
    return;
}
auto seq = Sequence::createWithTwoActions(MoveBy::create(0.6f, Vec2(0, -20)),
    Animate::create(AnimationCache::getInstance()->getAnimation("runAnimation")));
player->runAction(seq);
}

上面的按鈕實現的是點選按鈕移動,鬆開按鈕停止移動。但是這種實現可能會有bug,莫名其妙人物就不受控制了。所以我將之改為點選一下移動一下,即鬆開滑鼠按鈕後人物才會移動:

// 方向鍵D的移動

void HelloWorld::moveD(float dt) {

    //auto seq = Sequence::createWithTwoActions(Animate::create(AnimationCache::getInstance()->getAnimation("runAnimation")),

        //                                      MoveBy::create(0.15f, Vec2(10, 0)));

    player->setFlipX(false);

    auto location = player->getPosition();

    if (location.x + 20 >= visibleSize.width) {

        //auto seq = Sequence::createWithTwoActions(MoveBy::create(0.6f, Vec2(visibleSize.width-location.x-10, 0)),

            //Animate::create(AnimationCache::getInstance()->getAnimation("runAnimation")));

        //player->runAction(seq);

        player->runAction(Animate::create(AnimationCache::getInstance()->getAnimation("runAnimation")));

        player->runAction(MoveBy::create(0.6f, Vec2(visibleSize.width - location.x - 10, 0)));

        return;

    }

    //auto seq = Sequence::createWithTwoActions(MoveBy::create(0.6f, Vec2(20, 0)),

        //Animate::create(AnimationCache::getInstance()->getAnimation("runAnimation")));

    //player->runAction(seq);

    player->runAction(Animate::create(AnimationCache::getInstance()->getAnimation("runAnimation")));

    player->runAction(MoveBy::create(0.6f, Vec2(20, 0)));

}



// 方向鍵A的移動

void HelloWorld::moveA(float dt) {

    //auto seq = Sequence::createWithTwoActions(Animate::create(AnimationCache::getInstance()->getAnimation("runAnimation")),

        //MoveBy::create(0.15f, Vec2(-10, 0)));

    player->setFlipX(true);

    auto location = player->getPosition();

    if (location.x - 20 <= 0) {

        //auto seq = Sequence::createWithTwoActions(MoveBy::create(0.6f, Vec2(-location.x+10, 0)),

            //Animate::create(AnimationCache::getInstance()->getAnimation("runAnimation")));

        //player->runAction(seq);

        player->runAction(Animate::create(AnimationCache::getInstance()->getAnimation("runAnimation")));

        player->runAction(MoveBy::create(0.6f, Vec2(-location.x + 10, 0)));

        return;

    }

    //auto seq = Sequence::createWithTwoActions(MoveBy::create(0.6f, Vec2(-20, 0)),

        //Animate::create(AnimationCache::getInstance()->getAnimation("runAnimation")));

    //player->runAction(seq);

    player->runAction(Animate::create(AnimationCache::getInstance()->getAnimation("runAnimation")));

    player->runAction(MoveBy::create(0.6f, Vec2(-20, 0)));

}



// 方向鍵W的移動

void HelloWorld::moveW(float dt) {

    //auto seq = Sequence::createWithTwoActions(Animate::create(AnimationCache::getInstance()->getAnimation("runAnimation")),

        //MoveBy::create(0.15f, Vec2(0, 10)));

    auto location = player->getPosition();

    if (location.y + 20 >= visibleSize.height) {

        //auto seq = Sequence::createWithTwoActions(MoveBy::create(0.6f, Vec2(0, visibleSize.height-location.y-10)),

            //Animate::create(AnimationCache::getInstance()->getAnimation("runAnimation")));

        //player->runAction(seq);

        player->runAction(Animate::create(AnimationCache::getInstance()->getAnimation("runAnimation")));

        player->runAction(MoveBy::create(0.6f, Vec2(0, visibleSize.height - location.y - 10)));

        return;

    }

    //auto seq = Sequence::createWithTwoActions(MoveBy::create(0.6f, Vec2(0, 20)),

        //Animate::create(AnimationCache::getInstance()->getAnimation("runAnimation")));

    //player->runAction(seq);

    player->runAction(Animate::create(AnimationCache::getInstance()->getAnimation("runAnimation")));

    player->runAction(MoveBy::create(0.6f, Vec2(0, 20)));

}



// 方向鍵S的移動

void HelloWorld::moveS(float dt) {

    //auto seq = Sequence::createWithTwoActions(Animate::create(AnimationCache::getInstance()->getAnimation("runAnimation")),

        //MoveBy::create(0.15f, Vec2(0, -10)));

    auto location = player->getPosition();

    if (location.y - 20 <= 0) {

        //auto seq = Sequence::createWithTwoActions(MoveBy::create(0.6f, Vec2(0, -location.y+10)),

            //Animate::create(AnimationCache::getInstance()->getAnimation("runAnimation")));

        //player->runAction(seq);

        player->runAction(Animate::create(AnimationCache::getInstance()->getAnimation("runAnimation")));

        player->runAction(MoveBy::create(0.6f, Vec2(0, -location.y + 10)));

        return;

    }

    //auto seq = Sequence::createWithTwoActions(MoveBy::create(0.6f, Vec2(0, -20)),

        //Animate::create(AnimationCache::getInstance()->getAnimation("runAnimation")));

    //player->runAction(seq);

    player->runAction(Animate::create(AnimationCache::getInstance()->getAnimation("runAnimation")));

    player->runAction(MoveBy::create(0.6f, Vec2(0, -20)));

}

18.實現動畫Animation的回撥函式

auto callFunc = CallFunc::create([&] {isDone = true; });
auto seq = Sequence::createWithTwoActions(Animate::create(AnimationCache::getInstance()->getAnimation("attackAnimation")),
    callFunc);

19.通過互斥鎖避免動畫重疊進行

(1)首先我在類中新新增一個isDone成員,當isDone為false時,說明player正在執行動畫;當isDone為true時,說明player已經執行完動畫;

(2)將isDone初始為true,即可執行狀態;

(3)在X、Y的點選事件處理函式的開始部分(在執行動畫之前),新增一個條件判斷,判斷isDone是否為true。如果為true,則繼續執行下面的程式碼,如果為false,則return函式;

(4)然後利用Sequence的createTwoAction來實現動畫完成後的回撥函式,回撥函式的功能就是將isDone的值賦為true。

    // 技能鍵X的事件處理函式
    buttonX->addTouchEventListener([&](Ref* sender, Widget::TouchEventType type) {
        switch (type) {
        case ui::Widget::TouchEventType::BEGAN:
            break;
        case ui::Widget::TouchEventType::ENDED:
            if (isDone == false) return; // 動畫未完成,不能執行新動畫
            else isDone = false; // 開始執行動畫
            if (pT->getPercentage() == 0) return; // 進度條為0時不可再執行該動畫
            auto callFunc = CallFunc::create([&] {isDone = true; }); // 定義動畫執行完畢的回撥函式
            /*Animate* deadAnimation = Animate::create(AnimationCache::getInstance()->getAnimation("deadAnimation"));
            auto action = Sequence::create(callFunc);*/
            auto seq = Sequence::createWithTwoActions(Animate::create(AnimationCache::getInstance()->getAnimation("deadAnimation")),
                callFunc);
            player->runAction(seq);
            //player->runAction(deadAnimation);
            if (pT->getPercentage() - 40 >= 0) { // 每次X操作減少進度條40
                pT->setPercentage(pT->getPercentage() - 40);
            }
            else {
                pT->setPercentage(0);
            }
            break;
        }
    });

    // 技能鍵Y的事件處理函式
    buttonY->addTouchEventListener([&](Ref* sender, Widget::TouchEventType type) {
        switch (type) {
        case ui::Widget::TouchEventType::BEGAN:
            break;
        case ui::Widget::TouchEventType::ENDED:
            if (isDone == false) return;
            else isDone = false;
            /*Animate* attackAnimation = Animate::create(AnimationCache::getInstance()->getAnimation("attackAnimation"));
            player->runAction(attackAnimation);*/
            auto callFunc = CallFunc::create([&] {isDone = true; });
            auto seq = Sequence::createWithTwoActions(Animate::create(AnimationCache::getInstance()->getAnimation("attackAnimation")),
                callFunc);
            player->runAction(seq);
            if (pT->getPercentage() + 40 <= 100) {
                pT->setPercentage(pT->getPercentage() + 40);
            }
            else {
                pT->setPercentage(100);
            }
            break;
        }
    });
    this->addChild(buttonX);
    this->addChild(buttonY);
    return true;
}

20.自定義排程器

switch (type) {
case ui::Widget::TouchEventType::BEGAN:
    schedule(schedule_selector(HelloWorld::moveS), 0.3f);
    break;
case ui::Widget::TouchEventType::ENDED:
    unschedule(schedule_selector(HelloWorld::moveS));
    break;

21.Lambada函式

buttonA->addTouchEventListener([&](Ref* sender, Widget::TouchEventType type) {
        switch (type) {
        case ui::Widget::TouchEventType::BEGAN:
            schedule(schedule_selector(HelloWorld::moveA), 0.3f);
            break;
        case ui::Widget::TouchEventType::ENDED:
            unschedule(schedule_selector(HelloWorld::moveA));
            break;
        }
    });

22. 單例模式示例

class Factory:public cocos2d::Ref {
public:
    //獲取單例工廠
    static Factory* getInstance();
    //生成一個怪物,並存儲到容器中管理
    Sprite* createMonster();
    //讓容器中的所有怪物都往角色移動,通過容器管理所有的怪物很方便
    void moveMonster(Vec2 playerPos,float time);
    //移除怪物
    void removeMonster(Sprite*);
    //判斷碰撞
    Sprite* collider(Rect rect);
    //初始化怪物幀動畫
    void initSpriteFrame();
private:
    Factory();
    Vector<Sprite*> monster;
    cocos2d::Vector<SpriteFrame*> monsterDead;
    static Factory* factory;
};
// Monster.cpp
Factory* Factory::factory = NULL;
Factory::Factory() {
    initSpriteFrame();
}
// 獲取單例
Factory* Factory::getInstance() {
    if (factory == NULL) {
        factory = new Factory();
    }
    return factory;
}

// 生成monster死亡的動畫幀並存入到vector陣列中
void Factory::initSpriteFrame() {
    auto texture = Director::getInstance()->getTextureCache()->addImage("Monster.png");
    monsterDead.reserve(4);
    for (int i = 0; i < 4; i++) {
        auto frame = SpriteFrame::createWithTexture(texture, CC_RECT_PIXELS_TO_POINTS(Rect(
            
           

相關推薦

Cocos2d-x學習筆記2017.05.12更新

1.參考連結彙總 2.建立Sprite auto bg = Sprite::create("level-background-0.jpg"); bg-&g

5.27cocos2d-x初探學習筆記2--重要概念及Test樣例結構(轉)

這樣的 發生 菜單 add css 基礎 dsm 人的 添加 1.幾個重要概念 在cocos2d引擎中,有幾個概念,各自是導演。場景,布景和人物角色。 導演(CCDirector):在cocos2d-x引擎中,導演類是遊戲的組織者和領導者。導演制定規則讓遊戲內的場

Vue學習筆記-自定義指令

提醒 原帖完整收藏於IT老兵驛站,並會不斷更新。 前言 前面總結到了元件,對混入也進行了研究,不過感覺沒有啥需要總結的,就先總結指令吧,參考這裡,記錄筆記。 正文 簡介 全域性註冊 // 註冊一個全域性自定義指令 `v-focus` Vue.di

Vue學習筆記-元件基礎

提醒 原帖完整收藏於IT老兵驛站,並會不斷更新。 前言 最近在工作中頻繁遇到元件,所以就沒按照順序,先總結元件這一章節的內容了,將來再做調整和修改。 (2018-10-24注:這一章粗讀了兩遍,感覺下面有好些內容都沒有理解,有一些難度,看不明白就先修改一會兒,先乾點別的。

Vue學習筆記-Class 與 Style 繫結

提醒 原帖完整收藏於IT老兵驛站,並會不斷更新。 前言 本篇繼續對Vue的【Class 與 Style 繫結】篇進行總結學習。 正文 操作元素的 class 列表和內聯樣式是資料繫結的一個常見需求。因為它們都是屬性,所以我們可以用 v-bind 處理它們:只需

Vue學習筆記-計算屬性和偵聽器

提醒 原帖完整收藏於IT老兵驛站,並會不斷更新。 前言 參考官網的這裡和中文版,總體學習一下計算屬性,感覺這一章節總體是比較簡單的,做一下筆記來進行總結。思路是,原文寫的很清楚的,只做簡單的概括;對原文存在疑問的地方,摘抄原文,列舉問題,總結概括。 正文 計算屬性

Vue學習筆記-模板語法

提醒 原帖完整收藏於IT老兵驛站,並會不斷更新。 前言 忙了三週,又度過一個豐富的十一,現在騰出手來,繼續我的學習和總結。最近找到了Vue的中文網站,但是我不想放棄對英文網站的學習,那樣可以更準確地理解原意,可以提高自己的英文水平,所以基於英文網站,對照著中文,這樣來學習--

Vue學習筆記-模板語法1

提醒 原帖完整收藏於IT老兵驛站,並會不斷更新。 前言 忙了三週,又度過一個豐富的十一,現在騰出手來,繼續我的學習和總結。最近找到了Vue的中文網站,但是我不想放棄對英文網站的學習,那樣可以更準確地理解原意,可以提高自己的英文水平,所以基於英文網站,對照著中文,

Spring-data-jpa學習筆記

       通過上一篇筆記的,我們掌握了SpringData的相關概念及簡單的用法。但上一篇筆記主要講的是Dao層介面直接繼承Repository介面,然後再自己定義方法。主要闡述了自定義方法時的一些規則及SpringData是如何

Spring-data-jpa學習筆記

一、spring-data-jpa的簡單介紹 SpringData : Spring 的一個子專案。用於簡化資料庫訪問,支援NoSQL 和 關係資料儲存。其主要目標是使資料庫的訪問變得方便快捷。 SpringData 專案所支援 NoSQL 儲存:  MongoD

計算機視覺系列學習筆記

一. 影象基礎:畫素 畫素是影象最基礎的構成要素,每一張影象都是由畫素集合組成。 如果我們將影象當作一個網格,則每一小塊是由單個畫素組成,如下圖: 上圖的解析度為1000 * 750,意味著有1000畫素寬,750畫素高。可以將一張影象

【cs231n學習筆記2017】——— KNN

好久沒寫部落格了,最近有點忙,然後自己也有點懶。。。。。。 最近確定自己要搞計算機視覺方向,然後就開始看斯坦福的cs231n課程,準備寫系列部落格記錄自己的學習過程及心得。 ———————————–

RabbitMQ基礎學習筆記C#代碼示例

esp 輸出 出隊 csharp 實例代碼 為什麽 mode 規則 無需 一、定義: MQ是MessageQueue,消息隊列的簡稱(是流行的開源消息隊列系統,利用erlang語言開發)。MQ是一種應用程序對應用程序的通信方法。應用程序通過讀寫入隊和出隊的消息來通信

Java程序猿的JavaScript學習筆記9—— jQuery工具方法

article 順序 還要 並且 defined this ont property plain 計劃按例如以下順序完畢這篇筆記: Java程序猿的JavaScript學習筆記(1——理念) Java程序猿的JavaScript學習筆記(2——屬性

shell腳本學習筆記 正則表達式

時也 限定符 數量 介紹 是我 center sof 好的 fgrep 正則表達式一般有三個部分組成,他們各自是:字符類,數量限定符,位置限定符。規定一些特殊語法表示字符類、數 量限定符和位置關系,然後用這些特殊語法和普通字符一起表示一個模式,這

學習筆記10月17日--pycharm安裝

學習筆記一周二次課(10月17日)1.安裝pycharm官網https://www.jetbrains.com/pycharm/download下載軟件包,選擇Community免費版本,安裝比較簡單,就一直下一步下一步直到安裝完成。2.學會設置pycharm3.學會在pycharm中運行python程序學習

學習筆記10月18日 --pycharm基本使用方法

學習筆記一周三次課(10月18日)1. 學會通過pycharm給python程序傳遞參數設置python的傳遞參數:【Run】->【Edit Configurations】->【Script parameters】->按順序寫上需要的參數2. Pycharm常用快捷鍵總結

學習筆記10月25日--python的if、while、for語法

學習筆記二周三次課(10月25日)1.學習python特殊的縮進和語法python的縮進和冒號python之所以如此簡單,歸功於他的縮進機制,嚴格的縮進機制使得代碼非常整齊規範,賞心悅目,提高了可讀性,在一定意義上提高了可維護性。但對於從其他語言轉過來的朋友如:java開發人員,c語言開發,c++開發人員來說

學習筆記10月26日--復習&練習

學習筆記 python 培訓 二周四次課(10月26日)復習,做如下練習題1.實現1-100的所有的和sum = 0 for n in xrange(1, 101): sum += n n += 1 print(‘1+2+3+...+100 = ‘ + str(sum))結果:1+

學習筆記11月02日--高階函數

學習筆記 python培訓 三周四次課(11月2日)1.高階函數高級函數就是把函數當成參數傳遞的一種函數:例如:def add(x, y, f): return f(x) + f(y) print(add(-8, 11, abs))結果:19解釋:1,調用add函數,分別執行abs(-8)