bool GameScene24::init() { if ( !Layer::init() ) { return false; } m_ui = UISimple::create(); this->addChild(m_ui, 30); Size visibleSize = Director::getInstance()->getWinSize(); Point origin = Director::getInstance()->getVisibleOrigin(); zidan = false; time = 5.0; //背景 auto bg = Sprite::create("level_24/bg.jpg"); bg->setPosition(Point(visibleSize.width* 0.5f,visibleSize.height * 0.5f)); this->addChild(bg); //创建月亮 moon = Sprite::create("level_24/hand1.png"); moon->setPosition(Point(visibleSize.width * 0.5f,visibleSize.height * 0.5f)); moon->setAnchorPoint(Point::ANCHOR_MIDDLE); moon->setTag(13); this->addChild(moon); auto body = PhysicsBody::createCircle(moon->getContentSize().width * 0.5f); body->getShape(0)->setFriction(0.0f); body->getShape(0)->setRestitution(1.001f); body->getShape(0)->setDensity(1.0f); body->setCategoryBitmask(1); // 0001 body->setCollisionBitmask(1); // 0001 body->setContactTestBitmask(1); // 0001 body->setGravityEnable(false); moon->setPhysicsBody(body); body->applyImpulse(Vect(3500000, 0)); //创建锯齿边缘 auto border = Sprite::create("level_24/border.png"); Size borderSize = border->getContentSize(); auto border1 = createBorder(Point(borderSize.width * 0.9f, borderSize.height * 0.7f)); this->addChild(border1); auto border2 = createBorder(Point(visibleSize.width - borderSize.width * 0.5f, borderSize.height * 0.7f)); border2->setFlippedX(true); this->addChild(border2); auto border3 = createBorder(Point(visibleSize.width * 0.5f, visibleSize.height * 0.15f)); borderSize = border3->getContentSize(); border3->setRotation(90.0f); this->addChild(border3); auto border4 = createBorder(Point(visibleSize.width * 0.5f, visibleSize.height * 0.8f)); borderSize = border4->getContentSize(); border4->setRotation(90.0f); this->addChild(border4); //子弹 shoot = Sprite::create("level_24/zidan1.png"); shoot->setPosition(Point( visibleSize.width * 0.5f,visibleSize.height * 0.15f+40)); shoot->setVisible(false); shoot->setAnchorPoint(Point::ANCHOR_MIDDLE); shoot->setTag(14); this->addChild(shoot); body1 = PhysicsBody::createCircle(shoot->getContentSize().width * 0.5f); body1->setCategoryBitmask(1); // 0001 body1->setCollisionBitmask(1); // 0001 body1->setContactTestBitmask(1); // 0001 shoot->setPhysicsBody(body1); //加载初始文字 boss = Sprite::create("new/text_she.png"); boss->setAnchorPoint(Point::ANCHOR_MIDDLE_BOTTOM); boss->setPosition(Point(visibleSize.width/2,visibleSize.height+100)); this->addChild(boss,30); auto pSequence2 = Sequence::create(CallFunc::create( [&]() { auto bossAct = MoveTo::create(0.5f, Point(Director::getInstance()->getVisibleSize().width/2, 680)); auto bossEase = EaseBackInOut::create(bossAct); boss->runAction(bossEase); }), DelayTime::create(1.0f), CallFunc::create( [&]() { boss->runAction(MoveTo::create(0.5f, Point(Director::getInstance()->getVisibleSize().width/2, Director::getInstance()->getVisibleSize().height+200))); }),NULL); this->runAction(pSequence2); //触摸监听 auto listener = EventListenerTouchOneByOne::create(); //listener->setSwallowTouches(true); listener->onTouchBegan = CC_CALLBACK_2(GameScene24::onTouchBegan,this); listener->onTouchEnded = CC_CALLBACK_2(GameScene24::onTouchEnded,this); _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); //碰撞监听 auto contactListener = EventListenerPhysicsContact::create(); contactListener->onContactBegin = CC_CALLBACK_1(GameScene24::onContactBegin, this); _eventDispatcher->addEventListenerWithSceneGraphPriority(contactListener, this); //5秒内无动作失败 this->schedule(schedule_selector(GameScene24::update)); return true; }
bool HelloWorld::init() { if (!Layer::init()) { return false; } Size visibleSize = Director::getInstance()->getVisibleSize(); Vec2 origin = Director::getInstance()->getVisibleOrigin(); auto edgeBody = PhysicsBody::createEdgeBox(visibleSize, PhysicsMaterial(0,1,0), 3); edgeBody->setDynamic(false); auto edgeNode = Node::create(); edgeNode->setPosition(Point(visibleSize.width / 2 + origin.x, visibleSize.height / 2 + origin.y)); edgeNode->setPhysicsBody(edgeBody); this->addChild(edgeNode); bg = Sprite::create("background.png"); ball = Sprite::create("ball.png"); paddle = Sprite::create("paddle.png"); label = Label::createWithSystemFont(std::to_string(score), "Arial", 40); bg->setAnchorPoint(Vec2(0.5, 0.5)); ball->setAnchorPoint(Vec2(0, 1)); paddle->setAnchorPoint(Vec2(0, 1)); label->setAnchorPoint(Vec2(0, 0)); bg->setPosition(320, 240); ball->setPosition(160, 240); paddle->setPosition(208, 48); for (int i = 0; i < 8; i++) { for (int j = 0; j < 5; j++) { block[i][j] = Sprite::create("brick.png"); block[i][j]->setAnchorPoint(Vec2(0, 1)); block[i][j]->setPosition(192 + 32 * i, 400 - 16 * j); } } auto spriteBody = PhysicsBody::createCircle(ball->getContentSize().width/2, PhysicsMaterial(0, 1, 0)); spriteBody->setCollisionBitmask(1); spriteBody->setContactTestBitmask(true); Vect force = Vect(80, -80); spriteBody->applyImpulse(force); ball->setPhysicsBody(spriteBody); auto spriteBody2 = PhysicsBody::createBox(paddle->getContentSize(), PhysicsMaterial(0, 1, 0)); spriteBody2->setDynamic(false); paddle->setPhysicsBody(spriteBody2); for (int i = 0; i < 8; i++) { for (int j = 0; j < 5; j++) { auto blockBody = PhysicsBody::createBox(block[i][j]->getContentSize(), PhysicsMaterial(0, 1, 0)); blockBody->setCollisionBitmask(2); blockBody->setContactTestBitmask(true); blockBody->setDynamic(false); block[i][j]->setPhysicsBody(blockBody); } } this->addChild(bg, 0); this->addChild(ball, 0); this->addChild(paddle, 0); this->addChild(label, 1); for (int i = 0; i < 8; i++) { for (int j = 0; j < 5; j++) { this->addChild(block[i][j], 0); } } this->scheduleUpdate(); auto eventListener = EventListenerKeyboard::create(); auto contactListener = EventListenerPhysicsContact::create(); eventListener->onKeyPressed = [=](EventKeyboard::KeyCode keyCode, Event* event)->void { Vector<FiniteTimeAction*> actions; Vec2 loc = event->getCurrentTarget()->getPosition(); switch (keyCode) { case EventKeyboard::KeyCode::KEY_LEFT_ARROW: case EventKeyboard::KeyCode::KEY_A: paddle->setPosition(paddle->getPosition().x - 16, paddle->getPosition().y); if (paddle->getPosition().x < 0) { paddle->setPosition(560, paddle->getPosition().y); } break; case EventKeyboard::KeyCode::KEY_RIGHT_ARROW: case EventKeyboard::KeyCode::KEY_D: paddle->setPosition(paddle->getPosition().x + 16, paddle->getPosition().y); if (paddle->getPosition().x > 560) { paddle->setPosition(0, paddle->getPosition().y); } break; } }; contactListener->onContactBegin = CC_CALLBACK_1(HelloWorld::onContactBegin, this); this->_eventDispatcher->addEventListenerWithSceneGraphPriority(eventListener, paddle); this->getEventDispatcher()->addEventListenerWithSceneGraphPriority(contactListener, this); return true; }
void Pipe::SpawnPipe( cocos2d::Layer *layer ) { CCLOG( "SPAWN PIPE" ); //SpriteBatchNode* spritebatch = SpriteBatchNode::create("flappySprites.png"); //SpriteFrameCache* cache = SpriteFrameCache::getInstance(); //cache->addSpriteFramesWithFile("flappySprites.plist"); //auto backgroundSprite = Sprite::createWithSpriteFrameName("background1.png"); auto topPipe = Sprite::createWithSpriteFrameName( "pipe.png" ); auto bottomPipe = Sprite::createWithSpriteFrameName( "pipe.png" ); auto topPipeBody = PhysicsBody::createBox( topPipe->getContentSize( ) ); auto bottomPipeBody = PhysicsBody::createBox( bottomPipe->getContentSize( ) ); auto random = CCRANDOM_0_1( ); if ( random < LOWER_SCREEN_PIPE_THRESHOLD ) { random = LOWER_SCREEN_PIPE_THRESHOLD; } else if ( random > UPPER_SCREEN_PIPE_THRESHOLD ) { random = UPPER_SCREEN_PIPE_THRESHOLD; } auto topPipePosition = ( random * visibleSize.height ) + ( topPipe->getContentSize( ).height*2 / 3 ); topPipeBody->setDynamic( false ); bottomPipeBody->setDynamic( false ); topPipeBody->setCollisionBitmask( OBSTACLE_COLLISION_BITMASK ); bottomPipeBody->setCollisionBitmask( OBSTACLE_COLLISION_BITMASK ); topPipeBody->setContactTestBitmask( true ); bottomPipeBody->setContactTestBitmask( true ); topPipe->setPhysicsBody( topPipeBody ); bottomPipe->setPhysicsBody( bottomPipeBody ); topPipe->setFlippedY(true); topPipe->setPosition( Point( visibleSize.width + topPipe->getContentSize( ).width + origin.x, topPipePosition ) ); bottomPipe->setPosition( Point( topPipe->getPositionX(), topPipePosition - ( Sprite::createWithSpriteFrameName( "frame-1.png" )->getContentSize( ).height * PIPE_GAP ) - topPipe->getContentSize().height ) ); layer->addChild( topPipe ); layer->addChild( bottomPipe ); auto topPipeAction = MoveBy::create( PIPE_MOVEMENT_SPEED * visibleSize.width, Point( -visibleSize.width * 1.5, 0 ) ); auto bottomPipeAction = MoveBy::create( PIPE_MOVEMENT_SPEED * visibleSize.width, Point( -visibleSize.width * 1.5, 0 ) ); topPipe->runAction( topPipeAction ); bottomPipe->runAction( bottomPipeAction ); auto pointNode = Node::create( ); auto pointBody = PhysicsBody::createBox( Size( 1, Sprite::createWithSpriteFrameName( "frame-1.png" )->getContentSize( ).height * PIPE_GAP ) ); pointBody->setDynamic( false ); pointBody->setCollisionBitmask( POINT_COLLISION_BITMASK ); pointBody->setContactTestBitmask( true ); pointBody->resetForces(); pointNode->setPhysicsBody( pointBody ); pointNode->setPosition( Point( topPipe->getPositionX( ), topPipe->getPositionY( ) - ( topPipe->getContentSize( ).height / 2 ) - ( ( Sprite::createWithSpriteFrameName( "frame-1.png" )->getContentSize( ).height * PIPE_GAP ) / 2 ) ) ); layer->addChild( pointNode ); auto pointNodeAction = MoveBy::create( PIPE_MOVEMENT_SPEED * visibleSize.width, Point( -visibleSize.width * 1.5, 0 ) ); pointNode->runAction( pointNodeAction );}
void PowerUp::SetPowerUp(cocos2d::Layer *layer, float posX, float posY) { int i = cocos2d::RandomHelper::random_int(0, 20); //set this to everytime for testing purposes //CCLOG("I: %i", i); this->x = posX; this->y = posY; if (i == 1 || i == 2) //life Up { auto LifeUp = Sprite::create("Extra Life.png"); // May not need the auto auto LifeUpBounding = PhysicsBody::createCircle((10.0f, 10.0f), PhysicsMaterial(1.0f, 2.0f, 0.0f)); a = cocos2d::RandomHelper::random_int(-12000, 12000); b = cocos2d::RandomHelper::random_int(8000, 10000); //powerUpBounding->setVelocity(Vec2(a, b)); // LifeUpBounding->applyImpulse(Vec2(a, b)); LifeUpBounding->applyTorque(12000); LifeUpBounding->applyForce(Vect(a, b)); LifeUpBounding->setDynamic(true); LifeUpBounding->setGravityEnable(true); LifeUpBounding->setContactTestBitmask(true); LifeUpBounding->setCategoryBitmask(1); LifeUpBounding->setCollisionBitmask(LiveUp_Bitmask); LifeUp->setPhysicsBody(LifeUpBounding); LifeUp->setPosition(Vec2(x, y)); layer->addChild(LifeUp); LifeUp->setZOrder(1); } else if (i == 3 || i == 4 || i == 5 || i == 6)// Ball Split { auto two = Sprite::create("MultiBall Two.png"); // May not need the auto auto twoBounding = PhysicsBody::createCircle((10.0f, 10.0f), PhysicsMaterial(1.0f, 2.0f, 0.0f)); a = cocos2d::RandomHelper::random_int(-12000, 12000); b = cocos2d::RandomHelper::random_int(8000, 10000); //powerUpBounding->setVelocity(Vec2(a, b)); // twoBounding->applyImpulse(Vec2(a, b)); twoBounding->applyTorque(12000); twoBounding->applyForce(Vect(a, b)); twoBounding->setDynamic(true); twoBounding->setGravityEnable(true); twoBounding->setContactTestBitmask(true); twoBounding->setCategoryBitmask(1); twoBounding->setCollisionBitmask(TwoSplit_Bitmask); two->setPhysicsBody(twoBounding); two->setPosition(Vec2(x, y)); layer->addChild(two); two->setZOrder(1); } else if (i == 7 || i == 8 || i == 9)// Three Way (Imagine the chance to disappoint two women at once.) { auto three= Sprite::create("MultiBall Three.png"); // May not need the auto auto threeBounding = PhysicsBody::createCircle((10.0f, 10.0f), PhysicsMaterial(1.0f, 2.0f, 0.0f)); a = cocos2d::RandomHelper::random_int(-12000, 12000); b = cocos2d::RandomHelper::random_int(8000, 10000); //powerUpBounding->setVelocity(Vec2(a, b)); // threeBounding->applyImpulse(Vec2(a, b)); threeBounding->applyTorque(12000); threeBounding->applyForce(Vect(a, b)); threeBounding->setDynamic(true); threeBounding->setGravityEnable(true); threeBounding->setContactTestBitmask(true); threeBounding->setCategoryBitmask(1); threeBounding->setCollisionBitmask(ThreeSplit_Bitmask); three->setPhysicsBody(threeBounding); three->setPosition(Vec2(x, y)); layer->addChild(three); three->setZOrder(1); } else if (i == 10)// Nine Way! { auto nine = Sprite::create("MultiBall Nine.png"); // May not need the auto auto nineBounding = PhysicsBody::createCircle((10.0f, 10.0f), PhysicsMaterial(1.0f, 2.0f, 0.0f)); a = cocos2d::RandomHelper::random_int(-12000, 12000); b = cocos2d::RandomHelper::random_int(8000, 10000); //powerUpBounding->setVelocity(Vec2(a, b)); // nineBounding->applyImpulse(Vec2(a, b)); nineBounding->applyTorque(12000); nineBounding->applyForce(Vect(a, b)); nineBounding->setDynamic(true); nineBounding->setGravityEnable(true); nineBounding->setContactTestBitmask(true); nineBounding->setCategoryBitmask(1); nineBounding->setCollisionBitmask(NineSplit_Bitmask); nine->setPhysicsBody(nineBounding); //sets a bounding box around brick. nine->setPosition(Vec2(x, y)); layer->addChild(nine); nine->setZOrder(1); } }
bool LevelSeven::init() { if (!Layer::init()) { return false; } // 用于解决刚体穿透问题 this->scheduleUpdate(); // 计时器 this->schedule(schedule_selector(LevelSeven::timeCounter), 1.0f); auto visibleSize = Director::getInstance()->getVisibleSize(); auto origin = Director::getInstance()->getVisibleOrigin(); // 加载背景贴图 auto spriteBg = Sprite::create("background.png"); spriteBg->setAnchorPoint(Vec2::ZERO); spriteBg->setPosition(Vec2::ZERO); addChild(spriteBg); // 将资源加载到精灵帧缓存 SpriteFrameCache::getInstance()->addSpriteFramesWithFile("threeColor.plist"); ballOne = Sprite::createWithSpriteFrameName("hero.png"); ballOne->setPosition(visibleSize.width / 2.0f, visibleSize.height / 2.0f); auto ballBodyOne = PhysicsBody::createCircle(ballOne->getContentSize().width / 2, PHYSICSBODY_MATERIAL_DEFAULT); //是否设置物理为静态 //ballBodyOne->setDynamic(false); //设置物理的恢复力 ballBodyOne->getShape(0)->setRestitution(0.5f); //设置物体的摩擦力 ballBodyOne->getShape(0)->setFriction(0.0f); ballBodyOne->getShape(0)->setDensity(0.3f); // 设置质量 质量等于密度乘以面积 //ballBodyOne->getShape(0)->setMass(5000); // 设置物体是否受重力系数影响 ballBodyOne->setGravityEnable(true); ballBodyOne->setCategoryBitmask(1);// 分类掩码 ballBodyOne->setCollisionBitmask(1 | 2 | 4);// 碰撞掩码 ballBodyOne->setContactTestBitmask(2);// 接触测试掩码 // 把物体添加到精灵 ballOne->setPhysicsBody(ballBodyOne); this->addChild(ballOne); //创建一个盒子,用来碰撞 auto edgeSpace = Sprite::create(); auto boundBody = PhysicsBody::createEdgeBox(visibleSize, PHYSICSBODY_MATERIAL_DEFAULT, 3); boundBody->getShape(0)->setFriction(0.0f); boundBody->getShape(0)->setRestitution(1.0f); edgeSpace->setPhysicsBody(boundBody); edgeSpace->setPosition(Vec2(visibleSize.width / 2, visibleSize.height / 2)); this->addChild(edgeSpace); edgeSpace->setTag(0); boundBody->setCategoryBitmask(4); boundBody->setCollisionBitmask(1 | 2 | 4); boundBody->setContactTestBitmask(0); // 创建yuan auto white1 = Sprite::createWithSpriteFrameName("white2.png"); auto whiteBody1 = PhysicsBody::createCircle(white1->getContentSize().width / 2.0f, PHYSICSBODY_MATERIAL_DEFAULT); whiteBody1->setDynamic(false); whiteBody1->setCategoryBitmask(4); whiteBody1->setCollisionBitmask(1 | 2 | 4); whiteBody1->setContactTestBitmask(0); white1->setPhysicsBody(whiteBody1); this->addChild(white1); // 正方形 auto white1_2 = Sprite::createWithSpriteFrameName("white1.png"); auto whiteBody1_2 = PhysicsBody::createBox(white1_2->getContentSize(), PHYSICSBODY_MATERIAL_DEFAULT); whiteBody1_2->setDynamic(false); whiteBody1_2->setCategoryBitmask(4); whiteBody1_2->setCollisionBitmask(1 | 2 | 4); whiteBody1_2->setContactTestBitmask(0); white1_2->setPhysicsBody(whiteBody1_2); this->addChild(white1_2); // 创建三角形 Vec2 arr[] = { Vec2(-16.50000, 36.00000), Vec2(35.50000, -15.00000), Vec2(-34.00000, -32.50000) }; auto white3 = Sprite::createWithSpriteFrameName("white3.png"); auto whiteBody3 = PhysicsBody::createPolygon(arr, 3, PHYSICSBODY_MATERIAL_DEFAULT); whiteBody3->setDynamic(false); whiteBody3->setCategoryBitmask(4); whiteBody3->setCollisionBitmask(1 | 2 | 4); whiteBody3->setContactTestBitmask(0); white3->setPhysicsBody(whiteBody3); this->addChild(white3); // 随机位置 int index = CCRANDOM_0_1() * 3; switch (index) { case 0: { white1->setPosition(visibleSize.width / 2.0f, visibleSize.height / 2.0f + 200.0f); whiteBody1->setDynamic(true); whiteBody1->setGravityEnable(false); white1_2->setPosition(visibleSize.width / 2.0f + 100.0f, visibleSize.height / 2.0f); white3->setPosition(visibleSize.width / 2.0f - 100.0f, visibleSize.height / 2.0f); } break; case 1: { white1_2->setPosition(visibleSize.width / 2.0f, visibleSize.height / 2.0f + 200.0f); whiteBody1_2->setDynamic(true); whiteBody1_2->setGravityEnable(false); white1->setPosition(visibleSize.width / 2.0f + 100.0f, visibleSize.height / 2.0f); white3->setPosition(visibleSize.width / 2.0f - 100.0f, visibleSize.height / 2.0f); } break; case 2: { white3->setPosition(visibleSize.width / 2.0f, visibleSize.height / 2.0f + 200.0f); whiteBody3->setDynamic(true); whiteBody3->setGravityEnable(false); white1->setPosition(visibleSize.width / 2.0f + 100.0f, visibleSize.height / 2.0f); white1_2->setPosition(visibleSize.width / 2.0f - 100.0f, visibleSize.height / 2.0f); } break; default: break; } // 正方形旋转 auto rotation = RotateBy::create(1, 360); auto rotationback = rotation->reverse(); auto seq = Sequence::create(rotation, rotationback, nullptr); auto repeat = RepeatForever::create(seq); white1->runAction(repeat); // // 第二个正方形旋转 auto rotation_1 = RotateBy::create(2, 360); auto rotationback_1 = rotation_1->reverse(); auto seq1 = Sequence::create(rotation_1, rotationback_1, nullptr); auto repeat1 = RepeatForever::create(seq1); white1_2->runAction(repeat1); // 三角形旋转 auto rotation_2 = RotateBy::create(2, 360); auto rotationback_2 = rotation_2->reverse(); auto seq2 = Sequence::create(rotation_2, rotationback_2, nullptr); auto repeat2 = RepeatForever::create(seq2); white3->runAction(repeat2); auto yuantong0 = Sprite::createWithSpriteFrameName("yuantong.png"); yuantong0->setPosition(Vec2(visibleSize.width / 2.0f, visibleSize.height + yuantong0->getContentSize().height / 2.0f)); auto yuantongBody = PhysicsBody::createBox(yuantong0->getContentSize(), PHYSICSBODY_MATERIAL_DEFAULT); yuantongBody->setDynamic(false); yuantongBody->setCategoryBitmask(4); yuantongBody->setCollisionBitmask(1 | 2 | 4); yuantongBody->setContactTestBitmask(0); yuantong0->setPhysicsBody(yuantongBody); this->addChild(yuantong0); auto moveby0 = MoveBy::create(1.0f, Vec2(0, -90)); auto moveby0back = moveby0->reverse(); auto easein = EaseBackIn::create(moveby0); auto seq0 = Sequence::create(easein, CallFuncN::create(CC_CALLBACK_1(LevelSeven::callbackC, this)), moveby0back, nullptr); auto repeat0 = RepeatForever::create(seq0); yuantong0->runAction(repeat0); listener = EventListenerTouchOneByOne::create(); listener->onTouchBegan = [=](Touch* touch, Event* event){ auto touchPosition = touch->getLocation(); Vec2 force = Vec2::ZERO; if (touchPosition.x > visibleSize.width / 2.0f) { force = Vec2(50000.0f, 0.0f); } else { force = Vec2(-50000.0f, 0.0f); } ballOne->getPhysicsBody()->applyImpulse(force); return true; }; _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); // 时间显示文本 test = LabelAtlas::create(StringUtils::toString(LEVELSEVEN), "1.png", 14, 21, '0'); test->setScale(2.0f); test->setAnchorPoint(Vec2::ANCHOR_MIDDLE); test->setPosition(Vec2(test->getContentSize().width*2, visibleSize.height - test->getContentSize().height)); this->addChild(test); test2 = LabelAtlas::create("0", "1.png", 14, 21, '0'); test2->setScale(2.0f); test2->setAnchorPoint(Vec2::ANCHOR_MIDDLE); test2->setPosition(Vec2(visibleSize.width - test2->getContentSize().width*2, visibleSize.height - test2->getContentSize().height)); this->addChild(test2); return true; }
void HelloWorld::addPhysicsSprite() { // _scene->getPhysicsWorld()->setUpdateRate(5.0f); // wall 添加物理边境 Size size = VisibleRect::getVisibleRect().size; auto wall = Node::create(); //给节点添加静态矩形刚体(PhysicsBody)并带材质(PhysicsMaterial) wall->setPhysicsBody(PhysicsBody::createEdgeBox(Size(size.width - 5, size.height - 5), PhysicsMaterial(0.1f, 1.0f, 1.0f))); // wall->getPhysicsBody()->setDynamic(false);//设置为静态刚体(Edge // wall->getPhysicsBody()->setEnable(false); wall->getPhysicsBody()->setGroup(1); //组编号 /**一个body的CategoryBitmask和另一个body的ContactTestBitmask的逻辑与的结果不等于0时,接触事件将被发出,否则不发送。 *一个body的CategoryBitmask和另一个body的CollisionBitmask的逻辑与结果不等于0时,他们将碰撞,否则不碰撞 */ // wall->getPhysicsBody()->setCategoryBitmask(0x03);// 0011 碰撞系数编号 // wall->getPhysicsBody()->setContactTestBitmask(0x03);// 0001 碰撞检测编号 // wall->getPhysicsBody()->setCollisionBitmask(0x01);// 0001 碰撞编号 wall->setPosition(VisibleRect::center()); //位置可见区域中心 this->addChild(wall); //多纳的头 auto tou = Sprite::create(shizi_tou_png); // tou->setPhysicsBody(PhysicsBody::createCircle(271, PhysicsMaterial(1000.1f, 0.0f, 1.0f)));//添加半径为271动态圆形刚体 并赋予材质密度1000.1f 反弹力0.0f 摩擦力1.0f auto toubody = PEShapeCache::getInstance()->getPhysicsBodyByName("shizi_tou"); tou->setPhysicsBody(toubody); tou->getPhysicsBody()->setTag(DRAG_BODYS_TAG);//给刚体设置标签 tou->getPhysicsBody()->setMass(0.1);//刚体设置质量 tou->getPhysicsBody()->setGroup(2);//刚体组编号 tou->getPhysicsBody()->setCategoryBitmask(0x01); // 0001 tou->getPhysicsBody()->setContactTestBitmask(0x01); // 0001 tou->getPhysicsBody()->setCollisionBitmask(0x01); // 0001 // tou->getPhysicsBody()->setDynamic(false); 动态刚体是可以设置为静态 tou->getPhysicsBody()->setRotationEnable(false); //设置不可旋转刚体 碰撞后也不会旋转刚体 // tou->getPhysicsBody()->setGravityEnable(false);//设置是否接受重力影响 tou->getPhysicsBody()->setLinearDamping(3.0f); //设置线性阻尼系数 理论是0-1 但是可以大于1 值越大惯性越小 this->addChild(tou); tou->setPosition(VisibleRect::center()); auto touyingzi = Node::create(); touyingzi->setPhysicsBody(PhysicsBody::createCircle(271, PhysicsMaterial(1000.1f, 0.0f, 1.0f))); touyingzi->getPhysicsBody()->setTag(DRAG_BODYS_TAG_Y); //多纳的身子 //PhysicsShapePolygon 是通过点数组来构建不规则的凸多边形;用工具PhysicsEditor 编辑shap 让后导出Chipmunk 格式的plist 中的数据 注:PhysicsEditor Relative为锚点Anchor 设置为cocos默认值(0.5,0.5)才行 , auto shengzi = Sprite::create(shizi_shenzi_png); // auto shengzibody = PhysicsBody::create(); // Point vert1[3] = {Point(109.50000, 71.00000), Point(14.00000, 77.00000), Point(117.50000, 147.00000)}; // shengzibody->addShape(PhysicsShapePolygon::create(vert1, 3, PhysicsMaterial(0.1f, 0.0f, 1.0f))); // Point vert2[6] = {Point(-130.50000, -154.00000), Point(-120.50000, 46.00000), Point(-67.50000, 102.00000), Point(14.00000, 77.00000), Point(-4.00000, -93.00000), Point(-63.00000, -178.50000)}; // shengzibody->addShape(PhysicsShapePolygon::create(vert2, 6, PhysicsMaterial(0.1f, 0.0f, 1.0f))); // Point vert3[6] = {Point(138.50000, 18.00000), Point(110.50000, -177.00000), Point(51.50000, -175.00000), Point(-4.00000, -93.00000), Point(14.00000, 77.00000), Point(109.50000, 71.00000)}; // shengzibody->addShape(PhysicsShapePolygon::create(vert3, 6, PhysicsMaterial(0.1f, 0.0f, 1.0f))); // Point vert4[4] = {Point(-67.50000, 102.00000), Point(-55.00000, 172.50000), Point(-54.00000, 172.50000), Point(14.00000, 77.00000)}; // shengzibody->addShape(PhysicsShapePolygon::create(vert4, 4, PhysicsMaterial(0.1f, 0.0f, 1.0f))); auto shengzibody = PEShapeCache::getInstance()->getPhysicsBodyByName("shizi_shenzi"); shengzi->setPhysicsBody(shengzibody); shengzibody->setTag(DRAG_BODYS_TAG1); shengzibody->setRotationEnable(false); shengzibody->setGroup(2); shengzibody->setMass(0.1); shengzibody->setCategoryBitmask(0x02); //0010 shengzibody->setContactTestBitmask(0x02); //0010 shengzibody->setCollisionBitmask(0x02); //0010 shengzibody->setLinearDamping(3.0f); this->addChild(shengzi); shengzi->setPosition(VisibleRect::bottom() - Point(0, -300)); // auto contactListener = EventListenerPhysicsContactWithBodies::create(tou->getPhysicsBody(), shengzi->getPhysicsBody()); // contactListener->onContactBegin = CC_CALLBACK_1(HelloWorld::onContactBegin, this); // _eventDispatcher->addEventListenerWithSceneGraphPriority(contactListener, this); //多纳的头 auto b1 = Sprite::create(shizi_youshou_png); b1->setPhysicsBody(PEShapeCache::getInstance()->getPhysicsBodyByName("shizi_youshou")); b1->setPosition(VisibleRect::center()+Point(300,0)); b1->getPhysicsBody()->setTag(1); b1->getPhysicsBody()->setRotationEnable(false); this->addChild(b1); auto b2 = Sprite::create(shizi_zuoshou_png); b2->setPhysicsBody(PEShapeCache::getInstance()->getPhysicsBodyByName("shizi_zuoshou")); b2->setPosition(VisibleRect::center()-Point(300,0)); b2->getPhysicsBody()->setRotationEnable(false); b2->getPhysicsBody()->setTag(1); this->addChild(b2); // LabelTTF auto label2 = LabelTTF::create("多纳小狮子爱学习", "Arial", 64); label2->setPhysicsBody(PhysicsBody::createBox(label2->getBoundingBox().size, PhysicsMaterial(0.1f, 0.0f, 1.0f))); label2->getPhysicsBody()->setTag(1); // label2->getPhysicsBody()->setRotationEnable(false); label2->setPosition(VisibleRect::center()+Point(0,300)); addChild(label2, 0); PEShapeCache::getInstance()->removeBodysWithWithFile(buttonsbodys_plist); //注册碰撞检测监听 auto contactListener1 = EventListenerPhysicsContact::create(); contactListener1->onContactBegin = CC_CALLBACK_1(HelloWorld::onContactBegin, this); contactListener1->onContactPostSolve = CC_CALLBACK_2(HelloWorld::onContactPostSolve, this); _eventDispatcher->addEventListenerWithSceneGraphPriority(contactListener1, this); }