void Ball::initPhysics(){ PhysicsBody *spriteBody = PhysicsBody::createCircle(this->getSprite()->getBoundingBox().size.width , PHYSICSBODY_MATERIAL_DEFAULT); spriteBody->setDynamic(true); spriteBody->getShape(0)->setRestitution(1.0f); spriteBody->getShape(0)->setFriction(0.0f); spriteBody->getShape(0)->setDensity(1.0f); spriteBody->getShape(0)->setMass(100); spriteBody->setGravityEnable(false); spriteBody->setVelocity(this->velocity); this->getSprite()->setPhysicsBody(spriteBody); }
void ShowLayer::initPhyscsObject(Rect* m) { for (int i = 0; i < 26; i++) { Sprite* sp = Sprite::create(); auto maskLayer = LayerColor::create(Color4B(0, 0, 255, 200)); maskLayer->setContentSize(m[i].size); maskLayer->setAnchorPoint(Vec2(0, 0)); sp->addChild(maskLayer, 15); PhysicsBody* playerBody = PhysicsBody::createBox(m[i].size, PHYSICSBODY_MATERIAL_DEFAULT); playerBody->setDynamic(false); //设置质量 playerBody->getShape(0)->setMass(100); //设置物体是否受重力系数影响 playerBody->setGravityEnable(false); playerBody->setCategoryBitmask(3); playerBody->setContactTestBitmask(3); playerBody->setCollisionBitmask(3); sp->setContentSize(m[i].size); sp->setPosition(Vec2(m[i].getMidX(), m[i].getMidY())); sp->setAnchorPoint(Vec2(0.0, 0.0)); sp->setPhysicsBody(playerBody); this->addChild(sp); } }
void LevelFifteen::callbackC(Node* sender) { auto visibleSize = Director::getInstance()->getVisibleSize(); auto pictureName = 1 + rand() % 27; auto str = String::createWithFormat("%d.png", pictureName)->getCString(); auto ballTwo2 = Sprite::createWithSpriteFrameName(str); ballTwo2->setPosition(Vec2(visibleSize.width / 2.0f, visibleSize.height - 90)); PhysicsBody* ballBodyTwo = nullptr; if (pictureName <= 9) { ballBodyTwo = PhysicsBody::createBox(ballTwo2->getContentSize(), PHYSICSBODY_MATERIAL_DEFAULT); } else if (pictureName > 9 && pictureName <= 18) { ballBodyTwo = PhysicsBody::createCircle(ballTwo2->getContentSize().width / 2.0f, PHYSICSBODY_MATERIAL_DEFAULT); } else if (pictureName > 18 && pictureName <= 21) { Vec2 vec[] = { Vec2(-22.50000, 22.00000), Vec2(24.00000, 10.00000), Vec2(-10.00000, -24.00000) }; ballBodyTwo = PhysicsBody::createPolygon(vec, 3); } else if (pictureName > 21 && pictureName <= 24) { Vec2 vec[] = { Vec2(-15.50000, 15.00000), Vec2(15.50000, 6.00000), Vec2(-7.00000, -15.50000) }; ballBodyTwo = PhysicsBody::createPolygon(vec, 3); } else if (pictureName > 24 && pictureName <= 27) { Vec2 vec[] = { Vec2(-8.00000, 8.00000), Vec2(7.50000, 3.00000), Vec2(-4.00000, -7.50000) }; ballBodyTwo = PhysicsBody::createPolygon(vec, 3); } //是否设置物体为静态 //ballBodyTwo->setDynamic(false); ballBodyTwo->getShape(0)->setRestitution(1.0f); ballBodyTwo->getShape(0)->setFriction(0.0f); ballBodyTwo->getShape(0)->setDensity(1.0f); ballBodyTwo->setGravityEnable(false); ballBodyTwo->setCategoryBitmask(2);// 分类掩码 ballBodyTwo->setCollisionBitmask(1 | 2 | 4 | 8);// 碰撞掩码 ballBodyTwo->setContactTestBitmask(1 | 8);// 接触测试掩码 ballBodyTwo->addMass(1000.0f); int flag = CCRANDOM_0_1() * 2; auto num = 0; switch (flag) { case 0: num = -1; break; case 1: num = 1; break; default: break; } auto force1 = Vec2(CCRANDOM_0_1()*10000.0f*num, -300000.0f); ballBodyTwo->applyImpulse(force1); ballTwo2->setPhysicsBody(ballBodyTwo); this->addChild(ballTwo2); }
bool GameLayer::init() { if (!Layer::init()) { return false; } this->score = 0; this->gameStatus = GameStatus::GAME_RUNNING; this->bestScore = UserDefault::getInstance()->getIntegerForKey(KEY); visibleSize = Director::getInstance()->getVisibleSize(); //创建小鸟 this->bird = BirdSprite::getInstance(); this->bird->createBird(); //创建物理属性 PhysicsBody *body = PhysicsBody::createCircle(15); //设置为受到重力影响的动态刚体 body->setDynamic(true); //设置线性阻尼 body->setLinearDamping(0.0f); //设置刚体是否受物理世界重力的影响 body->setGravityEnable(false); //设置形状的恢复系数 //body->getShape(0)->setRestitution(0.0f); body->getShape(0)->setDensity(1.0f); //设置所属种类的掩码值 body->setCategoryBitmask(BIRD_MASK); body->setCollisionBitmask(BIRD_MASK | OBST_MASK); body->setContactTestBitmask(BIRD_MASK | OBST_MASK); this->bird->setPhysicsBody(body); this->bird->setPosition(visibleSize.width / 3, visibleSize.height / 2); this->bird->idle(); this->bird->fly(); this->addChild(this->bird, 2); //添加陆地物理属性 auto land = Node::create(); landHeight = BackgroundLayer::getLandHeight(); auto landBody = PhysicsBody::createBox(Size(visibleSize.width, landHeight)); landBody->getShape(0)->setRestitution(0.0f); landBody->setDynamic(false); landBody->setGravityEnable(false); landBody->setCategoryBitmask(OBST_MASK); landBody->setCollisionBitmask(BIRD_MASK | OBST_MASK); landBody->setContactTestBitmask(BIRD_MASK | OBST_MASK); land->setPhysicsBody(landBody); land->setPosition(visibleSize.width / 2, landHeight / 2); this->addChild(land, 10); //添加碰撞监听 auto contactListener = EventListenerPhysicsContact::create(); contactListener->onContactBegin = CC_CALLBACK_1(GameLayer::onContactBegin, this); _eventDispatcher->addEventListenerWithSceneGraphPriority(contactListener, this); this->createPips(); this->scheduleUpdate(); this->schedule(schedule_selector(GameLayer::scrollPipe), 0.01f); return true; }
bool HelloWorld::init(PhysicsWorld* world,int _level) { if ( !Layer::init() ) { return false; } level = _level; //NotificationCenter::getInstance()->addObserver(this, callfuncO_selector(HelloWorld::setLevel), "selectedLevel", NULL); dispatcher = Director::getInstance()->getEventDispatcher(); preLoadMusic(); playBgMusic(); m_world = world; //m_world->setAutoStep(false); m_world->setGravity(Vect(0,-grivity)); this->setAnchorPoint(Point(0,0)); winSize = Director::getInstance()->getWinSize(); visibleSize = Director::getInstance()->getVisibleSize(); origin = Director::getInstance()->getVisibleOrigin(); auto map1path = String::createWithFormat("%s%d%s","images/level",level,"/bg.png"); map1 = Sprite::create(map1path->getCString()); map1->setPosition(map1->getContentSize().width/2+origin.x, map1->getContentSize().height/2+origin.y); this->addChild(map1,1,MAP1_TAG); auto map2path = String::createWithFormat("%s%d%s", "images/level", level, "/bg.png"); map2 = Sprite::create(map2path->getCString()); map2->setPosition(map1->getContentSize().width+map2->getContentSize().width/2+origin.x, map2->getContentSize().height/2+origin.y); this->addChild(map2,1,MAP2_TAG); Sprite* edge1 = Sprite::create(); edge1->setContentSize(visibleSize); PhysicsBody* edgeBody1 = PhysicsBody::createEdgeBox(visibleSize); edgeBody1->setDynamic(false); edgeBody1->setGravityEnable(false); edgeBody1->setMass(100000.0f); edgeBody1->getShape(0)->setRestitution(0.0f); edgeBody1->setContactTestBitmask(0xFF); edgeBody1->setCategoryBitmask(0x04); edgeBody1->setCollisionBitmask(0xFF); edgeBody1->getShape(0)->setTag(EDGE_TAG); edge1->setPhysicsBody(edgeBody1); edge1->setPosition(visibleSize.width/2,visibleSize.height/2); addChild(edge1,1,EDGEONE_TAG); Sprite* edge = Sprite::create(); edge->setContentSize(visibleSize); PhysicsBody* edgeBody = PhysicsBody::createEdgeBox(Size(visibleSize.width,visibleSize.height/6)); edgeBody->setDynamic(false); edgeBody->setGravityEnable(false); edgeBody->setMass(100000.0f); edgeBody->getShape(0)->setRestitution(0.0f); edgeBody->setContactTestBitmask(0xFF); edgeBody->setCategoryBitmask(0x04); edgeBody->setCollisionBitmask(0xFF); edgeBody->getShape(0)->setTag(EDGE_TAG); edge->setPhysicsBody(edgeBody); edge->setPosition(visibleSize.width / 2, visibleSize.height / 12); addChild(edge, 1, EDGE_TAG); Sprite* floor = Sprite::create(); PhysicsBody* floorBody = PhysicsBody::createEdgeSegment(Point(0, visibleSize.height / 10), Point(visibleSize.width, visibleSize.height / 10)); floorBody->setDynamic(false); floorBody->setGravityEnable(false); floorBody->getShape(0)->setRestitution(0.0f); floorBody->setMass(100000.0f); floorBody->setCategoryBitmask(0x0003); floorBody->setContactTestBitmask(0x0003); floorBody->setCollisionBitmask(0x0001); floor->setPosition(0, visibleSize.height / 10); floor->setPhysicsBody(floorBody); //this->addChild(floor,2,FLOOR_TAG); setEnemyFrameNumber(); setPlayerFrameNumber(); initDamage(); player1 = new player(); auto playerPath = String::createWithFormat("%s%d%s", "images/level", level, "/player.png"); player1->sprite = Sprite::create(playerPath->getCString()); PhysicsBody* playerBody = PhysicsBody::createBox(player1->sprite->getContentSize()); playerBody->setRotationEnable(false); playerBody->getShape(0)->setRestitution(0.0f); playerBody->getShape(0)->setFriction(0.8f); playerBody->setMass(player1->mass); playerBody->setCollisionBitmask(0x04); playerBody->setCategoryBitmask(0x01); playerBody->setContactTestBitmask(0xFF); playerBody->getShape(0)->setTag(PLAYER_TAG); player1->sprite->setPhysicsBody(playerBody); player1->sprite->setPosition(player1->sprite->getContentSize().width/2+5, floor->getPositionY()+player1->sprite->getContentSize().height*3); addChild(player1->sprite,2,PLAYER_TAG); preLoadAnimation(); auto backItem = MenuItemImage::create("images/CloseNormal.png", "images/CloseSelected.png", CC_CALLBACK_0(HelloWorld::backToChoseLevel, this)); //auto pauseItem = MenuItemImage::create("CloseNormal.png", "CloseSelected.png", CC_CALLBACK_0(HelloWorld::pauseScene, this)); auto popupItem = MenuItemImage::create("images/CloseNormal.png", "images/CloseSelected.png", CC_CALLBACK_0(HelloWorld::toPopup, this)); //pauseItem->setPosition(-backItem->getContentSize().width,0); popupItem->setPosition(0,0); backItem->setPosition(backItem->getContentSize().width,0); auto backmenu = Menu::create(popupItem,backItem,NULL); backmenu->setPosition(visibleSize.width - backItem->getContentSize().width*2, visibleSize.height - backItem->getContentSize().height / 2); addChild(backmenu, 10); hpLayer = HPLayer::create(); hpLayer->setPosition(0, visibleSize.height / 15 * 14); addChild(hpLayer, 2, HPLAYER_TAG); Vector<MenuItem*> attackLogoItems; MenuItemSprite* attackLogoItem; for (int i = 0; i < ATTACKNUMBER; i++){ auto logoPath = String::createWithFormat("%s%d%s%d%s", "images/level", level, "/attack", i, "/logo.png"); auto normalSprite = Sprite::create(logoPath->getCString()); normalSprite->setOpacity(200); auto selectedSprite = Sprite::create(logoPath->getCString()); attackLogoItem = MenuItemSprite::create(normalSprite, selectedSprite, CC_CALLBACK_0(player::setAttack, player1, i)); attackLogoItem->setPosition(Vec2(0, -i*attackLogoItem->getContentSize().height)); attackLogoItems.pushBack(attackLogoItem); } auto attackLogoMenu = Menu::createWithArray(attackLogoItems); attackLogoMenu->setPosition(visibleSize.width - attackLogoItem->getContentSize().width, visibleSize.height / 2 + 2 * attackLogoItem->getContentSize().height); addChild(attackLogoMenu, 10); onTouchEvent(); onKeyBoardEvent(); physicsEvent(); EventCustom e("custom"); dispatcher->dispatchEvent(&e); enemyComing(1.0f); this->schedule(schedule_selector(HelloWorld::enemyComing), 5.0f); this->schedule(schedule_selector(HelloWorld::enemyAttack), 2.0f); this->scheduleUpdate(); log("init"); return true; }