判断两个圆的圆心距与其半径和,当两个圆的圆心距小于两圆的半径和时,表示发生了碰撞,反之则没有
void onTouchesBegan(const
std::vector<Touch*>&
touches,
Event *event);
void onTouchesMoved(const std::vector<Touch*>& touches, Event *event);
void onTouchesEnded(const std::vector<Touch*>& touches, Event *event);
private:
Size size;
bool isCollision(Sprite *sp);//检测两个矩形是否碰撞
void ChangeLabelContent(bool isCollision);//改变文本内容
//
void onTouchesMoved(const std::vector<Touch*>& touches, Event *event);
void onTouchesEnded(const std::vector<Touch*>& touches, Event *event);
private:
Size size;
bool isCollision(Sprite *sp);//检测两个矩形是否碰撞
void ChangeLabelContent(bool isCollision);//改变文本内容
//
bool isCircleCollision(Point
pos1,float
radius1,Point
pos2,float radius2);
size
=
Director::getInstance()->getWinSize();
auto sp1 = Sprite::create("r1.png");
sp1->setPosition(size/2);
//Sprite::addChild(Node *child, int zOrder, int tag)
addChild(sp1,0,1);
auto sp2 = Sprite::create("r2.png");
sp2->setPosition(Vec2(size.width*0.8, size.height/2));
addChild(sp2,0,2);
//添加显示碰撞信息
auto label = Label::createWithTTF("no collision", "fonts/Marker Felt.ttf", 25);
label->setPosition(Vec2(size.width/2, size.height*0.8));
addChild(label,1,3);
auto listener = EventListenerTouchAllAtOnce::create();
listener->onTouchesBegan = CC_CALLBACK_2(HelloWorld::onTouchesBegan, this);
listener->onTouchesMoved = CC_CALLBACK_2(HelloWorld::onTouchesMoved, this);
listener->onTouchesEnded = CC_CALLBACK_2(HelloWorld::onTouchesEnded, this);
Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener, this);
return true;
}
void HelloWorld::onTouchesBegan(const std::vector<Touch*>& touches, Event *event){
auto touch = static_cast<Touch*>(touches[0]);
auto point = touch->getLocation();
auto sp1 = (Sprite*)this->getChildByTag(1);
sp1->setPosition(point);
ChangeLabelContent(isCollision(sp1));
}
void HelloWorld::onTouchesMoved(const std::vector<Touch*>& touches, Event *event){
auto touch = static_cast<Touch*>(touches[0]);
auto point = touch->getLocation();
auto sp1 = (Sprite*)this->getChildByTag(1);
sp1->setPosition(point);
ChangeLabelContent(isCollision(sp1));
}
void HelloWorld::onTouchesEnded(const std::vector<Touch*>& touches, Event *event){
}
bool HelloWorld::isCollision(Sprite *sp){
bool flag = false;
auto sp2 = (Sprite*)this->getChildByTag(2);
// 重点
flag = isCircleCollision(sp->getPosition(), sp->getContentSize().width/2, sp2->getPosition(), sp2->getContentSize().width/2);
return flag;
}
void HelloWorld::ChangeLabelContent(bool isCollision){
auto label = (Label*)this->getChildByTag(3);
if (isCollision) {
label->setString("Collision!");
auto sp1 = Sprite::create("r1.png");
sp1->setPosition(size/2);
//Sprite::addChild(Node *child, int zOrder, int tag)
addChild(sp1,0,1);
auto sp2 = Sprite::create("r2.png");
sp2->setPosition(Vec2(size.width*0.8, size.height/2));
addChild(sp2,0,2);
//添加显示碰撞信息
auto label = Label::createWithTTF("no collision", "fonts/Marker Felt.ttf", 25);
label->setPosition(Vec2(size.width/2, size.height*0.8));
addChild(label,1,3);
auto listener = EventListenerTouchAllAtOnce::create();
listener->onTouchesBegan = CC_CALLBACK_2(HelloWorld::onTouchesBegan, this);
listener->onTouchesMoved = CC_CALLBACK_2(HelloWorld::onTouchesMoved, this);
listener->onTouchesEnded = CC_CALLBACK_2(HelloWorld::onTouchesEnded, this);
Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener, this);
return true;
}
void HelloWorld::onTouchesBegan(const std::vector<Touch*>& touches, Event *event){
auto touch = static_cast<Touch*>(touches[0]);
auto point = touch->getLocation();
auto sp1 = (Sprite*)this->getChildByTag(1);
sp1->setPosition(point);
ChangeLabelContent(isCollision(sp1));
}
void HelloWorld::onTouchesMoved(const std::vector<Touch*>& touches, Event *event){
auto touch = static_cast<Touch*>(touches[0]);
auto point = touch->getLocation();
auto sp1 = (Sprite*)this->getChildByTag(1);
sp1->setPosition(point);
ChangeLabelContent(isCollision(sp1));
}
void HelloWorld::onTouchesEnded(const std::vector<Touch*>& touches, Event *event){
}
bool HelloWorld::isCollision(Sprite *sp){
bool flag = false;
auto sp2 = (Sprite*)this->getChildByTag(2);
// 重点
flag = isCircleCollision(sp->getPosition(), sp->getContentSize().width/2, sp2->getPosition(), sp2->getContentSize().width/2);
return flag;
}
void HelloWorld::ChangeLabelContent(bool isCollision){
auto label = (Label*)this->getChildByTag(3);
if (isCollision) {
label->setString("Collision!");
}else
label->setString("no collision!");
}
bool HelloWorld::isCircleCollision(Point pos1,float radius1,Point pos2,float radius2){
// 重点
if (pos1.getDistance(pos2)>radius1+radius2) {
return false;
}
return true;
}
bool HelloWorld::isCircleCollision(Point pos1,float radius1,Point pos2,float radius2){
// 重点
if (pos1.getDistance(pos2)>radius1+radius2) {
return false;
}
return true;
}