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


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

    void Stage::addGround()
    {
    	auto g=Sprite::create();
    	g->setTextureRect(Rect(0,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,0,s.width,s.height));
     	this->setColor(Color3B(100,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, 'Microsoft YaHei'; font-size: 13px; white-space: pre-wrap; padding: 0px; margin-top: 0px; margin-bottom: 0px; line-height: 1.42857; color: rgb(51, 51, 51); word-break: break-all; word-wrap: break-word; border: 1px solid rgb(204, 204, 204); border-radius: 4px; overflow: hidden;  1543px; background-color: rgb(245, 245, 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,0,s.width,s.height));
    	bl->setColor(Color3B(100,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);
    然后就可以添加死掉的动作在拉姆达表达式中。



  • 相关阅读:
    Echarts数据可视化grid直角坐标系(xAxis、yAxis)
    Cannot find module '@babel/plugin-proposal-class-properties'
    git 基本操作
    React报错:Laravel React-Router browserHistory 刷新子页面报错404
    php报错:Notice: iconv(): Wrong charset, conversion from `GBK' to `UTF8' is not allowed
    Larval报错:后台上传图片,storage目录也有相应的图片,但前台访问不到图片。
    Laravel报错:1071 Specified key was too long; max key length is 1000 bytes
    thinkcmf报错:fileowner(): stat failed for /sys
    vbox虚拟机复制&&虚拟机指定静态IP
    thinkcmf在万网上部署
  • 原文地址:https://www.cnblogs.com/Anzhongliu/p/6091830.html
Copyright © 2020-2023  润新知