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] [渲 ...
随机推荐
- CentOS切换为iptables防火墙并进行相关配置
CentOS切换为iptables防火墙 切换到iptables首先应该关掉默认的firewalld,然后安装iptables服务. 1.关闭firewall: service firewalld s ...
- CF126B
CF126B Password 题意: 给出一个字符串 H,找一个最长的字符串 h,使得它既作为前缀出现过.又作为后缀出现过.还作为中间的子串出现过. 解法: 沿着 $ next_n $ 枚举字符串, ...
- LeetCode(58): 最后一个单词的长度
Easy! 题目描述: 给定一个仅包含大小写字母和空格 ' ' 的字符串,返回其最后一个单词的长度. 如果不存在最后一个单词,请返回 0 . 说明:一个单词是指由字母组成,但不包含任何空格的字符串. ...
- poj3061 poj3320 poj2566尺取法基础(一)
poj3061 给定一个序列找出最短的子序列长度,使得其和大于等于S 那么只要用两个下标,区间和小于S时右端点向右移动,区间和大于S时左端点向右移动,在这个过程中更新Min #include < ...
- python 全栈开发,Day121(DButils,websocket)
昨日内容回顾 1.Flask路由 1.endpoint="user" # 反向url地址 2.url_address = url_for("user") 3.m ...
- MVC开发中的常见错误-05-无法将类型“System.Data.Entity.Infrastructure.DbQuery<BBFJ.OA.Model.RoleInfo>”转换为“System.Collections.Generic.List<BBFJ.OA.Model.RoleInfo>”
List<RoleInfo> roleInfoList = (List<RoleInfo>)ViewBag.AllRoles; 错误原因很明确了 ViewBag.AllRole ...
- extern "C" 回顾
引入:在测试"extern "C" 与gcc, g++无关"时,使用到了extern "C"的概念,网上找篇文章回顾一下. 试验如下: te ...
- MVCJSONJQuery分页实现
思路: 1.用Ado.NET获取数据 2.控制器中创建一个方法参数为搜索条件 3.返回前台一个Json对象,把对象用一个类封装 4.用JQuery接收数据 public ActionResult In ...
- asp.net core 微信获取用户openid
获取openid流程为首先根据微信开发参数构造AuthorizeUrl认证链接,用户跳转到该链接进行授权,授权完成将跳转到回调页(首次认证需要授权,后面将直接再跳转至回调页),此时回调页中带上一个GE ...
- ORA-01427: 单行子查询返回多个行
有人问题我一个问题,情况如下:他要用根据divide_act_channel_day的new_amount字段去更新divide_stat的new_amount字段.两张表关联的条件:day=log_ ...