#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);

}

Cocos2d-x 3.x游戏开发之旅 笔记的更多相关文章

  1. Android游戏开发之旅 View类详解

    Android游戏开发之旅 View类详解 自定义 View的常用方法: onFinishInflate() 当View中所有的子控件 均被映射成xml后触发 onMeasure(int, int) ...

  2. Aery的UE4 C++游戏开发之旅(1)基础对象模型

    目录 UObject Actor种类 AActor APawn(可操控单位) AController(控制器) AGameMode(游戏模式) AHUD(HUD) ... Component种类 UA ...

  3. Aery的UE4 C++游戏开发之旅(3)蓝图

    目录 蓝图 蓝图命名规范 蓝图优化 暴露C++至蓝图 暴露C++类 暴露C++属性 暴露C++函数 暴露C++结构体/枚举 暴露C++接口 蓝图和C++的结合方案 使用继承重写蓝图 使用组合重写蓝图 ...

  4. Aery的UE4 C++游戏开发之旅(4)加载资源&创建对象

    目录 资源的硬引用 硬指针 FObjectFinder<T> / FClassFinder<T> 资源的软引用 FSoftObjectPaths.FStringAssetRef ...

  5. Aery的UE4 C++游戏开发之旅(2)编码规范

    目录 C++基础类型规范 命名规范 头文件规范 字符串规范 字符集规范 参考 C++基础类型规范 由于PC.XBOX.PS4等各平台的C++基础类型大小可能不同(实际上绝大部分都是整型类型的大小不同) ...

  6. Aery的UE4 C++游戏开发之旅(5)字符&字符串

    目录 TCHAR 字符 使用TEXT()宏包裹字符串字面量 转换字符编码 FString 字符串 FString 剖析 FString 使用 FName 字符串 FName 剖析 FName 使用 F ...

  7. 《cocos2d-x游戏开发之旅》问题2016-10-7

    今天按书上做,遇到问题卡住了 书P115 项目是 littlerunner

  8. HTML5 Canvas核心技术图形动画与游戏开发(读书笔记)----第一章,基础知识

    一,canvas元素 1 为了防止浏览器不支持canvas元素,我们设置“后备内容”(fallback content),下面紫色的字即为后备内容 <canvas id="canvas ...

  9. 【SIKIA计划】_07_Unity3D游戏开发-坦克大战笔记

    [新增分类][AudioClips]音频剪辑[AudioMixers]音频混合器[Editor][Fonts]字体[Materials]材质[Models]模型[Standard Assets] [渲 ...

随机推荐

  1. JProfiler 入门教程

    推荐文章:JProfiler 入门教程 一.安装JProfiler 从http://www.ej-technologies.com/下载5.1.2并申请试用序列号 二.主要功能简介 1.内存剖析 Me ...

  2. php中常用的正则表达式函数

    php中常用的正则表达式函数 * preg_match() * preg_match_all() * preg_replace() * preg_filter() * preg_grep() * pr ...

  3. bzoj3224 splay板子

    开始学习新知识:splay——tree 是个板子题,学习splay可以看博客 https://blog.csdn.net/Clove_unique/article/details/50630280 # ...

  4. 步步为营-66-Socket通信

    1.0 版本 1.1 服务器端 using System; using System.Collections.Generic; using System.Linq; using System.Net; ...

  5. java 判断字符串什么编码类型

    public static String getEncoding(String str) { String encode = "GB2312"; try { if (str.equ ...

  6. OSGI命令

    OSGi的一些支离破碎的知识 以下命令说明内容来自于Eclipse的OSGi框架Equinox. ---Controlling the OSGi framework---launch - start ...

  7. 【UER #1】DZY Loves Graph(待卡常数)

    题解: 正解是可持久化并查集 但这个显然是lct可以维护的 但这常数是个问题啊??? #include <bits/stdc++.h> using namespace std; struc ...

  8. python_cookbook之路:数据结构-解压可迭代对象赋值给多个变量以及扩展的迭代解压语法(*)

    1.一一对应: >>> data = [ 'ACME', 50, 91.1, (2012, 12, 21) ] >>> name, shares, price, d ...

  9. java连接数据库(jdbc)调用配置文件

    各种语言都有自己所支持的配置文件,后缀名“.properties”结尾的就是其中之一. 在java连接数据库时,采取读取配置文件的方式,来获取数据库连接. 新建jdbc.properties文件,内容 ...

  10. HDU 2602 Bone Collector 骨头收集者【01背包】

    题目链接:https://vjudge.net/contest/103424#problem/A 题目大意: 第一行输入几组数据,第二行第一个数字代表物体个数,第二个数代表总体积.需要注意的是,第三排 ...