cocos2dx3.X项目重写(四)添加地板,障碍物和碰撞检测

cocos2dx3.X项目重写(四)添加地板,障碍物和碰撞检测,第1张

概述我暂时不想使用地图,我想如果用精灵直接制造成方块也是可以的。代码如下,这个是添加地板 void Stage::addGround(){ auto g=Sprite::create(); g->setTextureRect(Rect(0,0,visible.width,15)); g->setColor(Color3B(100,100,100)); g->setPosition(visib

我暂时不想使用地图,我想如果用精灵直接制造成方块也是可以的。代码如下,这个是添加地板

voID Stage::addGround(){	auto g=Sprite::create();	g->setTextureRect(Rect(0,visible.wIDth,15));	g->setcolor(color3B(100,100,100));	g->setposition(visible.wIDth/2,world_y-8.5);	this->addChild(g);}
我并不知道Rect的前两个参数是干什么的,源代码写的是x和y,好像是坐标的意思,但是我设置了不同的值效果是一样的..

顺便提一句,血的教训,在继承自父类之后,在init()里要先执行一下父类的init。
写一个block类

bool myblock::init(){	Sprite::init();	speed = 5;	auto visible = Director::getInstance()->getVisibleSize();	/*Size s =(Size(rand()%20+5,rand()%20+5));*/	s =Size(rand()%25+10,rand()%35+5);	this->setPhysicsBody(PhysicsBody::createBox(s)); 	this->setTextureRect(Rect(0,s.wIDth,s.height)); 	this->setcolor(color3B(100,100)); 	this->setpositionX(visible.wIDth-s.wIDth/2); 	this->schedule(schedule_selector(myblock::block_run));	return true;}voID myblock::block_run(float f){	this->setpositionX(this->getposition().x-speed);	if (this->getpositionX()<0)<span >	</span>{<span >	</span>   removeFromParent();<span >	</span>}}
加了定时器,让方块一直往左走,当block离开屏幕就移除。

然后在Stage中加定时器,隔一段时间出现一个方块,但是又不能在计时器函数参数后面加一个rand()做时间,因为这是个伪随机数,后来在网上找到了一个很聪明的办法。

让数a=0,让b取一个制定范围的随机数,在updat里让a++,当a>b就生成一个障碍,并且让a=0,b再取一个随机数。如此循环。

voID Stage::restar(){	a=0;	b=rand()%120+60;}voID Stage::addblock(float f){	    a++;		if(a>=b)		{			auto b=myblock::create();			this->addChild(b);			b->setpositionY(world_y+b->s.height/2);			b->getPhysicsBody()->setDynamic(false);			restar();		}}
</pre><pre name="code"  >

由于block得添加两种,一种地上的,一种浮空的,只能趴着过的。两者必须随机添加。我的方法如下。

myblock.h

static myblock* create(Size s);

myblock.cpp

myblock* myblock::create(Size s){		myblock* bl = new myblock();	//Sprite::init();	bl->init();	bl->speed = 10;	auto visible = Director::getInstance()->getVisibleSize();	/*Size s =(Size(rand()%20+5,rand()%20+5));*/	/*i =Size(rand()%35+20,rand()%45+15);*/	bl->setPhysicsBody(PhysicsBody::createBox(s));	bl->setTextureRect(Rect(0,s.height));	bl->setcolor(color3B(100,100));	bl->setpositionX(visible.wIDth-s.wIDth/2+300);	bl->schedule(schedule_selector(myblock::block_run));	bl->getPhysicsBody()->setContactTestBitmask(1);		return bl;}

Stage.cpp

voID Stage::addblock(float f){	    a++;		if(a>=b)		{			if (b>90)			{				Size s =Size(rand()%35+20,rand()%45+15);				auto bl=myblock::create(s);				this->addChild(bl);				bl->setpositionY(world_y+s.height/2);				bl->getPhysicsBody()->setDynamic(false);				restar();			}			else			{				Size s =Size(rand()%75+35,rand()%25+10);				auto bl=myblock::create(s);				this->addChild(bl);				bl->setpositionY(world_y+s.height/2+100);				bl->getPhysicsBody()->setDynamic(false);				restar();			}		}}

测试碰撞,先添加Listener,这个物理引擎有自己的碰撞监听。

auto Listen = EventListenerPhysicsContact::create();	Listen->onContactBegin=[&](PhysicsContact& contact)							{								log("<<<<<<<<<<<<<<");								return true;							};	Director::getInstance()->getEventdispatcher()->addEventListenerWithSceneGraPHPriority(Listen,this);
然后给主角和block类各填一句话,打开碰撞检测开关,都设置成相同的数,两者就可以碰撞检测

->getPhysicsBody()->setContactTestBitmask(1);
然后就可以添加死掉的动作在拉姆达表达式中。 总结

以上是内存溢出为你收集整理的cocos2dx3.X项目重写(四)添加地板,障碍物碰撞检测全部内容,希望文章能够帮你解决cocos2dx3.X项目重写(四)添加地板,障碍物和碰撞检测所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1078373.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存