#include "HelloWorldScene.h"

USING_NS_CC;

#define RED_BIT_MASK    0b0100

#define GREEN_BIT_MASK  0b0010

#define BLUE_BIT_MASK   0b0001

#define EDGE_BIT_MASK   0b1000

Scene* HelloWorld::createScene()

{

auto scene = Scene::createWithPhysics();

scene->getPhysicsWorld()->setDebugDrawMask(PhysicsWorld::DEBUGDRAW_ALL);

scene->getPhysicsWorld()->setGravity(Vec2(0,-1000));

auto layer = HelloWorld::create();

scene->addChild(layer);

return scene;

}

bool HelloWorld::init()

{

if ( !Layer::init() )

{

return false;

}

visibleSize=Director::getInstance()->getVisibleSize();

auto touchListener=EventListenerTouchOneByOne::create();

touchListener->onTouchBegan =[this](Touch * t,Event * e){

this->addBox(t->getLocation());

return false;

};

Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(touchListener,this);

auto contactListener=EventListenerPhysicsContact::create();

contactListener->onContactBegin=[](PhysicsContact & contact){

switch (contact.getShapeA()->getBody()->getContactTestBitmask()|contact.getShapeB()->getBody()->getContactTestBitmask()) {

case RED_BIT_MASK|BLUE_BIT_MASK:

log("red rect contact blue rect");

break;

case RED_BIT_MASK|GREEN_BIT_MASK:

log("red rect contact green rect");

break;

case BLUE_BIT_MASK|GREEN_BIT_MASK:

log("blue rect contact green rect");

break;

case RED_BIT_MASK|EDGE_BIT_MASK:

log("red rect hit edge");

break;

case GREEN_BIT_MASK|EDGE_BIT_MASK:

log("green rect hit edge");

break;

case BLUE_BIT_MASK|EDGE_BIT_MASK:

log("blue rect hit edge");

break;

case GREEN_BIT_MASK:

log("two green rect hit");

break;

default:

break;

}

return true;

};

Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(contactListener,this);

return true;

}

void HelloWorld::onEnter(){

Layer::onEnter();

auto bounds=Node::create();

bounds->setContentSize(visibleSize);

bounds->setPhysicsBody(PhysicsBody::createEdgeBox(bounds->getContentSize()));

bounds->getPhysicsBody()->setContactTestBitmask(EDGE_BIT_MASK);

bounds->setPosition(visibleSize/2);

addChild(bounds);

addBox(visibleSize/2);

}

void HelloWorld::addBox(Vec2 position){

auto r=Sprite::create();

r->setTextureRect(Rect(50,50,50,50));

r->setPhysicsBody(PhysicsBody::createBox(r->getContentSize()));

addChild(r);

r->setPosition(position);

//    switch (rand()%6) {

//        case 0:

//            r->setColor(Color3B(255,0,255));

//            break;

//        case 1:

//            r->setColor(Color3B(255,0,0));

//            break;

//        case 2:

//            r->setColor(Color3B(0,255,0));

//            break;

//        case 3:

//            r->setColor(Color3B(0,0,255));

//            break;

//        case 4:

//            r->setColor(Color3B(0,255,255));

//            break;

//        case 5:

//            r->setColor(Color3B(255,255,0));

//            break;

//        default:

//            break;

//    }

switch (rand()%3) {

case 0:

r->setColor(Color3B(255,0,0));

r->getPhysicsBody()->setContactTestBitmask(RED_BIT_MASK);

break;

case 1:

r->setColor(Color3B(0,255,0));

r->getPhysicsBody()->setContactTestBitmask(GREEN_BIT_MASK);

break;

case 2:

r->setColor(Color3B(0,0,255));

r->getPhysicsBody()->setContactTestBitmask(BLUE_BIT_MASK);

break;

default:

break;

}

}

void HelloWorld::menuCloseCallback(Ref* pSender)

{

Director::getInstance()->end();

#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)

exit(0);

#endif

}

最新文章

  1. DBNEWNAME工具介绍
  2. PHP7中php.ini、php-fpm和www.conf的配置
  3. *HDU1598 并查集
  4. ADF_Data Binding系列2_使用URL Service Data Control
  5. java.io.stream
  6. 【LeetCode】16. 3Sum Closest
  7. ScrollView 与ListView 滑动冲突完美解决
  8. PHP内置Web Server探究(二)自定义PHP控制台输出console函数
  9. 获取第上一个兄弟元素 屏蔽浏览器的差异(PreviousElementSibling)
  10. appium问题整理
  11. 蓝桥杯java 关于大范围时间的
  12. $.each遍历json数组
  13. Python面向对象之反射
  14. linux常用命令(个人总结)
  15. Oracle Ora 错误解决方案合集
  16. vim技巧3
  17. python 更换 版本
  18. Zookeeper安装使用及JavaAPI使用
  19. HashMap和LinkedHashMap的比较使用
  20. mybatis 控制台打印sql脚本

热门文章

  1. Linq专题之查询操作
  2. 【循序渐进学Python】5.Python常用流程控制及其他语句
  3. PHP限制HTML内容中图片必须是本站的方法
  4. 【JS复习笔记】07 复习感想
  5. java之内的工具分享,附带下载链接,方便以后自己寻找
  6. [moka同学笔记]yii2.0 dropdownlist的简单使用(一)
  7. linux 常用技巧
  8. flask-uploads扩展的使用笔记
  9. 【iOS】Quartz2D矩阵操作
  10. LINUX重启MYSQL的命令