cocos2d-X CCsprite点击问题

cocos2d-x CCLayer上精灵的点击判断的问题

今天发现的问题,记录下,对cocos2d坐标转换的理解还不透彻,看来有必要去学习下OpenGL的基础知识了。

//使用的2dx是老版本.

1. 平时在CCLayer上放置的CCSprite,判断是否被点击到的,我一般这样做:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
//on "init" you need to initialize your instance
bool  HelloWorld::init()
{
     bool  bRet = false ;
     do
     {
         CC_BREAK_IF(! CCLayer::init());
         
         //Get window size.
         CCSize size = CCDirector::sharedDirector()->getWinSize();
         
         //pSprite is a CCSprite for test.
         pSprite = CCSprite::spriteWithFile( "fjut.png" );
         CC_BREAK_IF(! pSprite);
         
         //Place the sprite on the center of the screen 
         pSprite->setPosition(ccp(size.width/2, size.height/2));
         
         this ->addChild(pSprite, 0);
         bRet = true ;
     } while  (0);
     
     this ->setIsTouchEnabled( true );
     return  bRet;
}
void  HelloWorld::registerWithTouchDispatcher()
{
     CCTouchDispatcher::sharedDispatcher()->addTargetedDelegate( this , -1988, true );
}
 
static  CCRect getRect(CCNode* pNode)
{
     CCRect rc;
     rc.origin = pNode->getPosition();
     rc.size = pNode->getContentSize();
     rc.origin.x -= rc.size.width*0.5;
     rc.origin.y -= rc.size.height*0.5;
     return  rc;
}
 
void  HelloWorld::ccTouchEnded(CCTouch* pTouch, CCEvent* event)
{
     CCPoint touchLocation = convertTouchToNodeSpace(pTouch);
     if (CCRect::CCRectContainsPoint(getRect(pSprite), touchLocation))
     {
         printf ( "我被点中了!\n" );
     }
}
 
bool  HelloWorld::ccTouchBegan(CCTouch* touch, CCEvent* event)
{
     return  true ;
}

2. 要是在CCLayer1上放置CCLayer2(有偏移),再向CCLayer2上放置CCSprite的点击判断需要计算偏移量:

eg:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//on "init" you need to initialize your instance
bool  HelloWorld::init()
{
     bool  bRet = false ;
     do
     {
         CC_BREAK_IF(! CCLayer::init());
         bRet = true ;
     } while  (0);
     
     this ->setIsTouchEnabled( true );
     
     this ->testLayer();
     
     return  bRet;
}
 
void  HelloWorld::testLayer()
{
     CCLayer* ly = CCLayer::node();
     /*x, y轴各偏移100*/
     ly->setPosition(ccp(100, 100));
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
     //layer2 add to layer1
     this ->addChild(ly);
 
     pSprite = CCSprite::spriteWithFile( "fjut.png" );
     pSprite->setPosition(ccp(20, 20));
     ly->addChild(pSprite, 0);
}
 
void  HelloWorld::registerWithTouchDispatcher()
{
     CCTouchDispatcher::sharedDispatcher()->addTargetedDelegate( this , -1988, true );
}
 
static  CCRect getRect(CCNode* pNode)
{
     CCRect rc;
     rc.origin = pNode->getPosition();
     rc.size = pNode->getContentSize();
     /*x, y轴各偏移100*/
     rc.origin.x -= rc.size.width*0.5 - 100; //!!!!
     rc.origin.y -= rc.size.height*0.5 - 100; //!!!!
     return  rc;
}
 
void  HelloWorld::ccTouchEnded(CCTouch* pTouch, CCEvent* event)
{
     CCPoint touchLocation = convertTouchToNodeSpace(pTouch);
     if (CCRect::CCRectContainsPoint(getRect(pSprite), touchLocation))
     {
         printf ( "我被点中了!\n" );
     }
}
 
bool  HelloWorld::ccTouchBegan(CCTouch* touch, CCEvent* event)
{
     return  true ;
}
毛毛虫原创,转载请注明出处:http://www.cnblogs.com/fjut/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值