Cocos2d-x 3.x遊戲開發之旅 筆記

上校發表於2017-03-03

#include "HelloWorldScene.h"
#include "SimpleAudioEngine.h"
#include "MyHelloWorldScene.h"


USING_NS_CC;

Scene* HelloWorld::createScene()
{
// 'scene' is an autorelease object
auto scene = Scene::create();

// 'layer' is an autorelease object
auto layer = HelloWorld::create();

// add layer as a child to scene
scene->addChild(layer);

// return the scene
return scene;
}

void HelloWorld::backHome() {
Size visibleSize = Director::getInstance()->getVisibleSize();
Label* lable = Label::create("I am home!", "Arial", 35);
lable->setPosition(Point(visibleSize.width / 2, visibleSize.height / 2));
this->addChild(lable);
}

// on "init" you need to initialize your instance
bool HelloWorld::init()
{
//////////////////////////////
// 1. super init first
if (!Layer::init())
{
return false;
}

//多點觸控 Chapter5_4_AllOnceTouchEvent
//TODO
// 單點觸控,判斷觸控了精靈如按鈕點選效果
Size visibleSize = Director::getInstance()->getVisibleSize();
Sprite* sp1 = Sprite::create("sprite1.png");
sp1->setPosition(Point(visibleSize.width / 2, visibleSize.height / 2));
this->addChild(sp1);

Sprite* sp2 = Sprite::create("sprite2.png");
sp2->setPosition(Point(visibleSize.width / 2, visibleSize.height / 2));
this->addChild(sp2);

auto listener = EventListenerTouchOneByOne::create();
listener->onTouchBegan = [](Touch* touch, Event* event) {
//註冊監聽事件時繫結了一個Node物件,在這裡就可以取出這個物件
auto target = static_cast<Sprite*>(event->getCurrentTarget());
Point pos = Director::getInstance()->convertToGL(touch->getLocationInView());
//判斷單擊的座標是否在精靈的範圍內
if (target->getBoundingBox().containsPoint(pos)) {
//設定精靈的透明度100
target->setOpacity(100);
return true;
}
return false;
};
listener->onTouchEnded = [](Touch* touch, Event* event) {
//恢復精靈的透明度
auto target = static_cast<Sprite*>(event->getCurrentTarget());
target->setOpacity(255);
};
//註冊監聽事件,繫結精靈1
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, sp1);
//註冊監聽事件,繫結精靈2,這裡要注意,listener物件複製了一份
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener->clone(), sp2);
return true;

//// TouchEvent 觸控事件 onTouchBegan觸控事件開始、onTouchMoved觸控移動事件、onTouchEnded觸控事件結束、onTouchCancelled打斷事件
//auto listener = EventListenerTouchOneByOne::create();
//listener->onTouchBegan = [](Touch* touch, Event* event) {
// Point pos1 = touch->getLocation();//獲取單擊座標,基於3D
// Point pos2 = touch->getLocationInView();//獲取單擊座標,基於2D
// Point pos3 = Director::getInstance()->convertToGL(pos2);//獲取單擊座標,基於Cocos2d-x
// log("HelloWorldScene onTouchBegan! pos1 x=%f,y=%f", pos1.x, pos1.y);
// log("HelloWorldScene onTouchBegan! pos2 x=%f,y=%f", pos2.x, pos2.y);
// log("HelloWorldScene onTouchBegan! pos3 x=%f,y=%f", pos3.x, pos3.y);
// return true;
//};
//listener->onTouchMoved = [](Touch* touch, Event* event) {
// log("HelloWorldScene onTouchMoved");
//};

//listener->onTouchEnded = [=](Touch* touch, Event* event) {
// log("HelloWorldScene onTouchEnded");
//};

//_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
//return true;

//// 動作結束監聽callbackFunc C++的功能
//Size visibleSize = Director::getInstance()->getVisibleSize();
//Sprite* xiaoRuo = Sprite::create("2.png");
//xiaoRuo->setPosition(Point(visibleSize.width / 2, visibleSize.height / 2));
//this->addChild(xiaoRuo);
////移動動作
//MoveTo* moveToHome = MoveTo::create(10.0f, Point(visibleSize.width, visibleSize.height / 2));
////回撥物件,CallFunc也是一個動作,只不過這個動作是回撥一個函式
//auto callbackFunc = [&]() {backHome(); };
//CallFunc* callFunc = CallFunc::create(callbackFunc);
////組合兩個動作
//Action* actions = Sequence::create(moveToHome, callbackFunc, NULL);
////執行動作,小若開始回家
//xiaoRuo->runAction(actions);
//return true;


//// Sequence多個動作一個一個播放、Spawn多個動作一起播放
//Size visibleSize = Director::getInstance()->getVisibleSize();
//Sprite* xiaoRuo = Sprite::create("2.png");
//xiaoRuo->setPosition(Point(visibleSize.width / 2, visibleSize.height / 2));
//this->addChild(xiaoRuo);
////建立一個移動物件
//MoveBy* moveBy = MoveBy::create(2.2f, Point(40, 20));
////建立一個彈跳動作物件,彈跳高度為100,彈跳次數為5
//JumpBy* jumpBy = JumpBy::create(3.0f, Point(50, 1), 100, 5);
////建立一個旋轉動作物件
//RotateBy* rotateBy = RotateBy::create(2.5f, 220, 10);
////建立組合動作物件,將所有動作連起來
////Action* actions = Spawn::create(moveBy, jumpBy, rotateBy, NULL);
//Action* actions = Sequence::create(moveBy, jumpBy, rotateBy, NULL);
//xiaoRuo->runAction(actions);
//return true;

//// RepeatForver迴圈動作
//Size visibleSize = Director::getInstance()->getVisibleSize();
//Sprite* xiaoRuo = Sprite::create("2.png");
//xiaoRuo->setPosition(Point(visibleSize.width / 2, visibleSize.height / 2));
//this->addChild(xiaoRuo);
////建立一個JumpBy動作物件
//JumpBy* jumpBy = JumpBy::create(3.0f, Point(50, 1), 100, 1);
////以jumpBy為引數,建立一個永久性的重複動作
//RepeatForever* repeatForverAction = RepeatForever::create(jumpBy);
////以jumpBy為引數,建立一個重複次數的動作
//Repeat* repeatAction=Repeat::create(jumpBy,3);
////執行動作
//xiaoRuo->runAction(repeatForverAction);
//return true;


//Blink 精靈閃爍
//Size visibleSize = Director::getInstance()->getVisibleSize();
//Sprite* xiaoRuo = Sprite::create("2.png");
//xiaoRuo->setPosition(Point(visibleSize.width / 2, visibleSize.height / 2));
//this->addChild(xiaoRuo);
////建立Blink動作物件
//Blink* blink = Blink::create(3.0f, 3);//持續時間、閃爍次數
//xiaoRuo->runAction(blink);
//return true;

//// ScaleTo和ScaleBy縮放精靈
////建立一個精靈
//Size visibleSize = Director::getInstance()->getVisibleSize();
//Sprite* xiaoRuo = Sprite::create("2.png");
//xiaoRuo->setPosition(Point(visibleSize.width/2,visibleSize.height/2));
//this->addChild(xiaoRuo);
//建立MoveTo動作物件
//ScaleTo* scaleTo = ScaleTo::create(2.8f,0.4f,1.0f);//引數1:持續時間 2:X方向的拉伸值 3:Y方向的拉伸值
//ScaleBy* scaleTo = ScaleBy::create(2.8f, 0.4f, 1.0f);//引數1:持續時間 2:X方向的拉伸值 3:Y方向的拉伸值
//xiaoRuo->runAction(scaleTo);
//return true;

//// MoveTo移動到指定座標、MoveBy移動距離,移動精靈
////建立一個精靈
//Size visibleSize = Director::getInstance()->getVisibleSize();
//Sprite* xiaoRuo = Sprite::create("2.png");
//xiaoRuo->setPosition(Point(50, visibleSize.height / 2));
//this->addChild(xiaoRuo);
////建立MoveTo動作物件
////MoveTo* moveTo = MoveTo::create(0.9f, Point(250, 150));
//MoveBy* moveTo = MoveBy::create(0.9f, Point(250, 150));//0.9f動作持續時間
////精靈執行動作
//xiaoRuo->runAction(moveTo);
//return true;


//// 九妹,可伸縮的圖片,九妹結合按鈕製作按鈕的點選效果
////Scale9Sprite* nineGirl=Scale9Sprite::create

////播放特效音樂
//CocosDenshion::SimpleAudioEngine::getInstance()->playEffect("effect1.wav");
//return true;

////播放背景音樂
//CocosDenshion::SimpleAudioEngine::getInstance()->playBackgroundMusic("background.mp3", true);
//return true;

//Vector是cocos帶的佇列,Map是cocos帶的map
//Vector<String> vector;
//vector.pushBack("a");
//vector.pushBack("b");
//for (auto a :vector)
//{
// log("%s", a);
//}

//Map<int, Label*> map;
//return true;

//Value可以格式化字元
//Value valStr = Value("XiaoRuo is");
//Value valInt = Value(250);
//log("%s%d", valStr.asString().c_str(), valInt.asInt());
//return true;

//建立選單MenuItemImage MenuItemLabel並且設定排列方式
//Size visibleSize = Director::getInstance()->getVisibleSize();
//MenuItemImage* pCloseItem = MenuItemImage::create(
// "CloseNormal.png", "CloseSelected.png", this, menu_selector(HelloWorld::menuCloseCallback)
//);
//Label* label = Label::create("I am Label Item.", "Arial", 30);
//MenuItemLabel* pLabelItem = MenuItemLabel::create(label);
//Menu* pmenu = Menu::create(pCloseItem, pLabelItem, NULL);
//pmenu->alignItemsVertically();
//pmenu->setPosition(Point(visibleSize.width / 2, visibleSize.height / 2));
//this->addChild(pmenu, 1);
//return true;

//建立精靈
Sprite* sprite = Sprite::create("mypic.png");
sprite->setPosition(Point(200, 200));
this->addChild(sprite);
return true;

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

///////////////////////////////
//// 2. add a menu item with "X" image, which is clicked to quit the program
//// you may modify it.

//// add a "close" icon to exit the progress. it's an autorelease object
//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));

//// create menu, it's an autorelease object
//auto menu = Menu::create(closeItem, NULL);
//menu->setPosition(Vec2::ZERO);
//this->addChild(menu, 1);

///////////////////////////////
//// 3. add your codes below...

//// add a label shows "Hello World"
//// create and initialize a label

//auto label = Label::createWithTTF("Hello World", "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));

//// add the label as a child to this layer
//this->addChild(label, 1);

//// add "HelloWorld" splash screen"
//auto sprite = Sprite::create("HelloWorld.png");

//// position the sprite on the center of the screen
//sprite->setPosition(Vec2(visibleSize.width / 2 + origin.x, visibleSize.height / 2 + origin.y));

//// add the sprite as a child to this layer
//this->addChild(sprite, 0);

//return true;
}


void HelloWorld::menuCloseCallback(Ref* pSender)
{
//Director::getInstance()->replaceScene(MyHelloWorld::createScene());
/*Director::getInstance()->replaceScene(TransitionSlideInT::create(3.0f, MyHelloWorld::createScene()));*/
Director::getInstance()->pushScene(TransitionSlideInT::create(3.0f, MyHelloWorld::createScene()));//不釋放場景

////Close the cocos2d-x game scene and quit the application
//Director::getInstance()->end();

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

/*To navigate back to native iOS screen(if present) without quitting the application ,do not use Director::getInstance()->end() and exit(0) as given above,instead trigger a custom event created in RootViewController.mm as below*/

//EventCustom customEndEvent("game_scene_close_event");
//_eventDispatcher->dispatchEvent(&customEndEvent);


}

相關文章