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] [渲 ...
随机推荐
- 【前端】JS文本比较插件
一.先上效果图 二.JS代码 /** * [文本比较插件] * 传递两个参数dom1.dom2,以dom1为基准进行比较. * 0)dom1和dom2不能都为空: * 1)如果dom1不存在,则dom ...
- CSS Zoom属性
CSS中 Zoom属性 介绍 其实Zoom属性是IE浏览器的专有属性,Firefox等浏览器不支撑.它可以设置或检索对象的缩放比例.除此之外,它还有其他一些小感化,比如触发ie的hasLayout属性 ...
- mysql中的几种join 及 full join问题
[注意]:Oracle数据库支持full join,mysql是不支持full join的,但仍然可以同过左外连接+ union+右外连接实现 初始化SQL语句: /*join 建表语句*/ ...
- LiteQuery MAX(Integer)、MAX(String) 判断是否返回值
unit Unit6; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System ...
- django----重定向
urlpatterns = [ re_path(r'^(\w+)(\w+)/$',views.index,name="index"), ] 1.<a href="{ ...
- hdu1890 splay维护区间翻转
这题的建模有点不太一样,是按结点横坐标赋予键值的 同时每次rotate和splay时都要注意下往上往下更新 /* 先建立好splay tree,将结点按num/输入顺序排序,遍历时每次将当前结点提到根 ...
- Ext.js入门:模板(四)
1.Ext.DomHelper简介2.Template语法使用简介3.Template简单应用4.Template中使用转换函数5.使用模板的自定义接口6.XTemplate应用 一:Ext.DomH ...
- centos7网卡名修改
centos7网卡名不是以etho的方式命名,有时候在自动化方面不便于管理,在安装的时候输入如下代码即可命名: net.ifnames=0 biosdevname=0
- k8s 关键字以及管理流程。
一.流程图如下 二.用户通过kubectl提交需要运行的docker container(pod). 三.api server把请求存储在etcd里面. 四.scheduler(调度)扫描,分配机器. ...
- BZOJ1406 [AHOI2007]密码箱 数论
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ1406 题意概括 求所有数x,满足 x<n 且 x2≡1 (mod n). n<=2 ...