#include "Coordinate.h" #include "AppMacros.h" CCScene * Coordinate::scene() { scene = CCScene::create(); Coordinate * layer = create(); scene->addChild(layer); return scene; } bool init() { CCLayer::init(); //打开触摸开关 setTouchEnabled(true); //下面的kCCTouchesOneByOne是一个枚举: //typedef enum { // kCCTouchesAllAtOnce, // kCCTouchesOneByOne, //} ccTouchesMode; setTouchMode(kCCTouchesOneByOne); //创建一个精灵 CCSprite *big = CCSprite::create(); big->setColor(ccRED); //设置锚点 big->setAnchorPoint(ccp(0,0)); //表示的是一个矩形,CCRectMake是一个宏 big->setTextureRect(CCRectMake(0,150,150)); big->setPosition(ccp(100,100)); addChild(big); CCSprite *little = create(); little->setColor(ccYELLOW); little-> little-> little-> //从下面可以知道一个精灵中可以添加另外一个精灵 big->addChild(little); CCLog("little x = %f,y = %f",little->getPositionX(),0); font-family:新宋体; font-size:9.5pt">getPositionY()); CCPoint toWorld = big->convertToWorldSpace(little->getPosition()); "toWorld x = %f,y=%f",toWorld.x,toWorld.y); CCSprite *little2 = create(); little2->setColor(ccGREEN); little2-> little2-> little2-> addChild(little2); CCPoint toNode = big->convertToNodeSpace(little2->"little2 x = %f,little2->getPositionY()); "toNode x = %f,toNode.x,toNode.y); CCMoveBy *by = CCMoveBy::create(2,ccp(200,0)); CCMoveBy *by2 = (CCMoveBy *)by->reverse(); //最后一个NULL是一个哨兵 CCSequence *seq = CCSequence::create(by,by2,138); font-family:新宋体; font-size:9.5pt">NULL); big->runAction(CCRepeatForever::create(seq)); //第一个参数是:duration //第二个参数是:移动位置.表示上下移动 CCMoveBy *lby = CCMoveBy *lby2 = (CCMoveBy *)lby->reverse(); CCSequence *lseq = create(lby,lby2,138); font-family:新宋体; font-size:9.5pt">NULL); little->create(lseq)); return true; } //触摸事件开始 bool CCEvent *pEvent) { "ccTouchBegan"); //基于OpenGL的世界坐标 CCPoint pGl = pTouch->getLocation(); "GL:x = %f,pGl.x,pGl); //基于UI屏幕的坐标 CCPoint pUi = pTouch->getLocationInView(); "UI:x = %f,pUi.x,pUi.y); //将基于GL的坐标转换成为UI的屏幕坐标 CCPoint toUi = CCDirector::sharedDirector()->convertToUI(pGl); "ToUix = %f,toUi.x,toUi.y); //将屏幕坐标的转换成为本地坐标 CCPoint toGL = convertToGL(pUi); "ToGLx = %f,toGL.x,toGL.y); //转换成节点坐标 CCPoint node = this->convertToNodeSpace(pGl); "Node:x = %f,node.x,node.y); return false; } |