时间:2012.3.5
平台:xcode4.0,cocos2d-x
测试坐标和触摸消息
先了解下基本概念(不足请指正):
坐标转换主要就4个函数:
CCPointconvertToNodeSpace(const CCPoint& worldPoint);
CCPointconvertToWorldSpace(const CCPoint& nodePoint);
CCPointconvertToNodeSpaceAR(const CCPoint& worldPoint);
CCPointconvertToWorldSpaceAR(const CCPoint& nodePoint);
cocos2d-x默认情况下:
世界坐标:屏幕上原点基于(0,0)的笛卡尔坐标系(此时原点为屏幕左下角),
坐标x向右增长,y向上增长. x,y都是大于等于0.
对象窗口内的坐标:窗口原点基于(0,0)的笛卡尔坐标系(此时原点为窗口左下角),
坐标x向右增长,y向上增长. x,y都是大于等于0.
基于锚点的窗口内的坐标:窗口原点基于锚点(x,y)的笛卡尔坐标系(此时原点为锚点),
坐标x向右增长,y向上增长。 x,y可以为负数或者正数以及0。
CCPointconvertToNodeSpace(const CCPoint& worldPoint): 这个函数把世界坐标转换为对象窗口内的坐标。
CCPointconvertToWorldSpace(const CCPoint& nodePoint): 这个函数把对象窗口内的坐标转换为世界坐标。
CCPointconvertToNodeSpaceAR(const CCPoint& worldPoint):这个函数把世界坐标转换为基于锚点的窗口内的坐标。
CCPointconvertToWorldSpaceAR(const CCPoint& nodePoint):这个函数把基于锚点的对象窗口内的坐标转换为世界坐标。
首先是坐标和触摸:
先上图:(用label跟踪鼠标,截图中鼠标没有显示)
更改父layer(即scrollLayer)后的偏移位置 (20,20)后如下:
// // NRScrollView.h // cocosHello // // Created by mac on 12-3-5. // Copyright 2012年 __MyCompanyName__. All rights reserved. // #ifndef NRScrollLayer_H #define NRScrollLayer_H #include "cocos2d.h" #include "stdio.h" using namespace cocos2d; class NRScrollLayer:public CCLayer { private: CCLabelTTF *labelInfo; CCLayerColor *contentLayer; public: virtual bool init(); public: // default implements are used to call script callback if exist virtual void ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent); virtual void ccTouchesMoved(CCSet *pTouches, CCEvent *pEvent); virtual void ccTouchesEnded(CCSet *pTouches, CCEvent *pEvent); virtual void ccTouchesCancelled(CCSet *pTouches, CCEvent *pEvent); LAYER_NODE_FUNC(NRScrollLayer); }; #endif//NRScrollLayer_H
// // NRScrollLayer.cpp // cocosHello // // Created by mac on 12-3-5. // Copyright 2012年 __MyCompanyName__. All rights reserved. // #include "NRScrollLayer.h" using namespace cocos2d; bool NRScrollLayer::init() { CCSize scrollLayerSize = this->getContentSize(); contentLayer = CCLayerColor::layerWithColorWidthHeight(ccc4(120,0,0,120), scrollLayerSize.width,scrollLayerSize.height); contentLayer->setIsRelativeAnchorPoint(true); contentLayer->setAnchorPoint(ccp(0.5,0.5)); contentLayer->setPosition(ccp(scrollLayerSize.width/2.0,scrollLayerSize.height/2.0)); printf("ContentLayer:width=%.0f,height=%.0f\n",scrollLayerSize.width,scrollLayerSize.height); labelInfo= CCLabelTTF::labelWithString("click and move", "Arial", 28); labelInfo->setScaleY(0.5); labelInfo->setScaleX(0.5); labelInfo->setAnchorPoint(ccp(0.5,0.5)); labelInfo->setPosition(ccp(40,40)); contentLayer->addChild(labelInfo); contentLayer->setIsTouchEnabled(true); this->addChild(contentLayer); this->setIsTouchEnabled(true); return true; } void NRScrollLayer::ccTouchesBegan( CCSet *pTouches, CCEvent *pEvent) { CCSetIterator it = pTouches->begin(); CCTouch* touch = (CCTouch*)(*it); CCPoint location = touch->locationInView( touch->view() );//based on uiview, origin based on upper left. CCPoint convertedLocation = CCDirector::sharedDirector()->convertToGL(location);//glLocation, origin(0,0) CCPoint convertedToNodeLocation = this->convertToNodeSpace(convertedLocation); //gllocation,origin based on parent's lower left. CCPoint convertedToNodeLocationAR = this->convertToNodeSpaceAR(convertedLocation); char message[200]; sprintf(message, "UI:%.0f,%.0f\nGL:%.0f,%.0f\nNL:%.0f,%.0f\nAR:%.0f,%.0f", location.x,location.y,convertedLocation.x,convertedLocation.y, convertedToNodeLocation.x,convertedToNodeLocation.y, convertedToNodeLocationAR.x,convertedToNodeLocationAR.y); labelInfo->setString(message); labelInfo->setPosition(convertedLocation); } void NRScrollLayer::ccTouchesCancelled(cocos2d::CCSet *pTouches, cocos2d::CCEvent *pEvent){ } void NRScrollLayer::ccTouchesEnded(cocos2d::CCSet *pTouches, cocos2d::CCEvent *pEvent){ CCSetIterator it = pTouches->begin(); CCTouch* touch = (CCTouch*)(*it); CCPoint location = touch->locationInView( touch->view() );//based on uiview, origin based on upper left. CCPoint convertedLocation = CCDirector::sharedDirector()->convertToGL(location);//glLocation, origin(0,0) CCPoint convertedToNodeLocation = this->convertToNodeSpace(convertedLocation); //gllocation,origin based on parent's lower left. CCPoint convertedToNodeLocationAR = this->convertToNodeSpaceAR(convertedLocation); char message[200]; sprintf(message, "UI:%.0f,%.0f\nGL:%.0f,%.0f\nNL:%.0f,%.0f\nAR:%.0f,%.0f", location.x,location.y,convertedLocation.x,convertedLocation.y, convertedToNodeLocation.x,convertedToNodeLocation.y, convertedToNodeLocationAR.x,convertedToNodeLocationAR.y); labelInfo->setString(message); labelInfo->setPosition(convertedLocation); } void NRScrollLayer::ccTouchesMoved(cocos2d::CCSet *pTouches, cocos2d::CCEvent *pEvent){ CCSetIterator it = pTouches->begin(); CCTouch* touch = (CCTouch*)(*it); CCPoint location = touch->locationInView( touch->view() );//based on uiview, origin based on upper left. CCPoint convertedLocation = CCDirector::sharedDirector()->convertToGL(location);//glLocation, origin(0,0) CCPoint convertedToNodeLocation = this->convertToNodeSpace(convertedLocation); //gllocation,origin based on parent's lower left. CCPoint convertedToNodeLocationAR = this->convertToNodeSpaceAR(convertedLocation); char message[200]; sprintf(message, "UI:%.0f,%.0f\nGL:%.0f,%.0f\nNL:%.0f,%.0f\nAR:%.0f,%.0f", location.x,location.y,convertedLocation.x,convertedLocation.y, convertedToNodeLocation.x,convertedToNodeLocation.y, convertedToNodeLocationAR.x,convertedToNodeLocationAR.y); labelInfo->setString(message); labelInfo->setPosition(convertedLocation); }
我直接在helloworld的init中添加如下:
NRScrollLayer *scrollLayer = new NRScrollLayer; scrollLayer->setContentSize(CCSizeMake(480, 320)); printf("ScrollLayer:width=%.0f,height=%.0f\n",scrollLayer->getContentSize().width,scrollLayer->getContentSize().height); scrollLayer->init(); scrollLayer->autorelease(); scrollLayer->setPosition(ccp(20,20)); this->addChild(scrollLayer,1);
然后剪裁:(调用opengl方法,很简单的介绍下)
在头文件增加 public方法:
virtualvoid visit();
然后cpp文件增加实现:
voidNRScrollLayer::visit(){
glEnable(GL_SCISSOR_TEST);
glScissor(40, 20, 400, 280);
CCLayer::visit();
glDisable(GL_SCISSOR_TEST);
}
看看效果:
ok,it is over。