cocos2d-x 碰撞检测 20
CCSprite*sp1=CCSprite::create("banana.png");sp1->setPosition(ccp(200,300));addChild(s...
CCSprite* sp1 = CCSprite::create("banana.png");
sp1->setPosition(ccp(200, 300));
addChild(sp1, 0, 1);
CCSprite* sp2 = CCSprite::create("streak-hd.png");
sp2->setPosition(ccp(250, 20));
addChild(sp2, 0, 2);
sp1->runAction(actionTo);
CCRect move1Rect = CCRectMake(sp1->getPosition().x,sp1->getPosition().y,
sp1->getContentSize().width,sp1->getContentSize().height);
CCRect move2Rect = CCRectMake(sp2->getPosition().x-sp2->getContentSize().width/2,sp2->getPosition().y-sp2->getContentSize().height/2,
sp2->getContentSize().width,sp2->getContentSize().height);
if ( move1Rect.intersectsRect(move2Rect) )
{
CCLog("YESSSSS!!!");
}
请问为什么检测不出来碰撞? 展开
sp1->setPosition(ccp(200, 300));
addChild(sp1, 0, 1);
CCSprite* sp2 = CCSprite::create("streak-hd.png");
sp2->setPosition(ccp(250, 20));
addChild(sp2, 0, 2);
sp1->runAction(actionTo);
CCRect move1Rect = CCRectMake(sp1->getPosition().x,sp1->getPosition().y,
sp1->getContentSize().width,sp1->getContentSize().height);
CCRect move2Rect = CCRectMake(sp2->getPosition().x-sp2->getContentSize().width/2,sp2->getPosition().y-sp2->getContentSize().height/2,
sp2->getContentSize().width,sp2->getContentSize().height);
if ( move1Rect.intersectsRect(move2Rect) )
{
CCLog("YESSSSS!!!");
}
请问为什么检测不出来碰撞? 展开
3个回答
展开全部
是否锚点没设置好?而且检测碰撞,就必须每一帧都做一次判断,除非你这不是一段代码,而是分开两段。。。
void run()
{
CCSprite* sp1 = CCSprite::create("banana.png");
sp1->setAnchorPoint(ccp(0, 0));
sp1->setPosition(ccp(200, 300));
addChild(sp1, 0, 1);
CCSprite* sp2 = CCSprite::create("streak-hd.png");
sp2->setPosition(ccp(250, 20));
sp2->->setAnchorPoint(ccp(0, 0));
addChild(sp2, 0, 2);
CCAction* actionTo = .....
sp1->runAction(actionTo);
schedule(schedule_selector(ScheduleTest::bomb));
}
void ScheduleTest::bomb(){
CCSprite* sp1 = (CCSprite*)this->getChildByTag(1);
CCSprite* sp2 = (CCSprite*)this->getChildByTag(2);
CCRect move1Rect = CCRectMake(sp1->getPosition().x,sp1->getPosition().y,
sp1->getContentSize().width,sp1->getContentSize().height);
CCRect move2Rect = CCRectMake(sp2->getPosition().x,sp2->getPosition().y,
sp2->getContentSize().width,sp2->getContentSize().height);
if ( move1Rect.intersectsRect(move2Rect) )
{
CCLog("YESSSSS!!!");
}
}
void run()
{
CCSprite* sp1 = CCSprite::create("banana.png");
sp1->setAnchorPoint(ccp(0, 0));
sp1->setPosition(ccp(200, 300));
addChild(sp1, 0, 1);
CCSprite* sp2 = CCSprite::create("streak-hd.png");
sp2->setPosition(ccp(250, 20));
sp2->->setAnchorPoint(ccp(0, 0));
addChild(sp2, 0, 2);
CCAction* actionTo = .....
sp1->runAction(actionTo);
schedule(schedule_selector(ScheduleTest::bomb));
}
void ScheduleTest::bomb(){
CCSprite* sp1 = (CCSprite*)this->getChildByTag(1);
CCSprite* sp2 = (CCSprite*)this->getChildByTag(2);
CCRect move1Rect = CCRectMake(sp1->getPosition().x,sp1->getPosition().y,
sp1->getContentSize().width,sp1->getContentSize().height);
CCRect move2Rect = CCRectMake(sp2->getPosition().x,sp2->getPosition().y,
sp2->getContentSize().width,sp2->getContentSize().height);
if ( move1Rect.intersectsRect(move2Rect) )
{
CCLog("YESSSSS!!!");
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询