碰撞检测

#include "HelloWorldScene.h"
#include "cocostudio/CocoStudio.h"
#include "ui/CocosGUI.h"




USING_NS_CC;


using namespace cocostudio::timeline;
ui::Text *pText;
Sprite *spt1, *spt2;
Scene* HelloWorld::createScene()
{
    // 'scene' is an autorelease object
    auto scene = Scene::create();
    
    // 'layer' is an autorelease object
    auto layer = HelloWorld::create();


    // add layer as a child to scene
    scene->addChild(layer);


    // return the scene
    return scene;
}




bool HelloWorld::init()
{
if (!Layer::init())
{
return false;
}
Size visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin();


auto sprite = Sprite::create("HelloWorld.png");
sprite->setPosition(Vec2(visibleSize.width / 2 + origin.x, visibleSize.height / 2 + origin.y));
this->addChild(sprite, 1);


spt1 = Sprite::create("red.png");
spt1->setPosition(Vec2(visibleSize.width / 2 + origin.x, visibleSize.height / 2 + origin.y));
this->addChild(spt1, 2);


spt2 = Sprite::create("green.png");
spt2->setPosition(Vec2(visibleSize.width / 2 + origin.x, visibleSize.height*0.8 + origin.y));
this->addChild(spt2, 2);


pText = ui::Text::create("NO TOUCH", "marry.ttf", 40);
pText->setPosition(Vec2(200, 600)); //设置字的位置
pText->setColor(Color3B::RED);
this->addChild(pText, 2);


auto listener1 = EventListenerTouchOneByOne::create(); //创建一个多点触控的监听事件
listener1->onTouchBegan = CC_CALLBACK_2(HelloWorld::onTouchBegan, this);
listener1->onTouchMoved = CC_CALLBACK_2(HelloWorld::onTouchMoved, this);//设置事件回调函数
//_eventDispatcher->addEventListenerWithSceneGraphPriority(listener1->clone(), this);//以场景图方式注册
//_eventDispatcher->addEventListenerWithSceneGraphPriority(listener1->clone(), this);


auto mouselistener = EventListenerMouse::create();
mouselistener->onMouseMove = [](Event *event)
{
Size visibleSize = Director::getInstance()->getVisibleSize();
//Vec2 origin = Director::getInstance()->getVisibleOrigin();
EventMouse *p;
p = dynamic_cast<EventMouse *>(event);
Vec2 pos = p->getLocation();
pos.y = visibleSize.width-pos.y-visibleSize.width/2+spt2->getContentSize().width/**/;
spt2->setPosition(pos);


if (spt2->boundingBox().intersectsRect(spt1->boundingBox()))//矩形碰撞检测
pText->setString("HAVE TOUCHED!!");
else
pText->setString("NO TOUCH");
};
_eventDispatcher->addEventListenerWithSceneGraphPriority(mouselistener, this);


if (!Layer::init())
{
return false;
}




return true;
}


void HelloWorld::onTouchMoved(cocos2d::Touch* touch, cocos2d::Event* evt)
{
Vec2 pos = touch->getLocation();
spt2->setPosition(pos);
if (spt2->boundingBox().intersectsRect(spt1->boundingBox()))//矩形碰撞检测
pText->setString("HAVE TOUCHED!!");
else
pText->setString("NO TOUCH");
/*圆形碰撞检测
if (isCircleTouched(spt1->getPosition(), spt1->getContentSize().width / (float)2.0,
spt2->getPosition(), spt2->getContentSize().width /(float) 2.0))
{
pText->setString("HAVE TOUCHED!!");
}
else
pText->setString("NO TOUCH");*/
}
bool HelloWorld::onTouchBegan(cocos2d::Touch* touch, cocos2d::Event* evt)
{
Vec2 pos = touch->getLocation();
spt2->setPosition(pos);
return true;
}
bool HelloWorld::isCircleTouched(cocos2d::Vec2 v1, float r1,cocos2d:: Vec2 v2, float r2)
{
if (sqrt((v1.x - v2.x)*(v1.x - v2.x) + (v1.y - v2.y)*(v1.y - v2.y))> r1 + r2)
return false;
else
return true;
}
void HelloWorld::menuCloseCallback(Ref* pSender)
{
Director::getInstance()->end();


#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
exit(0);
#endif
}

你可能感兴趣的:(碰撞检测)