Last active
March 26, 2017 07:38
-
-
Save ugonight/e38936087e531efa0459648e0d4decf3 to your computer and use it in GitHub Desktop.
初心者向け!cocos2d-xでの脱出ゲーム制作の手引き ref: http://qiita.com/ugonight_nanase/items/06f2c42dc543d5d32518
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "AppDelegate.h" | |
#include "control.h" //"HelloWorldScene.h" | |
~ | |
// create a scene. it's an autorelease object | |
auto scene = Control::create(); // HelloWorld::createScene(); | |
// run | |
director->runWithScene(scene); | |
return true; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
~ | |
void Control::changeField(std::string s) { | |
removeChildByName("field"); | |
addChild(mFieldList[s], 0, "field"); | |
mFieldList[s]->changedField(); //追記 | |
} | |
void Control::showMsg(std::string s) { | |
((Label*)getChildByName("msg"))->setString(s); | |
} | |
//追記 | |
Field* Control::getField(std::string field) { | |
return mFieldList[field]; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
~ | |
public: | |
virtual bool init(); | |
void initField(); | |
void changeField(std::string field); | |
void showMsg(std::string msg); | |
Field* getField(std::string field); //追記 | |
static Control* getInstance(); | |
CREATE_FUNC(Control); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
~ | |
void Field::changedField() { | |
//現在のフィールドに移動したときに呼び出される処理を書く | |
} | |
ObjectN* Field::getObject(std::string s) { | |
return mObjectList[s]; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include "field.h" | |
#include "object.h" | |
#include "control.h" | |
#include "item.h" //追記 | |
#include "cocos2d.h" | |
class Field : public cocos2d::Layer { | |
protected: | |
std::map<std::string, ObjectN*> mObjectList; | |
void addObject(ObjectN* obj, std::string objName, int ZOder, bool addChild); | |
public: | |
virtual bool init(); | |
virtual void initField(); | |
virtual void changedField(); | |
ObjectN* getObject(std::string objName); //追記 | |
CREATE_FUNC(Field); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "field1.h" | |
USING_NS_CC; | |
void Field1::initField() | |
{ | |
auto visibleSize = Director::getInstance()->getVisibleSize(); | |
Vec2 origin = Director::getInstance()->getVisibleOrigin(); | |
auto bg = Sprite::create("field1.png"); | |
bg->setPosition(visibleSize / 2); | |
addChild(bg, 0, "bg"); | |
auto door = ObjectN::create(); | |
door->setArea(Rect(190, 100, 70, 110)); | |
door->setTouchEvent(CallFunc::create([this] { | |
if (Item::getInstance()->getSelectedItem() == "key") { | |
Control::getInstance()->showMsg("Great!You are a friend who can escape!"); | |
Item::getInstance()->deleteItem("key"); | |
} | |
else { | |
Control::getInstance()->showMsg("The door is locked."); | |
} | |
})); | |
addObject(door, "door", 1, true); | |
auto right = ObjectN::create(); | |
right->setArea(Rect(440, 0, 40, 200)); | |
right->setTexture("right.png"); | |
right->setTouchEvent(CallFunc::create([this] { | |
Control::getInstance()->changeField("field2"); | |
Control::getInstance()->showMsg("turn right"); | |
})); | |
addObject(right, "right", 2, true); | |
auto key = ObjectN::create(); | |
key->setTexture("key.png"); | |
key->setPosition(Vec2(300, 100)); | |
key->setArea(Rect(280, 200, 40, 40)); | |
key->setTouchEvent(CallFunc::create([this] { | |
Control::getInstance()->showMsg("I got the key"); | |
Item::getInstance()->itemGet("key"); | |
removeChildByName("key"); | |
})); | |
addObject(key, "key", 2, false); | |
} | |
void Field1::changedField() { | |
auto field2 = Control::getInstance()->getField("field2"); | |
if (!field2->getChildByName("rope")) { | |
addChild(mObjectList["key"], 2, "key"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include "field.h" | |
#include "cocos2d.h" | |
class Field1 : public Field { | |
public: | |
virtual void initField(); | |
virtual void changedField(); | |
CREATE_FUNC(Field1); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "field2.h" | |
USING_NS_CC; | |
void Field2::initField() | |
{ | |
auto visibleSize = Director::getInstance()->getVisibleSize(); | |
Vec2 origin = Director::getInstance()->getVisibleOrigin(); | |
auto bg = Sprite::create("field2.png"); | |
bg->setPosition(visibleSize / 2); | |
addChild(bg, 0, "bg"); | |
auto left = ObjectN::create(); | |
left->setArea(Rect(0, 0, 40, 200)); | |
left->setTexture("left.png"); | |
left->setTouchEvent(CallFunc::create([this] { | |
Control::getInstance()->changeField("field1"); | |
Control::getInstance()->showMsg("turn left"); | |
})); | |
addObject(left, "left", 1, true); | |
auto rope = ObjectN::create(); | |
rope->setArea(Rect(345, 0, 30, 190)); | |
rope->setTouchEvent(CallFunc::create([this] { | |
Control::getInstance()->showMsg("There was a noise behind."); | |
removeChildByName("rope"); | |
})); | |
addObject(rope, "rope", 2, true); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include "field.h" | |
#include "cocos2d.h" | |
class Field2 : public Field { | |
public: | |
virtual void initField(); | |
CREATE_FUNC(Field2); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "control.h" | |
#include "field.h" | |
//Fieldクラスをinclude | |
#include "field1.h" | |
#include "field2.h" | |
void Control::initField() { | |
//場面登録 | |
mFieldList["field1"] = Field1::create(); | |
mFieldList["field2"] = Field2::create(); | |
for (auto field : mFieldList) { | |
field.second->retain(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "item.h" | |
void Item::initItem() { | |
mItemList["key"] = new ItemObj("key.png"); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "item.h" | |
USING_NS_CC; | |
//追記 | |
static Item* instance; | |
Item *Item::getInstance() { | |
return instance; | |
} | |
bool Item::init() | |
{ | |
if (!Layer::init()) | |
{ | |
return false; | |
} | |
auto visibleSize = Director::getInstance()->getVisibleSize(); | |
Vec2 origin = Director::getInstance()->getVisibleOrigin(); | |
auto itemWindow = Sprite::create("itemWindow.png"); | |
itemWindow->setAnchorPoint(Vec2::ANCHOR_BOTTOM_LEFT); | |
itemWindow->setPosition(Vec2::ZERO); | |
addChild(itemWindow, 0, "itemWindow"); | |
auto possession = Layer::create(); | |
addChild(possession, 1, "possession"); | |
//追記 | |
auto frame = Sprite::create("select.png"); | |
frame->setPosition(Vec2(-32, 0)); | |
frame->setAnchorPoint(Vec2::ANCHOR_BOTTOM_LEFT); | |
addChild(frame, 2, "frame"); | |
initItem(); | |
updateItem(); | |
//追記 | |
auto listener = EventListenerTouchOneByOne::create(); | |
listener->setSwallowTouches(true); | |
listener->onTouchBegan = CC_CALLBACK_2(Item::touchEvent, this); | |
this->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener, this); | |
mSelectedItem = ""; | |
instance = this; | |
return true; | |
} | |
void Item::updateItem() { | |
auto possession = getChildByName("possession"); | |
possession->removeAllChildren(); | |
for (auto item : mItemList) { | |
if (item.second->getGetFlag()) { | |
auto spr = Sprite::create(item.second->getImage()); | |
spr->setAnchorPoint(Vec2::ANCHOR_BOTTOM_LEFT); | |
spr->setPosition(Vec2(32 * possession->getChildrenCount(), 0)); | |
possession->addChild(spr, 1, item.first); | |
} | |
} | |
} | |
void Item::itemGet(std::string s) { | |
mItemList[s]->setGetFlag(1); | |
updateItem(); | |
} | |
void Item::deleteItem(std::string s) { | |
mItemList[s]->setGetFlag(0); | |
updateItem(); | |
} | |
//追記 | |
bool Item::touchEvent(cocos2d::Touch* touch, cocos2d::Event* event) { | |
auto window = getChildByName("itemWindow"); | |
auto targetBox = window->getBoundingBox(); | |
auto touchPoint = touch->getLocation(); | |
if (targetBox.containsPoint(touchPoint)) { | |
for (auto item : getChildByName("possession")->getChildren()) { | |
if (item->getBoundingBox().containsPoint(touchPoint)) { | |
mSelectedItem = item->getName(); | |
getChildByName("frame")->setPositionX(item->getPositionX()); | |
break; | |
} | |
} | |
return true; | |
} | |
return false; | |
} | |
std::string Item::getSelectedItem() { return mSelectedItem; } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include "cocos2d.h" | |
class Item : public cocos2d::Layer { | |
public: | |
virtual bool init(); | |
void itemGet(std::string itemName); | |
void deleteItem(std::string itemName); | |
//追記 | |
std::string getSelectedItem(); | |
static Item* getInstance(); | |
CREATE_FUNC(Item); | |
private: | |
class ItemObj { | |
std::string imgName; | |
bool getFlag = 0; | |
public: | |
ItemObj(std::string imageName) { | |
imgName = imageName; | |
} | |
std::string getImage() { return imgName; }; | |
void setGetFlag(bool i) { getFlag = i; }; | |
bool getGetFlag() { return getFlag; }; | |
}; | |
std::map<std::string, ItemObj*> mItemList; | |
std::string mSelectedItem; //追記 | |
void initItem(); | |
void updateItem(); | |
bool touchEvent(cocos2d::Touch* touch, cocos2d::Event* event); //追記 | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "object.h" | |
USING_NS_CC; | |
bool ObjectN::init() { | |
if (!Sprite::init()) | |
{ | |
return false; | |
} | |
mArea = Rect(0, 0, 0, 0); | |
mTouchEvent = CallFunc::create([]() {return; }); | |
mTouchEvent->retain(); | |
//デフォルトで中央に表示 | |
this->setPosition(Director::getInstance()->getVisibleSize() / 2); | |
return true; | |
} | |
void ObjectN::setArea(cocos2d::Rect rect) { | |
mArea = rect; | |
auto listener = EventListenerTouchOneByOne::create(); | |
listener->onTouchBegan = CC_CALLBACK_2(ObjectN::touchOn, this); | |
this->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener, this); | |
} | |
bool ObjectN::touchOn(cocos2d::Touch* touch, cocos2d::Event* event) { | |
if (mArea.containsPoint(Vec2(touch->getLocationInView().x, touch->getLocationInView().y))) { | |
runAction(mTouchEvent); | |
return true; | |
} | |
return false; | |
} | |
void ObjectN::setTouchEvent(cocos2d::CallFunc *func) { mTouchEvent = func; mTouchEvent->retain(); } | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include "cocos2d.h" | |
class ObjectN : public cocos2d::Sprite { | |
cocos2d::Rect mArea; | |
cocos2d::CallFunc *mTouchEvent; | |
bool touchOn(cocos2d::Touch* touch, cocos2d::Event* event); | |
public: | |
virtual bool init(); | |
void setArea(cocos2d::Rect rect); | |
void setTouchEvent(cocos2d::CallFunc *func); | |
CREATE_FUNC(ObjectN); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment