当前位置: 首页>>代码示例>>C++>>正文


C++ PhysicsBody::setGroup方法代码示例

本文整理汇总了C++中PhysicsBody::setGroup方法的典型用法代码示例。如果您正苦于以下问题:C++ PhysicsBody::setGroup方法的具体用法?C++ PhysicsBody::setGroup怎么用?C++ PhysicsBody::setGroup使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在PhysicsBody的用法示例。


在下文中一共展示了PhysicsBody::setGroup方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: init

bool SceneFlyGame::init()
{
	Scene::initWithPhysics();
	// 显示边框线
	//this->getPhysicsWorld()->setDebugDrawMask(PhysicsWorld::DEBUGDRAW_ALL);
	// 返回按钮
	Node * back = TBack::create();
	addChild(back);
	back->setLocalZOrder(100);
	// 创建一个鸟
	Sprite * bird = Sprite::create();
	m_bird = bird;
	bird->setContentSize(Size(16, 16));
	addChild(bird);
	bird->setPosition(winSize.width / 4, winSize.height / 2);
	bird->setScale(2, 1);

	Texture2D * texture = Director::getInstance()->getTextureCache()->addImage("flyFishRight.png");
	Vector<SpriteFrame *> frames;
	for (int i = 0; i < 6; i++)
	{
		SpriteFrame * frame = SpriteFrame::createWithTexture(texture, Rect(i * 16, 0, 16, 16));
		frames.pushBack(frame);
	}
	Animation * animation = Animation::createWithSpriteFrames(frames, .1f);
	Animate * animate = Animate::create(animation);
	bird->runAction(RepeatForever::create(animate));

	// 给鸟加一个body
	Size size = bird->getBoundingBox().size;
	Point pts[] = { 
		Point(-size.width / 2, -size.height / 2),
		//Point(-size.width / 2, size.height / 2),
		Point(-size.width / 2, 0),
		Point(0, size.height / 2),
		Point(size.width / 2, size.height / 2),
		Point(size.width / 2, -size.height / 2)
	};
	PhysicsBody * body = PhysicsBody::createPolygon(pts, 5);
	bird->setPhysicsBody(body);
	body->setGroup(1);
	body->setContactTestBitmask(1); // 碰撞标志位

	// 添加边框
	{
		PhysicsBody * body = PhysicsBody::createEdgeBox(winSize, PhysicsMaterial(1.0f, 1.0f, 1.0f));
		Sprite * sprite = Sprite::create();
		addChild(sprite);
		sprite->setPhysicsBody(body);
		sprite->setPosition(winSize.width / 2, winSize.height / 2);
		body->setContactTestBitmask(1);
	}
	// 鸟上升
	auto ev = EventListenerTouchOneByOne::create();
	ev->onTouchBegan = [&](Touch*, Event *){
		m_press = true;
		return true;
	};
	ev->onTouchEnded = [&](Touch*, Event *){
		m_press = false;
	};
	_eventDispatcher->addEventListenerWithSceneGraphPriority(ev, this);
	m_press = false;
	schedule(schedule_selector(SceneFlyGame::fly));
	
	//碰撞检测
	auto evCollision = EventListenerPhysicsContactWithGroup::create(1);

	evCollision->onContactBegin = [&](PhysicsContact & contact){
		m_dead = true;
		// 去除连续的碰撞检测
		m_bird->getPhysicsBody()->setGroup(0);
		return true;
	};
	m_dead = false;
	_eventDispatcher->addEventListenerWithSceneGraphPriority(evCollision, this);

	schedule(schedule_selector(SceneFlyGame::addBlock), 0.5f);
	schedule(schedule_selector(SceneFlyGame::moveBlock));
	return true;
}
开发者ID:hcy12321,项目名称:cocos2dx3xTest,代码行数:81,代码来源:SceneFlyGame.cpp


注:本文中的PhysicsBody::setGroup方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。