当前位置:编程学习 > wap >>

[Cocos2D-X ]初窥门径(8)判断精灵点击

在场景中添加几个精灵


[cpp]
mSprite1=CCSprite::create("Sprite.png"); 
mSprite1->setPosition(ccp(150,mSize.height/2)); 
this->addChild(mSprite1); 
mSprite2=CCSprite::create("Sprite.png"); 
mSprite2->setPosition(ccp(300,mSize.height/2)); 
this->addChild(mSprite2); 

 mSprite1=CCSprite::create("Sprite.png");
 mSprite1->setPosition(ccp(150,mSize.height/2));
 this->addChild(mSprite1);
 mSprite2=CCSprite::create("Sprite.png");
 mSprite2->setPosition(ccp(300,mSize.height/2));
 this->addChild(mSprite2);

 

 \


判断点击的点是否在精灵自身的范围内,也就是判断是否点击了精灵:


[cpp]
void TouchScene::ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent)   
{   
    CCPoint touchPoint=convertTouchToNodeSpace(pTouch); 
    if(mSprite1->boundingBox().containsPoint(touchPoint)) 
    { 
        CCMessageBox("mSprite1 touched",""); 
    } 
    if(mSprite2->boundingBox().containsPoint(touchPoint)) 
    { 
        CCMessageBox("mSprite2 touched",""); 
    } 
}  

void TouchScene::ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent) 

 CCPoint touchPoint=convertTouchToNodeSpace(pTouch);
 if(mSprite1->boundingBox().containsPoint(touchPoint))
 {
  CCMessageBox("mSprite1 touched","");
 }
 if(mSprite2->boundingBox().containsPoint(touchPoint))
 {
  CCMessageBox("mSprite2 touched","");
 }
}

 

 \

 

 

 

 

 

 

补充:移动开发 , 其他 ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,