Cocos2d-x 3.x游戏开发之旅 笔记
#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游戏开发之旅 笔记的更多相关文章
- Android游戏开发之旅 View类详解
Android游戏开发之旅 View类详解 自定义 View的常用方法: onFinishInflate() 当View中所有的子控件 均被映射成xml后触发 onMeasure(int, int) ...
- Aery的UE4 C++游戏开发之旅(1)基础对象模型
目录 UObject Actor种类 AActor APawn(可操控单位) AController(控制器) AGameMode(游戏模式) AHUD(HUD) ... Component种类 UA ...
- Aery的UE4 C++游戏开发之旅(3)蓝图
目录 蓝图 蓝图命名规范 蓝图优化 暴露C++至蓝图 暴露C++类 暴露C++属性 暴露C++函数 暴露C++结构体/枚举 暴露C++接口 蓝图和C++的结合方案 使用继承重写蓝图 使用组合重写蓝图 ...
- Aery的UE4 C++游戏开发之旅(4)加载资源&创建对象
目录 资源的硬引用 硬指针 FObjectFinder<T> / FClassFinder<T> 资源的软引用 FSoftObjectPaths.FStringAssetRef ...
- Aery的UE4 C++游戏开发之旅(2)编码规范
目录 C++基础类型规范 命名规范 头文件规范 字符串规范 字符集规范 参考 C++基础类型规范 由于PC.XBOX.PS4等各平台的C++基础类型大小可能不同(实际上绝大部分都是整型类型的大小不同) ...
- Aery的UE4 C++游戏开发之旅(5)字符&字符串
目录 TCHAR 字符 使用TEXT()宏包裹字符串字面量 转换字符编码 FString 字符串 FString 剖析 FString 使用 FName 字符串 FName 剖析 FName 使用 F ...
- 《cocos2d-x游戏开发之旅》问题2016-10-7
今天按书上做,遇到问题卡住了 书P115 项目是 littlerunner
- HTML5 Canvas核心技术图形动画与游戏开发(读书笔记)----第一章,基础知识
一,canvas元素 1 为了防止浏览器不支持canvas元素,我们设置“后备内容”(fallback content),下面紫色的字即为后备内容 <canvas id="canvas ...
- 【SIKIA计划】_07_Unity3D游戏开发-坦克大战笔记
[新增分类][AudioClips]音频剪辑[AudioMixers]音频混合器[Editor][Fonts]字体[Materials]材质[Models]模型[Standard Assets] [渲 ...
随机推荐
- HTML5事件—visibilitychange 页面可见性改变事件
转:https://blog.csdn.net/yusirxiaer/article/details/73480916 又看到一个很有意思的HTML5事件 visibilitychange事件是浏览器 ...
- 中文多分类 BERT
直接把自己的工作文档导入的,由于是在外企工作,所以都是英文写的 Steps: git clone https://github.com/google-research/bert prepare dat ...
- Spark的HA部署
一.安装JDK.Scala 二.安装zookeeper 三.安装Hadoop 四.安装Spark 1.修改spark/conf/spark-env.sh export JAVA_HOME=/usr/j ...
- hdu1556 树状数组区间更新单点查询板子
就是裸的区间更新: 相对于直观的线段树的区间更新,树状数组的区间更新原理不太相同:由于数组中的一个结点控制的是一块区间,当遇到更新[l,r]时,先将所有能控制到 l 的结点给更新了,这样一来就是一下子 ...
- python 全栈开发,Day81(博客系统个人主页,文章详情页)
一.个人主页 随笔分类 需求:查询当前站点每一个分类的名称以及对应的文章数 完成这个需求,就可以展示左侧的分类 它需要利用分组查询,那么必须要会基于双下划线的查询. 基于双下划线的查询,简单来讲,就是 ...
- VS-常用的快捷键-总结
1: 快速添加引用 === Shift+Alt+F10; 也可用于实现抽象类 2: 直接添加属性 ===prop+Tab+Tab; 3: 根据字段添加属性 === Ctrl +r+e; 4: 格式化代 ...
- 【C++ Primer | 06】 函数
contexpr函数 const用于运行期常量,constexpr用于编译期常量 • [test1.cpp] #include <iostream> using namespace std ...
- SpringMVC集成springfox-swagger2自动生成接口文档
本节内容: 什么是Swaggger Springfox与Swagger的关系 SpringMVC集成springfox-swagger2 一.什么是Swaggger Swagger是一个流行的API开 ...
- 每天减一半。问多少天这个绳子会小于5米?进而得while和for的关系
一:前提 1.程序 2.结果 3.使用 for的条件只要>5 变化的条件是x/=2 4.进而使用while,得第二种方法 5.结果相同 二:结论 程序可以使用for的必将可以使用while. 其 ...
- 070 关于HBase的概述
1.hbase的特点 ->数据存储量可以达到亿级别数据维持在秒级 ->按列存储的数据库 ->能够存储上百万列 ->hbase的底层存储依赖于HDFS ->如何扩展hbas ...