cocos2dx3.X项目重写(四)添加地板,障碍物和碰撞检测
发布时间:2020-12-14 16:57:14 所属栏目:百科 来源:网络整理
导读:我暂时不想使用地图,我想如果用精灵直接制造成方块也是可以的。代码如下,这个是添加地板 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,
我暂时不想使用地图,我想如果用精灵直接制造成方块也是可以的。代码如下,这个是添加地板 我并不知道Rect的前两个参数是干什么的,源代码写的是x和y,好像是坐标的意思,但是我设置了不同的值效果是一样的..
顺便提一句,血的教训,在继承自父类之后,在init()里要先执行一下父类的init。
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 style="white-space:pre"> </span>{ <span style="white-space:pre"> </span> removeFromParent(); <span style="white-space:pre"> </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" class="cpp" style="box-sizing: border-box; font-family: Arial,245);"> 由于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);然后就可以添加死掉的动作在拉姆达表达式中。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |