cocos2d-x学习之设置精灵的触摸范围
2014-04-04 17:46
323 查看
cocos2d-x学习之设置精灵的触摸范围
分类: coco2d-x游戏开发2012-09-2813:38 3081人阅读 评论(0) 收藏 举报
cocos2d-x游戏开发精灵的触摸
触摸事件是游戏中必不可少的一部分,但是有时候我们不想使整个精灵都可以接受触摸响应的,所以我们要设精灵的一部分响应。
同样先给个效果图
[cpp] view
plaincopyprint?
这样当我们点击两个精灵重叠的部分时就不好判断由那个精灵接收触摸事件因此我们要设置精灵的一部分接受触摸事件
同样我们先初始化两个精灵
[cpp] view
plaincopyprint?
bool HelloWorld::init()
{
//////////////////////////////
// 1. super init first
if ( !CCLayer::init() )
{
return false;
}
/////////////////////////////
// 2. add a menu item with "X" image, which is clicked to quit the program
// you may modify it.
// add a "close" icon to exit the progress. it's an autorelease object
CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
"CloseNormal.png",
"CloseSelected.png",
this,
menu_selector(HelloWorld::menuCloseCallback) );
pCloseItem->setPosition( ccp(CCDirector::sharedDirector()->getWinSize().width - 20, 20) );
// create menu, it's an autorelease object
CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);
pMenu->setPosition( CCPointZero );
this->addChild(pMenu, 1);
/////////////////////////////
// 3. add your codes below...
// add a label shows "Hello World"
// create and initialize a label
CCLabelTTF* pLabel = CCLabelTTF::create("设置精灵的触摸范围", "Thonburi", 34);
plabel1 = CCLabelTTF::create("你触摸了精灵重叠区域","Thonburi",34);
// ask director the window size
size = CCDirector::sharedDirector()->getWinSize();
// position the label on the center of the screen
pLabel->setPosition( ccp(size.width / 2, size.height - 20) );
// add the label as a child to this layer
this->addChild(pLabel, 1);
// add "HelloWorld" splash screen"
pSprite = CCSprite::create("Icon-72.png");
pSprite1 = CCSprite::create("Icon-72.png");
// position the sprite on the center of the screen
pSprite->setPosition(ccp(size.width/2, size.height/2) );
pSprite1->setPosition(ccp(size.width/2-50, size.height/2-50) );
plabel1->setPosition(ccp(size.width/2, size.height/2-120));
plabel1->setVisible(false);
// add the sprite as a child to this layer
this->addChild(pSprite, 0);
this->addChild(pSprite1,1);
this->addChild(plabel1);
this->setTouchEnabled(true);
return true;
}
接下来我们给精灵的一小部分设置为可触摸
[cpp] view
plaincopyprint?
void HelloWorld::ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent){
CCTouch *touch = (CCTouch *)pTouches->anyObject();
CCPoint beginLoc = touch->locationInView();
beginLoc = CCDirector::sharedDirector()->convertToGL(beginLoc);
CCRect rect = pSprite1->boundingBox(); //得到精灵的矩形框
int x = rect.origin.x; //得到矩形框的左下角x坐标
int y = rect.origin.y; //得到矩形框的左下角x坐标
int w = rect.size.width; //得到矩形框的宽
int h = rect.size.height; //得到矩形框的高
rect = CCRect(210, 130, 20, 20); //重新设置精灵的矩形框x坐标为210,Y坐标为130,宽w为20,高h为20
CCLog("%d==%d==%d==%d",x,y,w,h);
if(CCRect::CCRectContainsPoint(rect, beginLoc)){
plabel1->setVisible(true);
}
}
这样就可以设置我们想要的触摸区域了 不会再为精灵重叠由那个精灵响应触摸事件而烦恼了。
相关文章推荐
- cocos2d-x学习之设置精灵的触摸范围
- Cocos2d-x游戏开发之设置精灵的触摸范围
- Cocos2d设置精灵触摸范围
- cocos2d-x学习笔记——创建精灵
- 【Cocos2d-X开发学习笔记】第06期:渲染框架之精灵类(CCSprite)的使用
- Cocos2d-x精灵学习笔记
- cocos2d-x 学习笔记—缓存中创建精灵
- 游戏基础元素之精灵——Cocos2d-x学习历程(九)
- 关于Cocos2d-x中精灵节点的透明度的设置
- cocos2d-x学习笔记(4)精灵和菜单
- cocos2d-x 触摸时间设置优先级
- (转)cocos2d-x学习笔记(三)让精灵按照自己设定的运动轨迹行动(曲线移动)。(以椭圆轨迹为例)
- (转)cocos2d-x学习笔记(五)仿真树叶飘落效果的实现(精灵旋转、翻转、钟摆运动等综合运用)
- cocos2d-x基础学习--编写自己的精灵类
- cocos2d-x学习笔记——各种遍历与范围for语句的使用
- cocos2d-x学习之层的触摸消息响应
- cocos2d-x学习笔记-CCSprite(精灵)
- Cocos2d-x学习笔记(五)仿真树叶飘落效果的实现(精灵旋转、翻转、钟摆运动等综合运用)
- cocos2d-x学习之(二)有关精灵的一些秘密
- Cocos2d-x 单点触摸--让我们用手指动起来的精灵