Cocos2d 基础知识 2
单点触摸
//触摸开始
bool Chapter05::onTouchBegan(Touch*touch,Event *unused_event)
//{
// log("Touch Began\r\n");
// Vec2 touchPoint =touch->getLocation();
// log("x:%f,y%f",touchPoint.x,touchPoint.y);
// return true;
//}
//触摸移动
// void Chapter05::onTouchMoved(Touch*touch,Event *unused_event)
//{
// auto drawNode=DrawNode::create();//创造 轨迹
// this->addChild(drawNode,10);
// Vec2 touchPoint =touch->getPreviousLocation();
// log("Touch Moved\r\n");
// log("x:%f,y%f",touchPoint.x,touchPoint.y);
// if (sp->getBoundingBox().containsPoint(touch->getLocation()))//边界框 包含()
// {
// sp->setPosition(touch->getLocation());// 位置
drawNode->drawLine(touch->getLocation(), touch->getPreviousLocation(), Color4F(1, 0, 0, 1));
// }
// drawNode->drawLine(touch->getLocation(), touch->getPreviousLocation(), Color4F(1, 0, 0, 1));
//
//}
触摸结束
// void Chapter05::onTouchEnded(Touch*touch,Event *unused_event)
//{
// Vec2 touchPoint =touch->getStartLocation();
// log("Touch Ended\r\n");
//
//}
触摸取消:例 手机出现了来电,是我们游戏进入后台
// void Chapter05::onTouchCancelled(Touch*touch,Event *unused_event)
//{
// long("Touch Cancelled\r\n");
//}
//单点触摸
onEnter()
{
/*// auto touchListener=EventListenerTouchOneByOne::create();//触摸监听
// touchListener->onTouchBegan=CC_CALLBACK_2(Chapter05::onTouchBegan, this);
// touchListener->onTouchMoved=CC_CALLBACK_2(Chapter05::onTouchMoved, this);
// touchListener->onTouchEnded=CC_CALLBACK_2(Chapter05::onTouchEnded, this);
// touchListener->onTouchCancelled=CC_CALLBACK_2(Chapter05::onTouchCancelled, this);
//
// Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority (touchListener,this);//注册该监听
// drawNode->drawLine(touch->getLocation(), touch->getPreviousLocation(), Color4F(1, 0, 0, 1));*/
}
在哪里注册就在哪里销毁,销毁触摸接听
void Chapter05::onExit()
{
Director::getInstance()->getEventDispatcher()->removeEventListenersForTarget(this);
Layer::onExit();
}
或者种形式
void ChaCase1::onEnter()
{
Layer::onEnter();
auto touchListener = EventListenerTouchOneByOne::create();
touchListener->onTouchBegan = CC_CALLBACK_2(ChaCase1::onTouchBegan,this);
touchListener->onTouchMoved = CC_CALLBACK_2(ChaCase1::onTouchMoved,this);
touchListener->onTouchEnded = CC_CALLBACK_2(ChaCase1::onTouchEnded, this);
touchListener->onTouchCancelled=[=](Touch* t,Event * e)
{
Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(touchListener, this);
};
touchListener->setSwallowTouches(true);
Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(touchListener, this);
this->scheduleUpdate();
}
//模态对话框
/* {
auto visableSize = Director::getInstance()->getVisibleSize();
auto sp = Sprite::create("StartBG.png");
sp->setPosition(240,160);
sp->setScale(sp->getContentSize().width/visableSize.width, sp->getContentSize().height/visableSize.height);
this->addChild(sp);
//
//
auto btn1 = Button::create("c0.png");
btn1->setPosition(Vec2(240, 160));
btn1->addClickEventListener([=](Ref* sender){
cout<<"btn1"<<endl;
auto widget = Widget::create();//创建一个widget对象 ,类似于一个层
widget->setAnchorPoint(Vec2(0, 0));//设置锚点
widget->setPosition(Vec2(0, 0));//设置位置
widget->setContentSize(visableSize);//设置覆盖区域
widget->setTouchEnabled(true);//开启触摸
widget->setSwallowTouches(true);//吞噬触摸
this->addChild(widget);
auto sp1 = Sprite::create("tabBg1.png");
sp1->setPosition(240,160);
widget->addChild(sp1);
auto _btn = Button::create("c4.png");
_btn->setAnchorPoint(Vec2(1, 1));
_btn->setPosition(sp1->getContentSize());
_btn->addClickEventListener([=](Ref* sender){
cout<<"_btn"<<endl;
//删除父节点中的当前节点并清除动作及回调函数
widget->removeFromParentAndCleanup(true);
});
sp1->addChild(_btn);
});
this->addChild(btn1);
auto btn2 = Button::create("c1.png");
btn2->setPosition(Vec2(240, 220));
btn2->addClickEventListener([=](Ref* sender){
cout<<"btn2"<<endl;
});
this->addChild(btn2);
auto btn3 = Button::create("c2.png");
btn3->setPosition(Vec2(240, 100));
btn3->addClickEventListener([=](Ref* sender){
cout<<"btn3"<<endl;
});
this->addChild(btn3);
}*/
//画轨迹
/*// auto drawNode=DrawNode::create();//创造 轨迹
//
// this->addChild(drawNode,10);
// drawNode->setLineWidth(10);//线宽
//画路线
// drawNode->drawLine(Vec2(0,0), Vec2(480,320), Color4F(1, 0, 0, 1));//画路线
// // 开始位置 结束位置 线颜色
// //画圆
// drawNode->drawCircle(Vec2(240,160), 50, 90, 4, false, Color4F(1, 0, 0, 1));//画圆
// //画点
// drawNode->drawDot(Vec2(50,50), 20, Color4F(0,0,0,1));
//rect矩形
// drawNode->drawRect(Vec2(visableSize.width/2-100,50), Vec2(visableSize.width/2+100,50), Vec2(visableSize.width/2+100,100), Vec2(visableSize.width/2-100, 100), Color4F(1,0,0,1));
//
//drawCubicBezier贝塞尔曲线
// drawNode->drawCubicBezier(Vec2(0,origin.y), Vec2(visableSize.width/2,origin.y+visableSize.height/4), Vec2(visableSize.width/2,origin.y+visableSize.height*3/4), Vec2(visableSize.width, origin.y+visableSize.height), 100, Color4F(1,1,0,1));
*/
广告字幕循环
auto text =Text::create ("!京北来迎欢","",40);
text->setPosition(Vec2(240,160));
this->addChild(text,1);
auto moveby=MoveBy::create(6, Vec2(270,0 ));
auto call = CallFunc::create([=](){
text->setPosition(Vec2(0,160));
});
auto sequence = Sequence::create(moveby,call, NULL);
auto rep = RepeatForever::create(sequence);
text->runAction(rep);
auto stencil=Sprite::create("fish_1.png");
auto clippingNode=ClippingNode::create();
clippingNode->setStencil(stencil);//设置裁剪模板
clippingNode->setInverted(true);//设置地板可见
clippingNode->setAlphaThreshold(0);//设置绘制底板透明度为0
clippingNode->setPosition(Vec2(240,160));
this->addChild(clippingNode,2);
auto content=Sprite::create("任务效果图.png");
content->setScale(content->getContentSize().width/240, content->getContentSize().height/160);
clippingNode->addChild(content,2);
多点触摸
void Chapter05::onEnter()
{
Layer::onEnter();
auto touchListener=EventListenerTouchAllAtOnce::create();
touchListener->onTouchesBegan=CC_CALLBACK_2(Chapter05::onTouchesBegan,this );
touchListener->onTouchesMoved=CC_CALLBACK_2(Chapter05::onTouchesMoved,this );
touchListener->onTouchesEnded=CC_CALLBACK_2(Chapter05::onTouchesEnded,this );
touchListener->onTouchesCancelled=CC_CALLBACK_2(Chapter05::onTouchesCancelled,this );
Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(touchListener, this);
}
void Chapter05::onExit()
{
Director::getInstance()->getEventDispatcher()->removeEventListenersForTarget(this);
Layer::onExit();
}
void Chapter05::onTouchesBegan(const std::vector<Touch*>& touches, Event *unused_event)
{
log("\r\nTouches began\r\n");
for (auto item:touches)
{
log("x=%f,y=%f",item->getLocation().x,item->getLocation().y);
}
if(touches.size()>1)
{
auto a1=touches[0]->getLocation();
auto a2=touches[1]->getLocation();
distance=a1.distance(a2);
}
}
void Chapter05::onTouchesMoved(const std::vector<Touch*>& touches, Event *unused_event)
{
log("\r\nTouches began\r\n");
for(auto item:touches)
{
log("x=%f,y=%f",item->getLocation().x,item->getLocation().y);
}
if(touches.size()>1)
{
auto a11=touches[0]->getLocation();
auto a12=touches[1]->getLocation();
float _distance=a11.distance(a12);//两点的距离
float _scale=_distance/distance;
if (_scale>=10)
{
_scale=10;
}
sp->setScale( _scale);
}
}
void Chapter05::onTouchesEnded(const std::vector<Touch*>& touches, Event *unused_event)
{
log("\r\nTouches began\r\n");
for(auto item:touches)
{
log("x=%f,y=%f",item->getLocation().x,item->getLocation().y);
}
}
void Chapter05::onTouchesCancelled(const std::vector<Touch*>& touches, Event *unused_event)
{
log("\r\ntouches cancellde\r\n");
}
删除之前动作
auto _callfunc=CallFunc::create([=](){
//removeFromParentAndCleanup
表示把sprite从屏幕上删除
//删除父节点中的当前节点并清除动作及回调函数
progressCircle1->removeFromParentAndCleanup(true);
});
//一样的功能,删除
// auto _callfunc=RemoveSelf::create();
或
this->removeChild(text);
弧长=nπr/180,在这里n就是角度数,即圆心角n所对应的弧长。
n=弧度/ π*180 n就是角度数
atan();求弧度的函数
Vec2 touchPoint=touch->getPreviousLocation();
float tan;
float angle;
if (touchPoint.x>=240)
{
tan=touchPoint.y/(touchPoint.x-240);
angle=90-atan(tan)/3.14*180;
s666->setRotation(angle);
}
if (touchPoint.x<240)
{
tan=touchPoint.y/(240-touchPoint.x);
angle=270+atan(tan)/3.14*180;
s666->setRotation(angle);
}
或
float CatchFish1::cannonRotation(Point p1,Point p2)//大炮旋转 p1是鼠标位置 p2是大炮位置
{
float offx=p1.x-p2.x;
float offy=p1.y-p2.y;
if (offy<=0)
{
return -180;
}
float ratio= offy/ offx;
float angle=atan(ratio)/M_PI*180;
if (angle>0) {
cannon->setRotation(90-angle);
return 90-angle;
}
else
{
cannon->setRotation(-90-angle);
return -90+angle;
}
}
static Color4F color=Color4F::BLUE;
layer1=LayerColor::create(Color4B::WHITE,300,200);
btn3->setScale9Enabled(true);
碰撞框
bool containsPoint(const CCPoint& point) const;
bool intersectsRect(const CCRect& rect) const;
Vec2 PerPoint=touch->getPreviousLocation();//
Vec2 touchPoint=touch->getLocation();//当前点
碰撞的图片 鼠标当前点
if (layer1->getBoundingBox().containsPoint(touchPoint))
{
drawNode->drawLine(PerPoint, touchPoint, color);
}
//矩形与矩形的碰撞检测
auto fishNetRect = fishNet->getBoundingBox();
auto fishRect = fish->getBoundingBox();
fishNetRect.intersectsRect(fishRect))//相交的矩形
//粒子效果气泡
auto bubble=ParticleSystemQuad::create("qipaom.plist");
bubble->setPosition(Vec2(240, 100));
bubble->setAutoRemoveOnFinish(true);//是否在结束后自动删除
bubble->setEmissionRate(5);//每秒 5个
bubble->setDuration(-1);//循环
this->addChild( bubble,4);
第7章屏幕适配
屏幕适配 第一个参数 设计分辨率宽 高 适配策略
setDesignResolutionSize(DW, DH, resolutionPolicy)
资源分辨率,设计分辨率,屏幕分辨率
Resources width 以下简写为RW, Resources height 以下简写为RH
Design width 以下简写为DW, Design height 以下简写为DH
Screen width 以下简写为SW, Screen height 以下简写为SH
例如:
EXACT_FIT:屏幕宽 与 设计宽比 作为X方向的缩放因子,屏幕高 与 设计高比 作为Y方向的缩放因子。保证了设计区域完全铺满屏幕,但是可能会出现图像拉伸。
NO_BORDER:屏幕宽、高分别和设计分辨率宽、高计算缩放因子,取较(大)者作为宽、高的缩放因子。保证了设计区域总能一个方向上铺满屏幕,而另一个方向一般会超出屏幕区域。
SHOW_ALL:屏幕宽、高分别和设计分辨率宽、高计算缩放因子,取较(小)者作为宽、高的缩放因子。保证了设计区域全部显示到屏幕上,但可能会有黑边。
FIXED_HEIGHT:保持传入的设计分辨率高度不变,根据屏幕分辨率修正设计分辨率的宽度。
FIXED_WIDTH:保持传入的设计分辨率宽度不变,根据屏幕分辨率修正设计分辨率的高度。
#include "cocostudio/CocoStudio.h"
#include "ui/CocosGUI.h"
using namespace cocos2d::ui;
auto root=CSLoader::createNode("Layer.csb");
this->addChild(root);
AppDelegate.cpp里面写 文件夹
FileUtils::getInstance()->addSearchPath(“COCOSSTUDIO");
作用是写图片:不用加COCOSSTUDIO/
新设置的路径放在路径搜索的前面
MessageBox 写2个参数 提示
Cocos2d 基础知识 2相关推荐
- cocos2d html5 mysql_cocos2d-html5基础知识
cocos2d-html5是cocos2d引擎家族中的的html5实现! cocos2d-html5文件结构如下: box2d(对box2d物理引擎支持的js) HelloWorld tests(测试 ...
- Cocos2d之Box2d基础知识
一.box2d基础知识 1.关于 Box2D 是一个用于游戏的 2D 刚体仿真库.从游戏的视角来看,物理引擎就是一个程序性动画(procedural animation)的系统,而不是由动画师去移动你 ...
- Python 基础知识及实战
Python是一门简单易学的编程语言,支持非常多的模块和功能,适合于初学者.Python属于解释型语言,这意味着你在运行时不需要进行编译,解释器会解释并运行你的代码.这虽然会降低运行速度(不到几秒), ...
- 嵌入式Linux的OTA更新,基础知识和实现
嵌入式Linux的OTA更新,基础知识和实现 OTA updates for Embedded Linux, Fundamentals and implementation 更新的需要 一旦嵌入式Li ...
- 计算机基础知识第十讲,计算机文化基础(第十讲)学习笔记
计算机文化基础(第十讲)学习笔记 采样和量化PictureElement Pixel(像素)(链接: 采样的实质就是要用多少点(这个点我们叫像素)来描述一张图像,比如,一幅420x570的图像,就表示 ...
- 嵌入式linux编程,嵌入式Linux学习笔记 - 嵌入式Linux基础知识和开发环境的构建_Linux编程_Linux公社-Linux系统门户网站...
注:所有内容基于友善之臂Mini2440开发板 一.嵌入式Linux开发环境的构建 嵌入式开发一般分为三个步骤: 1.编译bootloader,烧到开发板 2.编译嵌入式Linux内核,烧到开发板 3 ...
- 《计算机网络应用基础》模拟试卷(六),《计算机与网络应用基础知识1》模拟试卷...
<计算机与网络应用基础知识1>模拟试卷 (4页) 本资源提供全文预览,点击全文预览即可全文预览,如果喜欢文档就下载吧,查找使用更方便哦! 9.9 积分 <计算机与网络应用基础知识1& ...
- python向量计算库教程_NumPy库入门教程:基础知识总结
原标题:NumPy库入门教程:基础知识总结 视学算法 | 作者 知乎专栏 | 来源 numpy可以说是 Python运用于人工智能和科学计算的一个重要基础,近段时间恰好学习了numpy,pandas, ...
- python常用变量名_python基础知识整理
Python Python开发 Python语言 python基础知识整理 序言:本文简单介绍python基础知识的一些重要知识点,用于总结复习,每个知识点的具体用法会在后面的博客中一一补充程序: 一 ...
最新文章
- 怎么快速把语音转成文字
- linux C 中的volatile使用
- 深度神经网络:WX+b vs XW+b
- Python语言学习之时间那些事:python和时间的使用方法之详细攻略
- 实用垃圾收集,第1部分–简介
- java 结构体数组初始化_C数组结构体联合体快速初始化
- php判断与网站访问时间,织梦CMS模板用php判断某个时间段显示和隐藏内容
- 音乐语音识别,one speech recognition,音标,读音,翻译,英文例句,英语词典
- LINUX下载编译python
- Chrome(谷歌浏览器)插件资料 !
- PLSQL导出表结构SQL
- 转:飞思卡尔单片机RAM与flash相关问题
- 扁平化设计(Flat Design)
- 转载 Android端调用Caffe模型实现CNN分类
- 【公益译文】网络威胁信息共享指南
- 零基础成为网络工程师经验分享,附完整学习路线
- 微信运动小红心点赞脚本
- 【转载】手机快充的核心模块:ChargePump
- 实战PyQt5: 086-图元类QGraphicsItem
- pyecharts制作3d地图加柱状图