Cocos2dx中零散知识点
2016-03-09 18:20
489 查看
cocos2dx中有三种定时器:schedule,scheduleUpdate,scheduleOnce。功能分别是 每隔几秒调用自定义函数、调用系统默认的update()函数、只调用一次自定义函数
1、scheduleUpdate
加入当前节点后,程序会每帧都会自动执行一次默认的Update函数。(注:一定是Update函数哦,若想调用其他自己命名的函数则使用schedule)
看例子,走起。
首先在HelloWord类的头文件中声明Update函数:
然后在HelloWorld类源文件中实现函数Update:
voidHelloWorld::Update(float dt)
{
}
现在我们可以调用了,在需要他不断执行的地方加入调用的代码就ok:
运行之后你将会看到不断有baibai被打印出来
停止方法:
this->unscheduleUpdate();
2、schedule
功能:每隔几秒执行一次函数
首先还是在HelloWorld中声明所要执行的函数:
然后在源文件实现:
void HelloWorld::Move(floatdt)
{
}
现在去执行他,注意参数哦
运行之后,baibai每隔1.0f才会被打印一次。
停止方法:
this->unschedule(schedule_selector(HelloWorld::Move));
3、scheduleOnce
功能:在几秒之后执行,并且只会执行一次。
我们就执行上面所写的Move函数吧。
this->scheduleOnce(schedule_selector(HelloWorld::Move),1.0f); //在1.0f之后执行,并且只执行一次。
运行一下,baibai只是被打印了一次就完了。。。
ok,定时器的调用已经讲完了,大家不妨自己写一些函数体验一下。
4、停止所有计时器
this->unscheduleAllSelectors()
CCNode类的setPosition,getPosition函数如果是一个Node的Child则获取的坐标就是该Node的本地坐标
另一个关键问题就是在cocos2d-x里就是各种对象的大小问题。因为在cocos2d-x里CCNode对象有缩放的方法setScaleX和setScaleY。所以在获取对象大小的时候必须根据情况明确指定获取对象原始大小,还是缩放后的大小。当然cocos2d-x里提供了对应函数来完成这些操作:
getContentSize函数来获得节点原始的大小。只是逻辑尺寸,不是像素
boundingBox函数来获得经过缩放和旋转之后的外框盒大小。
getContentSizeInPixels获得的是像素点大小
像素点和逻辑点关系:逻辑点大小 = 像素大小
getVisibleSize:默示获得视口(可视区域)的大小,若是DesignResolutionSize跟屏幕尺寸一样大,则getVisibleSize便是getWinSize。
getVisibleOrigin:默示可视区域的出发点坐标,这在处理惩罚相对地位的时辰很是有效,确保节点在不合辨别率下的地位一致。
坐标转换:
GL坐标系,cocos2d-x默认坐标系:
CCPoint CCDirector::convertToGL(const CCPoint& uiPoint)
{
CCSize s = m_obWinSizeInPoints;
float newY = s.height - uiPoint.y;
}
屏幕坐标系: 默认原点在左上角
CCPoint CCDirector::convertToUI(const CCPoint& glPoint)
{
CCSize winSize = m_obWinSizeInPoints;
float oppositeY = winSize.height - glPoint.y;
}
两种坐标的X方向没有变,只变了Y方向,cocos2d-x里默认的GL坐标系,即左下角为原点ccp(0.0f,0.0f)
1、scheduleUpdate
加入当前节点后,程序会每帧都会自动执行一次默认的Update函数。(注:一定是Update函数哦,若想调用其他自己命名的函数则使用schedule)
看例子,走起。
首先在HelloWord类的头文件中声明Update函数:
void Update(float dt); //注意参数类型
然后在HelloWorld类源文件中实现函数Update:
voidHelloWorld::Update(float dt)
{
CCLOG("baibai");
}
现在我们可以调用了,在需要他不断执行的地方加入调用的代码就ok:
this->scheduleUpdate(); //this是当前节点,如layer,所以可以省略啦。
运行之后你将会看到不断有baibai被打印出来
停止方法:
this->unscheduleUpdate();
2、schedule
功能:每隔几秒执行一次函数
首先还是在HelloWorld中声明所要执行的函数:
void Move(float dt);
然后在源文件实现:
void HelloWorld::Move(floatdt)
{
CCLOG("baibai");
}
现在去执行他,注意参数哦
this->schedule(schedule_selector(HelloWorld::Move),1.0f); //每隔1.0f执行一次,省略参数则表示每帧都要执行
运行之后,baibai每隔1.0f才会被打印一次。
停止方法:
this->unschedule(schedule_selector(HelloWorld::Move));
3、scheduleOnce
功能:在几秒之后执行,并且只会执行一次。
我们就执行上面所写的Move函数吧。
this->scheduleOnce(schedule_selector(HelloWorld::Move),1.0f); //在1.0f之后执行,并且只执行一次。
运行一下,baibai只是被打印了一次就完了。。。
ok,定时器的调用已经讲完了,大家不妨自己写一些函数体验一下。
4、停止所有计时器
this->unscheduleAllSelectors()
CCNode类的setPosition,getPosition函数如果是一个Node的Child则获取的坐标就是该Node的本地坐标
另一个关键问题就是在cocos2d-x里就是各种对象的大小问题。因为在cocos2d-x里CCNode对象有缩放的方法setScaleX和setScaleY。所以在获取对象大小的时候必须根据情况明确指定获取对象原始大小,还是缩放后的大小。当然cocos2d-x里提供了对应函数来完成这些操作:
getContentSize函数来获得节点原始的大小。只是逻辑尺寸,不是像素
boundingBox函数来获得经过缩放和旋转之后的外框盒大小。
getContentSizeInPixels获得的是像素点大小
像素点和逻辑点关系:逻辑点大小 = 像素大小
getVisibleSize:默示获得视口(可视区域)的大小,若是DesignResolutionSize跟屏幕尺寸一样大,则getVisibleSize便是getWinSize。
getVisibleOrigin:默示可视区域的出发点坐标,这在处理惩罚相对地位的时辰很是有效,确保节点在不合辨别率下的地位一致。
坐标转换:
GL坐标系,cocos2d-x默认坐标系:
CCPoint CCDirector::convertToGL(const CCPoint& uiPoint)
{
CCSize s = m_obWinSizeInPoints;
float newY = s.height - uiPoint.y;
}
屏幕坐标系: 默认原点在左上角
CCPoint CCDirector::convertToUI(const CCPoint& glPoint)
{
CCSize winSize = m_obWinSizeInPoints;
float oppositeY = winSize.height - glPoint.y;
return ccp(glPoint.x,oppositeY);
}
两种坐标的X方向没有变,只变了Y方向,cocos2d-x里默认的GL坐标系,即左下角为原点ccp(0.0f,0.0f)
// 创建精灵的五种方法 //方法一:直接创建精灵 //适合于要显示的是这张图片的全部区域, CCSprite * sprite = CCSprite::create("Icon.png"); //上面那句话也可以根据需要这样来写: //CCString* fileName = CCString::createWithFormat("Icon_%d.jpg", flag); //CCSprite* sprite = CCSprite::create(fileName->getCString()); sprite->setPosition(ccp(100, 100)); this->addChild(sprite); // 方法二:参数 图片名称 矩形区域 //适合于需要显示此图片的部分区域 CCSprite * sprite = CCSprite::create("Icon.png",CCRectMake(0, 0, 30, 30)); sprite->setPosition(ccp(100, 100)); this->addChild(sprite); //方法三: 利用帧缓存中的一帧的名称声称一个对象 // 适合于plist打包好的文件 CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("test_icon.plist"); CCSprite * sprite = CCSprite::createWithSpriteFrameName("Icon.png"); sprite->setPosition(ccp(100, 100)); this->addChild(sprite); //方法四: 利用另外一帧生成一个精灵对象 //适合于做帧动画使用 CCSpriteFrame * frame = CCSpriteFrame::create("Icon.png", CCRectMake(0, 0, 40, 30)); CCSprite * sprite = CCSprite::createWithSpriteFrame(frame); sprite->setPosition(ccp(310, 150)); addChild(sprite); //方法五:利用纹理, //适合于需要频繁使用的图片 CCSpriteBatchNode* spriteTexture = CCSpriteBatchNode::create("iocn.png"); spriteTexture->setPosition(CCPointZero); addChild(spriteTexture); CCSprite* sprite = CCSprite::createWithTexture(spriteTexture->getTexture()); sprite->setPosition(ccp(visiblesize.width/2, 100)); spriteTexture->addChild(sprite, 2);
常用的封装方法
//返回场景 static CCScene* scene(CCLayer*layer) { CCScene *scene=CCScene::create(); scene->addChild(layer); //scene->autorelease(); return scene; } //移动点 static void moveNode(CCNode *node,CCPoint point) { node->setPosition(node->getPosition()+point); } //格式化,串接字符串 static char* format(int v, const char* prefix = "", const char* suffix = "") { static char buf[2048]; sprintf(buf, "%s%d%s", prefix, v, suffix); return buf; } //创建动画 static CCAnimation* CreateAnimation(const char* filename,int start,int end,int width,float delay) { CCTexture2D *texture=CCTextureCache::sharedTextureCache()->addImage(filename); CCArray *array=CCArray::create(); for (int i=start;i<end;i++) { CCSpriteFrame *frame=CCSpriteFrame::createWithTexture(texture,CCRectMake(i*width,0,width,texture->getContentSize().height)); array->addObject(frame); } return CCAnimation::createWithSpriteFrames(array,delay); } //创建帧 static CCSpriteFrame* getSpriteFrame(const char* filename, int pos, int width) { CCTexture2D* texture = CCTextureCache::sharedTextureCache()->addImage(filename); CCSpriteFrame* frame = CCSpriteFrame::createWithTexture(texture, CCRectMake(pos*width, 0, width, texture->getContentSize().height)); return frame; } //地图坐标转格子地图 static CCPoint Point2Tile(CCTMXTiledMap* map, CCPoint ptInMap) { int dx = map->getTileSize().width; int dy = map->getTileSize().height; int x = ptInMap.x / dx; int y = ptInMap.y / dy; y = map->getMapSize().height - 1 - y; return ccp(x, y); } //格子地图坐标转地图坐标 static CCPoint Tile2PointLB(CCTMXTiledMap* map, CCPoint ptTile) { ptTile.y = map->getMapSize().height - 1 - ptTile.y; return ccp(ptTile.x * map->getTileSize().width, ptTile.y * map->getTileSize().height); }
相关文章推荐
- cocos2dx3.x 按钮 输入框
- Logcat 不显示日志的另一个原因. 跟cocos2dx关系不大.
- cocos2dx学习笔记之retain()和release()函数简介
- Android游戏包括Unity3D和Cocos2D-X退出不干净的问题
- cocos2d-x之悦动的小球
- cocos2d-js的自定义事件的两种创建方法
- cocos 中文字符显示问题
- cocos2d 场景切换特效
- cocos2dx Action 特效
- cocos2dx Action 动作
- Ubuntu下快速建立跨多个平台的cocos2d-x项目
- cocos2dx 基础
- cocos2d-x之多个移动的小球
- Cocos2d-x之lambda表达式的使用
- [Cocos2d-x v3.x]序列帧动画
- 在mac环境下,导入 quick-cocos2d-x 项目 报错
- Cocos2d-x之多分辨率屏幕适配
- 【cocos2d 3.x】粒子plist的简单使用
- cocos2d-android(1)
- php服务器与cocos2d-x客户端session通信