zoukankan      html  css  js  c++  java
  • cocos2d-x 3.0 使用最新物理引擎的一个源代码实例

    1.碰撞函数參数由两个变成一个了

    2.检測不到碰撞。须要设置那三个參数。同一时候还要设置成动态的。

    body进行设置。

    3.初始入口文件也发生了改变。

    附录上我近期调试好的cocos2d-x 3.1 使用最新物理引擎的一个源代码实例,是一个小猫碰撞小车的样例。最新的引擎都可用。

    资源文件和代码下载:http://download.csdn.net/detail/u014734779/7417009

    部分代码參考:

    Scene* HelloWorld::createScene()
    {
        // 'scene' is an autorelease object
    	auto scene = Scene::createWithPhysics();
    
    	scene->getPhysicsWorld()->setDebugDrawMask(PhysicsWorld::DEBUGDRAW_ALL);
    	Vect gravity = Vect(0.0f, 0.0f);
    	scene->getPhysicsWorld()->setGravity(gravity);
    
        
        // 'layer' is an autorelease object
        auto layer = HelloWorld::create();
    
    	layer->setPhyWorld(scene->getPhysicsWorld());
    
        // add layer as a child to scene
        scene->addChild(layer);
    
        // return the scene
        return scene;
    }
    
    // on "init" you need to initialize your instance
    bool HelloWorld::init()
    {
        //////////////////////////////
        // 1. super init first
        if ( !Layer::init() )
        {
            return false;
        }
    	
        
    
    	_spriteSheet = SpriteBatchNode::create("sprites.png", 2);
    	SpriteFrameCache::getInstance()->addSpriteFramesWithFile("sprites.plist", "sprites.png");
    	this->addChild(_spriteSheet);
    	this->spawnCar();
    	this->schedule(schedule_selector(HelloWorld::secondUpadte), 1.0f);
    	this->scheduleUpdate();
    
        return true;
    }
    
    void HelloWorld::spawnCar()
    {
    	SpriteFrame* frame = SpriteFrameCache::getInstance()->spriteFrameByName("car.png");
    	Sprite* car = Sprite::createWithSpriteFrame(frame);
    	car->setPosition(Point(100, 100));
    	addBoxBodyForSprite(car);
    	car->setTag(2);
    	car->runAction(MoveTo::create(1.0f, Point(300, 300)));
    	car->runAction(RepeatForever::create(Sequence::create(MoveTo::create(1.0,Point(300, 100)),MoveTo::create(1.0,Point(200, 200)),MoveTo::create(1.0,Point(100, 100)),NULL)));
    	_spriteSheet->addChild(car);
    }
    
    void HelloWorld::spawnCat()
    {
    	auto winSize = Director::getInstance()->getWinSize();
    
    	auto cat = Sprite::createWithSpriteFrameName("cat.png");
    
    	int minY = cat->getContentSize().height / 2;
    	int maxY = winSize.height - (cat->getContentSize().height / 2);
    	int rangeY = maxY - minY;
    	int actualY = CCRANDOM_0_1() * rangeY;
    
    	int startX = winSize.width + (cat->getContentSize().width / 2);
    	int endX = -(cat->getContentSize().width / 2);
    
    	Point startPos = Point(startX, actualY);
    	Point endPos = Point(endX, actualY);
    
    	cat->setPosition(startPos);
    	addBoxBodyForSprite(cat);
    	cat->setTag(1);
    	cat->runAction(Sequence::create(MoveTo::create(1.0, endPos), CallFuncN::create(this, callfuncN_selector(HelloWorld::spriteDone)),NULL));
    
    	_spriteSheet->addChild(cat);
    	
    }
    
    void HelloWorld::addBoxBodyForSprite(Sprite* sprite)
    {
    	auto body = PhysicsBody::createBox(sprite->getContentSize());
    	body->setDynamic(true);
    	body->setCategoryBitmask(1);    // 0001
        body->setCollisionBitmask(-1);   // 0001
        body->setContactTestBitmask(-1); // 0001  必需要设置,并且设置为setDynamic(true);并且必须是0重力能够解决
    	
    	
    	sprite->setPhysicsBody(body);
    }
    
    void HelloWorld::secondUpadte(float dt)
    {
    	this->spawnCat();
    }
    
    void HelloWorld::spriteDone(Node* sender)
    {
    	Sprite *sprite = (Sprite*)sender;
    	_spriteSheet->removeChild(sprite,true);
    }
    
    void HelloWorld::onEnter()
    {
    	Layer::onEnter();
    
    	auto contactListener = EventListenerPhysicsContact::create();
    	contactListener->onContactBegin = CC_CALLBACK_1(HelloWorld::onContactBegin, this);
    
    	auto dispatcher = Director::getInstance()->getEventDispatcher();
    
    	dispatcher->addEventListenerWithSceneGraphPriority(contactListener, this);
    }
    
    bool HelloWorld::onContactBegin( PhysicsContact& contact)
    {
    	auto spriteA = (Sprite*)contact.getShapeA()->getBody()->getNode();
    	auto spriteB = (Sprite*)contact.getShapeB()->getBody()->getNode();
    
    	if (spriteA->getTag() == 1)
    	{
    		spriteA->removeFromParentAndCleanup(true);
    	}
    
    	if (spriteB->getTag() == 1)
    	{
    		spriteB->removeFromParentAndCleanup(true);
    	}
    
    	return true;
    }


  • 相关阅读:
    PostgreSQL 数据类型
    Oracle存储过程
    网络安全统计显示XSS和过时的软件是主要问题
    XSS Fuzzer工具
    Windows安全擦除硬盘擦除器
    msfvenom生成payload命令+内网透到外网
    owaspbwa筆記
    laraval框架model注意事项
    windows mysql utf-8中文乱码解决方法
    为laravel分页样式制定class
  • 原文地址:https://www.cnblogs.com/mfrbuaa/p/5124694.html
Copyright © 2011-2022 走看看