一起做网站可以一件代发吗,网络培训机构排名前十,wordpress设置 vip栏目,网站建设文化流程图cocos2dx中有三种定时器#xff1a;schedule#xff0c;scheduleUpdate#xff0c;scheduleOnce。功能分别是 每隔几秒调用自定义函数、调用系统默认的update()函数、只调用一次自定义函数
1、scheduleUpdate 加入当前节点后#xff0c;程序会每帧都会自动执行一次默认的…cocos2dx中有三种定时器schedulescheduleUpdatescheduleOnce。功能分别是 每隔几秒调用自定义函数、调用系统默认的update()函数、只调用一次自定义函数
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类的setPositiongetPosition函数如果是一个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 *sceneCCScene::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 *textureCCTextureCache::sharedTextureCache()-addImage(filename);CCArray *arrayCCArray::create();for (int istart;iend;i){CCSpriteFrame *frameCCSpriteFrame::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);}