cocos 物理碰撞和触发事件

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

USING_NS_CC;

using namespace cocostudio::timeline;

Scene* HelloWorld::createScene()
{
	auto scene = Scene::createWithPhysics();
    
	scene->getPhysicsWorld()->setDebugDrawMask(PhysicsWorld::DEBUGDRAW_ALL);//开启线框调试模式

	scene->getPhysicsWorld()->setAutoStep(false);

    auto layer = HelloWorld::create();

	PhysicsBody* body = PhysicsBody::createEdgeBox(Director::getInstance()->getWinSizeInPixels()*0.9);

	body->setCategoryBitmask(0x3);

	body->setCollisionBitmask(0x3);

	body->setContactTestBitmask(0x3);
	//Todo 2、body
	layer->setPhysicsBody(body);

    scene->addChild(layer);

    return scene;
}
bool HelloWorld::init()
{
	scheduleUpdate();
    if ( !Layer::init() )
    {
        return false;
    }
	setTouchEnabled(true);

	setTouchMode(Touch::DispatchMode::ONE_BY_ONE);

    rootNode = CSLoader::createNode("MainScene.csb");

	sprite = rootNode->getChildByName<Sprite*>("Sprite_1");

	PhysicsBody* body = PhysicsBody::createBox(sprite->getContentSize());

	PhysicsBody* bodyCircle = PhysicsBody::createCircle(sprite->getContentSize().width/2);


	//掩码 100001000 A
	//     000001000 B
	//按位与 >0 A拥有B的特征
	bodyCircle->setCategoryBitmask(0x2);//Category类别掩码

	bodyCircle->setCollisionBitmask(0x2);//Collision碰撞掩码

	bodyCircle->setContactTestBitmask(0x1);//ContactTest连接掩码,触碰触发事件需要设置
	//Todo 1、bodyCircle

	bodyCircle->setDynamic(true);//true使用物理模拟,false反物理模拟,反运动运动学

	bodyCircle->setGravityEnable(true);//重力

	bodyCircle->setName("bird");

	sprite->setPhysicsBody(bodyCircle);

	sprite->getPhysicsBody()->setMass(0.5f);

	//碰撞检测事件 全局检测 
	 physicsContact = EventListenerPhysicsContact::create();//物理连接事件

	physicsContact->onContactBegin = CC_CALLBACK_1(HelloWorld::onCollsionEnter,this);

	getEventDispatcher()->addEventListenerWithSceneGraphPriority(physicsContact, rootNode);

    addChild(rootNode);

    return true;
}

void HelloWorld::update(float dt)
{
	for (int i = 0; i < 3; i++)
	{
		this->getScene()->getPhysicsWorld()->step(1 / 180.0f);
	}
}

bool HelloWorld::onTouchBegan(Touch * touch, Event * unused_event)
{
	//克隆
	Sprite* clone = Sprite::createWithTexture(sprite->getTexture());

	PhysicsBody* bodyClone = PhysicsBody::createBox(clone->getContentSize());

	bodyClone->setCategoryBitmask(0x1);

	bodyClone->setCollisionBitmask(0x1);

	bodyClone->setContactTestBitmask(0x2);

	bodyClone->setMass(sprite->getPhysicsBody()->getMass());

	bodyClone->setDynamic(true);

	bodyClone->setGravityEnable(true);

	bodyClone->setName("clone");

	//body->setCategoryBitmask(6);

	//body->setCollisionBitmask(3);

	//body->setContactTestBitmask(1);

	clone->setPhysicsBody(bodyClone);

	clone->setPosition(touch->getLocation());

	this->addChild(clone);

	return true;
}

bool HelloWorld::onContactBegin(PhysicsContact & contact)
{
	return true;
}

bool HelloWorld::onCollsionEnter(PhysicsContact & contact)
{
	//碰撞物体类型
	std :: string bodyA = contact.getShapeA()->getBody()->getName();

	std :: string bodyB  = contact.getShapeB()->getBody()->getName();

	if (bodyA == "bird")
	{
		contact.getShapeA()->getBody()->getOwner()->setVisible(false);

		contact.getShapeA()->getBody()->setEnabled(false);//关闭刚体
	}

	else if (bodyB == "clone")
	{
		contact.getShapeB()->getBody()->getOwner()->setVisible(false);

		contact.getShapeB()->getBody()->setEnabled(false);//关闭刚体
	}
	return true;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
CocosCreator实现的 解救人质 游戏,学会碰撞检测rescue.7z // Bullet.js cc.Class({ extends: cc.Component, properties: { mSpeed: 300, }, // LIFE-CYCLE CALLBACKS: // onLoad () {}, start() { var manager = cc.director.getCollisionManager(); // 获取碰撞检测系统 manager.enabled = true; }, update(dt) { // 设置子弹移动,当超出屏幕范围未发生碰撞时自动销毁 this.node.y += this.mSpeed * dt; if (this.node.y > 580) { console.log('超出屏幕范围,子弹销毁!'); this.node.destroy(); } }, /** * 当碰撞产生的时候调用 * @param {Collider} other 产生碰撞的另一个碰撞组件 * @param {Collider} self 产生碰撞的自身的碰撞组件 */ onCollisionEnter: function (other, self) { console.log('on collision enter'); if (other.tag == 1) { // 子弹碰到人质时,解救失败! console.log('解救人质失败!'); var failLabel = this.node.parent.getChildByName('failLabel'); failLabel.active = true; this.node.destroy(); } else if (other.tag == 2) { // 子弹碰到敌人时,解救成功! console.log('解救人质成功!'); var successLabel = this.node.parent.getChildByName('successLabel'); successLabel.active = true; this.node.destroy(); } }, /** * 当碰撞产生后,碰撞结束前的情况下,每次计算碰撞结果后调用 * @param {Collider} other 产生碰撞的另一个碰撞组件 * @param {Collider} self 产生碰撞的自身的碰撞组件 */ onCollisionStay: function (other, self) { console.log('on collision stay'); }, /** * 当碰撞结束后调用 * @param {Collider} other 产生碰撞的另一个碰撞组件 * @param {Collider} self 产生碰撞的自身的碰撞组件 */ onCollisionExit: function (other, self) { console.log('on collision exit'); } });

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值