cocos2d-x 判断node是不是精灵
1个回答
展开全部
//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;
}
转载,仅供参考。
"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;
}
转载,仅供参考。
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询