convertToNodeSpace:把世界坐标转换到当前节点的本地坐标系中。
- voidHelloWorld::myInit14()
- {
- CCSizesize=CCDirector::sharedDirector()->getWinSize();
- CCSprite*back=CCSprite::create("ui_miss_butt_bj2song.png");
- back->setPosition(ccp(size.width*0.5,size.height*0.5));
- this->addChild(back,1,123);
- CCSprite*buttonSP=CCSprite::create("team_ok_butt.png");
- buttonSP->setPosition(ccp(back->getContentSize().width*0.8,back->getContentSize().height*0.5));
- back->addChild(buttonSP,321);
- }
- boolHelloWorld::ccTouchBegan(CCTouch*pTouch,CCEvent*pEvent)
- {
- beganPoint=pTouch->getLocation();
- returntrue;
- }
- voidHelloWorld::ccTouchEnded(CCTouch*pTouch,CCEvent*pEvent)
- {
- CCPointendPoint=pTouch->getLocation();
- CCSprite*back=(CCSprite*)this->getChildByTag(123);
- CCSprite*buttonSP=(CCSprite*)back->getChildByTag(321);
- CCPointnodeBeganPoint=back->convertToNodeSpace(beganPoint);
- CCPointnodeEndPoint=back->convertToNodeSpace(endPoint);
- if(buttonSP->boundingBox().containsPoint(nodeBeganPoint)&&buttonSP->boundingBox().containsPoint(nodeEndPoint)){
- CCLog("被点到了");
- }
- }
convertToWorldSpace:把基于当前节点的本地坐标系下的坐标转换到世界坐标系中。
- voidHelloWorld::myInit13()
- {
- CCSizesize=CCDirector::sharedDirector()->getWinSize();
- CCSprite*sp1=CCSprite::create("Icon.png");
- sp1->setPosition(ccp(size.width*0.5,size.height*0.5));
- this->addChild(sp1);
- CCSprite*sp2=CCSprite::create("Icon.png");
- sp2->setColor(ccYELLOW);
- sp2->setPosition(ccp(sp1->getContentSize().width,sp1->getContentSize().height));
- sp1->addChild(sp2);
- CCSprite*sp3=CCSprite::create("Icon.png");
- sp3->setColor(ccBLUE);
- sp3->setPosition(ccp(sp2->getContentSize().width,sp2->getContentSize().height));
- sp2->addChild(sp3);
- CCPointworldPoint=sp3->convertToWorldSpace(sp3->getAnchorPointInPoints());
- CCLog("worldX==%f,worldY==%f",worldPoint.x,worldPoint.y);
- CCSprite*sp4=CCSprite::create("Icon.png");
- sp4->setColor(ccGREEN);
- sp4->setPosition(worldPoint);
- this->addChild(sp4);
- }
原文链接:https://www.f2er.com/cocos2dx/341371.html