void Person::addPhysics() { auto size = (this->getBoundingBox()).size; log("%lf %lf", size.width, size.height); auto material = PhysicsMaterial(100.0f, 0.01f, 1.0f); if(_type == TYPE::HERO) size.width /= 2; PhysicsBody *body = PhysicsBody::createBox(Size(size.width,size.height),material); // body->addShape(PhysicsShapeBox::create(Size(size.width,size.height),material)); body->setCategoryBitmask(_type); if(_type == TYPE::MONSTER) { body->setCollisionBitmask(TYPE::MONSTER | TYPE::BRICK | TYPE::GROUND | TYPE::TANGH | TYPE::BULLET | TYPE::BOSS | TYPE::PLANK); body->setContactTestBitmask(TYPE::MONSTER | TYPE::HERO | TYPE::BRICK | TYPE::TANGH | TYPE::BULLET | TYPE::BOSS); } else if(_type == TYPE::HERO) { body->setCollisionBitmask( TYPE::HERO | TYPE::GROUND | TYPE::TANGH | TYPE::PLANK); body->setContactTestBitmask(TYPE::MONSTER | TYPE::HERO | TYPE::GROUND | TYPE::TANGH | TYPE::BULLET | TYPE::TRAP | TYPE::BOSS | TYPE::BULLETENEMY | TYPE::PLANK | TYPE::BUFF); } else if(_type == TYPE::BOSS) { body->setCollisionBitmask( TYPE::HERO | TYPE::GROUND | TYPE::BOSS | PLANK); body->setContactTestBitmask( TYPE::HERO | TYPE::GROUND | TYPE::BOSS); } body->setDynamic(true); body->setLinearDamping(0.0f); body->setRotationEnable(false); body->setGravityEnable(true); this->setPhysicsBody(body); }
bool HeroSprite::init() { bool bRet = false; do { CC_BREAK_IF(!Sprite::initWithSpriteFrameName("heroStand_0001.png")); Size heroSize = this->getContentSize(); PhysicsBody *body = PhysicsBody::createBox(heroSize-Size(0,16), PhysicsMaterial(0, 0, 0)); body->setLinearDamping(0.0f); body->setDynamic(true); body->setGravityEnable(true); body->setTag(TAG_HERO_PHYS_BODY); body->setCategoryBitmask(1<<2); body->setContactTestBitmask(1<<0 | 1<<1); body->setMass(50); body->setRotationEnable(false); this->setPhysicsBody(body); mState = STATE_IDLE; mRunAnimate = NULL; mSmokeRunAnimate = NULL; bRet = true; } while(0); return bRet; }
bool HelloWorld::onTouchBegan(cocos2d::Touch *touch, cocos2d::Event *event) { CCLOG("%s", "touch"); auto location = touch->getLocation(); auto arr = _scene->getPhysicsWorld()->getShapes(location);//从物理世界得到多边形 PhysicsBody *body = nullptr; for (auto &obj : arr) { if ((obj->getBody()->getTag() & DRAG_BODYS_TAG) != 0) //得到刚体 { body = obj->getBody(); break; } } if (body != nullptr) { //创建一个刚体 Node *mouse = Node::create(); mouse->setPhysicsBody(PhysicsBody::create(PHYSICS_INFINITY, PHYSICS_INFINITY)); mouse->getPhysicsBody()->setDynamic(false); mouse->setPosition(location); this->addChild(mouse); body->setLinearDamping(0.0f); //用图钉关节与点中刚体绑定 赋予力 可以拖动 PhysicsJointPin *joint = PhysicsJointPin::construct(mouse->getPhysicsBody(), body, location); joint->setMaxForce(5000.0f * body->getMass()); _scene->getPhysicsWorld()->addJoint(joint); _mouses.insert(std::make_pair(touch->getID(), mouse)); return true; } return false; }
void BoySprite::setPhysical() { // add the physical body PhysicsBody *body = PhysicsBody::create(); body->addShape(NORMAL_SHAPE); body->setDynamic(true); body->setLinearDamping(0.0f); body->setRotationEnable(false); this->setPhysicsBody(body); }
/* auto ball = Sprite::create("Ball.png"); PhysicsBody *ballbody = PhysicsBody::create(); ballbody->addShape(PhysicsShapeCircle::create(BIRD_RADIUS+5)); ballbody->setCategoryBitmask(ColliderTypeBird); ballbody->setCollisionBitmask(ColliderTypeLand | ColliderTypePip); ballbody->setContactTestBitmask(1); ballbody->setDynamic(true); ballbody->setLinearDamping(0.0f); ballbody->setGravityEnable(true); ball->setPhysicsBody(ballbody); auto visibleSize = Director::getInstance()->getVisibleSize(); ball->setPosition(bird->getPositionX()+2*20,visibleSize.height); addChild(ball); */ void GameLayer::addBallObstacle(float dt) { auto ball = Sprite::create("Ball.png"); PhysicsBody *ballbody = PhysicsBody::create(); ballbody->addShape(PhysicsShapeCircle::create(BIRD_RADIUS+5)); ballbody->setCategoryBitmask(ColliderTypeBall); ballbody->setCollisionBitmask(ColliderTypeLand | ColliderTypePip | ColliderTypeBird | ColliderTypeBall); ballbody->setContactTestBitmask(ColliderTypeBird); ballbody->setDynamic(true); ballbody->setLinearDamping(0.0f); ballbody->setGravityEnable(true); ball->setPhysicsBody(ballbody); auto visibleSize = Director::getInstance()->getVisibleSize(); ball->setPosition(bird->getPositionX()+2*20,visibleSize.height); addChild(ball); }
bool Witch::init() { mWitch = CCSprite::createWithSpriteFrameName(Tex::Player_0); this->addChild(mWitch); CCLOG("Witch Width:%f", mWitch->getContentSize().width); mWitch->setPosition(0, 0); PhysicsBody *body = PhysicsBody::create(); body->setTag(1); this->setPhysicsBody(body); body->addShape(PhysicsShapeBox::create(Size(mWitch->getContentSize().width, mWitch->getContentSize().height))); body->setDynamic(true); body->setLinearDamping(0.0f); body->setGravityEnable(false); return true; }
void HelloWorld::onTouchEnded(Touch *touch, Event *event) { auto it = _mouses.find(touch->getID()); if (it != _mouses.end()) { this->removeChild(it->second); _mouses.erase(it); } //增加摩擦阻尼 减小惯性 PhysicsBody *body = _scene->getPhysicsWorld()->getBody(DRAG_BODYS_TAG); if (body != nullptr) { body->setLinearDamping(2.5f); } PhysicsBody *body1 = _scene->getPhysicsWorld()->getBody(DRAG_BODYS_TAG1); if (body1 != nullptr) { body1->setLinearDamping(2.5f); } }
bool GameLayer::init(){ if(Layer::init()) { //get the origin point of the X-Y axis, and the visiable size of the screen Size visiableSize = Director::getInstance()->getVisibleSize(); Point origin = Director::getInstance()->getVisibleOrigin(); this->gameStatus = GAME_STATUS_READY; this->score = 0; // Add the bird this->bird = BirdSprite::getInstance(); this->bird->createBird(); PhysicsBody *body = PhysicsBody::create(); body->addShape(PhysicsShapeCircle::create(BIRD_RADIUS)); //ÅöײÉèÖà body->setCategoryBitmask(ColliderTypeBird); body->setCollisionBitmask(ColliderTypeLand | ColliderTypePip); body->setContactTestBitmask(ColliderTypeLand | ColliderTypePip); body->setDynamic(true); body->setLinearDamping(0.0f); body->setGravityEnable(false); this->bird->setPhysicsBody(body); this->bird->setPosition(origin.x + visiableSize.width*1/3 - 5,origin.y + visiableSize.height/2 + 5); this->bird->idle(); this->addChild(this->bird); // Add the ground this->groundNode = Node::create(); float landHeight = BackgroundLayer::getLandHeight(); auto groundBody = PhysicsBody::create(); groundBody->addShape(PhysicsShapeBox::create(Size(288, landHeight))); groundBody->setDynamic(false); groundBody->setLinearDamping(0.0f); //ÅöײÉèÖà groundBody->setCategoryBitmask(ColliderTypeLand); groundBody->setCollisionBitmask(ColliderTypeBird); groundBody->setContactTestBitmask(ColliderTypeBird); this->groundNode->setPhysicsBody(groundBody); this->groundNode->setPosition(144, landHeight/2); this->addChild(this->groundNode); // init land this->landSpite1 = Sprite::createWithSpriteFrame(AtlasLoader::getInstance()->getSpriteFrameByName("land")); this->landSpite1->setAnchorPoint(Point::ZERO); this->landSpite1->setPosition(Point::ZERO); this->addChild(this->landSpite1, 30); this->landSpite2 = Sprite::createWithSpriteFrame(AtlasLoader::getInstance()->getSpriteFrameByName("land")); this->landSpite2->setAnchorPoint(Point::ZERO); this->landSpite2->setPosition(this->landSpite1->getContentSize().width-2.0f,0); this->addChild(this->landSpite2, 30); shiftLand = schedule_selector(GameLayer::scrollLand); this->schedule(shiftLand, 0.01f); this->scheduleUpdate(); auto contactListener = EventListenerPhysicsContact::create(); contactListener->onContactBegin = CC_CALLBACK_1(GameLayer::onContactBegin, this); this->getEventDispatcher()->addEventListenerWithSceneGraphPriority(contactListener, this); this->dialogLayer = nullptr; return true; }else { return false; } }
void CCutScene::clipPoly(PhysicsShapePolygon* shape, Point normal, float distance) { PhysicsBody* body = shape->getBody(); int count = shape->getPointsCount(); int pointsCount = 0; Point* points = new Point[count + 1]; Vector2dVector vcPoints; vcPoints.clear(); Vector2d v2Point(0, 0); for (int i=0, j=count-1; i<count; j=i, ++i) { Point a = body->local2World(shape->getPoint(j)); float aDist = a.dot(normal) - distance; if (aDist < 0.0f) { points[pointsCount] = a; ++pointsCount; } Point b = body->local2World(shape->getPoint(i)); float bDist = b.dot(normal) - distance; if (aDist*bDist < 0.0f) { float t = std::fabs(aDist)/(std::fabs(aDist) + std::fabs(bDist)); Vec2 v2Tmp = a.lerp(b, t); points[pointsCount] = v2Tmp; ++pointsCount; } } Point center = PhysicsShape::getPolyonCenter(points, pointsCount); for (int i = 0; i < pointsCount; i++) { points[i] = body->world2Local(points[i]); vcPoints.push_back(Vector2d(points[i].x, points[i].y)); } PhysicsBody* polyon = PhysicsBody::createPolygon(points, pointsCount); CFoodCut* pNode = (CFoodCut*)(body->getNode()); std::vector<int> vMaterials; vMaterials.clear(); vMaterials = pNode->getMaterials(); MATERIAL_ID eId = MI_MAX; if (vMaterials.size() != 0) { eId = (MATERIAL_ID)vMaterials[0]; } CFoodCut *filledPolygon = CFoodCut::create(eId, vcPoints, pNode->getPanziIndex(), pNode->getTouchedIndex()); filledPolygon->setPhysicsBody(polyon); int nTmp = rand() % 50 + 50; int nTmpRotate = rand() % 30 - 60; filledPolygon->setPosition(body->getPosition()); //filledPolygon->setRotation(filledPolygon->getRotation() + nTmpRotate); filledPolygon->getPhysicsBody()->setTag(_sliceTag); filledPolygon->getPhysicsBody()->setGravityEnable(false); polyon->setVelocity(body->getVelocityAtWorldPoint(center)); //polyon->setAngularVelocity(body->getAngularVelocity()); polyon->setTag(_sliceTag); float fMass = polyon->getMass(); float fV = 80; float fImpulse = fMass * fV; float fTmpX = (float)(Random() % 30) / 100.0f - 0.15f; float fTmpY = (float)(rand() % 30) / 100.0f - 0.15f; polyon->applyImpulse((normal + Vec2(fTmpX, fTmpY)) * -fImpulse); polyon->setLinearDamping(0.8f); addChild(filledPolygon, 80); filledPolygon->setBirthTime(getCurTime()); m_vCutFoods.push_back(filledPolygon); m_nSliceCount ++; delete[] points; }
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 GameLayer::init(){ if(Layer::init()) { //get the origin point of the X-Y axis, and the visiable size of the screen Size visiableSize = Director::getInstance()->getVisibleSize(); Point origin = Director::getInstance()->getVisibleOrigin(); this->gameStatus = GAME_STATUS_READY; this->score = 0; // Add the bird this->bird = BirdSprite::getInstance(); this->bird->createBird(); PhysicsBody *body = PhysicsBody::create(); body->addShape(PhysicsShapeCircle::create(BIRD_RADIUS)); body->setCategoryBitmask(ColliderTypeBird); body->setCollisionBitmask(ColliderTypeLand & ColliderTypePip | ColliderTypeBall); body->setContactTestBitmask(ColliderTypeLand | ColliderTypePip | ColliderTypeBall); body->setDynamic(true); body->setLinearDamping(0.0f); body->setGravityEnable(false); this->bird->setPhysicsBody(body); this->bird->setPosition(origin.x + visiableSize.width*1/3 - 5,origin.y + visiableSize.height/2 + 5); this->bird->idle(); this->addChild(this->bird); /* //初始化加速粒子 accelerateParticle = ParticleSystemQuad::create("particleImpact.plist"); accelerateParticle->setScale(0.5f); accelerateParticle->setPosition(0,0); addChild(accelerateParticle); */ accelerateParticle = NULL; pipSpeed = 2; /* //闪亮 Blink *blink = Blink::create(5.0f, 10); bird->runAction(blink); */ //Ball this->ball = Sprite::create("ball.png"); PhysicsBody *ballbody = PhysicsBody::create(); ballbody->addShape(PhysicsShapeCircle::create(BIRD_RADIUS+5)); ballbody->setCategoryBitmask(ColliderTypeBall); ballbody->setCollisionBitmask(ColliderTypePip | ColliderTypeLand); ballbody->setContactTestBitmask(ColliderTypePip | ColliderTypeLand); ballbody->setDynamic(true); ballbody->setLinearDamping(0.0f); ballbody->setGravityEnable(false); ball->setPhysicsBody(ballbody); // static Shaky3D* create(float duration, const Size& gridSize, int range, bool shakeZ); ball->setPosition(bird->getPositionX(),bird->getPositionY()+30); ball->setTag(100); // addChild(ball); BallisTouch = false; //BallName ballName = Sprite::create("BallWithHoney.png"); ballName->setPosition(ball->getPositionX(),ball->getPositionY()+40); //addChild(ballName); // Add the ground this->groundNode = Node::create(); float landHeight = BackgroundLayer::getLandHeight(); auto groundBody = PhysicsBody::create(); groundBody->addShape(PhysicsShapeBox::create(Size(288, landHeight))); groundBody->setDynamic(false); groundBody->setLinearDamping(0.0f); groundBody->setCategoryBitmask(ColliderTypeLand); groundBody->setCollisionBitmask(ColliderTypeBird | ColliderTypeBall); groundBody->setContactTestBitmask(ColliderTypeBird | ColliderTypeLand); this->groundNode->setPhysicsBody(groundBody); this->groundNode->setPosition(144, landHeight/2); this->addChild(this->groundNode); // init land this->landSpite1 = Sprite::createWithSpriteFrame(AtlasLoader::getInstance()->getSpriteFrameByName("land")); this->landSpite1->setAnchorPoint(Point::ZERO); this->landSpite1->setPosition(Point::ZERO); this->addChild(this->landSpite1, 30); this->landSpite2 = Sprite::createWithSpriteFrame(AtlasLoader::getInstance()->getSpriteFrameByName("land")); this->landSpite2->setAnchorPoint(Point::ZERO); this->landSpite2->setPosition(this->landSpite1->getContentSize().width-2.0f,0); this->addChild(this->landSpite2, 30); shiftLand = schedule_selector(GameLayer::scrollLand); this->schedule(shiftLand, 0.01f); this->scheduleUpdate(); auto contactListener = EventListenerPhysicsContact::create(); contactListener->onContactBegin = CC_CALLBACK_1(GameLayer::onContactBegin, this); this->getEventDispatcher()->addEventListenerWithSceneGraphPriority(contactListener, this); return true; }else { return false; } }
bool GameLayer::init() { if (!Layer::init()) return false; // Welcome Message Size visiableSize = Director::getInstance()->getVisibleSize(); Vec2 origin = Director::getInstance()->getVisibleOrigin(); // screen background auto screenBackground = Sprite::create("screen_background.png"); screenBackground->setAnchorPoint(Point::ZERO); screenBackground->setPosition(Vec2(0, 0)); this->addChild(screenBackground); // sprite ⇨ mosquito mosquito = Sprite::create("mosquito.png"); mosquito->setPosition(Vec2(30, visiableSize.height - 300)); mosquito->setScale(0.1 * ( visiableSize.width / mosquito->getBoundingBox().size.width)); this->addChild(mosquito); //bug->runAction(this->create(6, bug->getPosition(), Vec2(visiableSize.width - 30, visiableSize.height - 300), 100)); mosquito->runAction(Spawn::create(RotateBy::create(5, 1080), this->create(5, mosquito->getPosition(),Vec2(visiableSize.width - 30, visiableSize.height - 300), 100), NULL)); PhysicsBody *body = PhysicsBody::createCircle(BUG_RADIUS); body->addShape(PhysicsShapeCircle::create(BUG_RADIUS)); body->setDynamic(true); body->setLinearDamping(0.0f); body->setGravityEnable(false); body->setMass(1.0f); body->setContactTestBitmask(0xFFFFFFFF); this->mosquito->setPhysicsBody(body); // ************************************************************************ // // MotionStreakを作成 pStreak = MotionStreak::create(1.0, 1.0f, 3.0f, Color3B::GREEN, "suspend.png"); addChild(pStreak); this->groundNode = Node::create(); //イベントリスナー作成 auto listener = EventListenerTouchOneByOne::create(); //タッチ開始時 listener->onTouchBegan = [this](Touch* touch, Event* event) { Point pos = this->convertTouchToNodeSpace(touch); this->pStreak->setPosition(pos); auto groundBody = PhysicsBody::createBox(Size(4, pos.y)); groundBody->addShape(PhysicsShapeBox::create(Size(4, pos.y))); groundBody->setDynamic(false); groundBody->setLinearDamping(0.0f); groundBody->setMass(1.0f); groundBody->setContactTestBitmask(0xFFFFFFFF); this->groundNode->setPhysicsBody(groundBody); this->groundNode->setPosition(pos.x, pos.y); this->addChild(this->groundNode); return true; }; //タッチ移動時 listener->onTouchMoved = [this](Touch* touch, Event* event) { Point pos = this->convertTouchToNodeSpace(touch); this->pStreak->setPosition(pos); }; //ディスパッチャに登録 Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(listener, 100); // *************************************************************************************** // // sprite ⇨ suspend (init for invisible) suspendBackground = Sprite::create("suspend_background.png"); suspendBackground->setAnchorPoint(Point::ZERO); suspendBackground->setPosition(Vec2(0, 0 + 40)); suspendBackground->setScale(visiableSize.width / suspendBackground->getBoundingBox().size.width, (visiableSize.height - 40)/ suspendBackground->getBoundingBox().size.height); suspendBackground->setTag(2009); suspendBackground->setVisible(false); this->addChild(suspendBackground); //进入暂停 auto suspendBtn = Sprite::create("suspend.png"); auto menuSuspendItem = MenuItemSprite::create(suspendBtn, suspendBtn, NULL, CC_CALLBACK_1(GameLayer::menuSuspendCallback, this)); menuSuspendItem->setPosition(Vec2(visiableSize.width - suspendBtn->getBoundingBox().size.width - 20, visiableSize.height - suspendBtn->getBoundingBox().size.height - 20)); auto menu = Menu::create(menuSuspendItem, NULL); menu->setPosition(Point::ZERO); this->addChild(menu, 3); // suspend close button auto closeButton = Sprite::create("exit.png"); MenuItem *menuCloseItem = MenuItemSprite::create(closeButton, closeButton, NULL, CC_CALLBACK_1(GameLayer::menuSuspendCloseCallback, this)); menuCloseItem->setPosition(Vec2(245, 128)); pauseMenu = Menu::create( menuCloseItem, NULL); pauseMenu->setPosition(Point::ZERO); pauseMenu->setVisible(false); this->addChild(pauseMenu, 4); schedule(schedule_selector(GameLayer::update), 2.0f); auto contactListener = EventListenerPhysicsContact::create(); contactListener->onContactBegin = CC_CALLBACK_1(GameLayer::onContactBegin, this); this->getEventDispatcher()->addEventListenerWithSceneGraphPriority(contactListener, this); return true; }
// on "init" you need to initialize your instance bool Level_6::init() { ////////////////////////////// // 1. super init first if ( !Layer::init() ) { return false; } Size visibleSize = Director::getInstance()->getVisibleSize(); Vec2 visiblePoint = Director::getInstance()->getVisibleOrigin(); auto root_level = CSLoader::createNode("Scene/LevelScene/Level_6.csb"); root_level->setTag(100001); this->addChild(root_level); //设置物理 空心世界 PhysicsBody* psworld =PhysicsBody::createEdgeBox(visibleSize*1.5,PHYSICSBODY_MATERIAL_DEFAULT,2); psworld->setCategoryBitmask(0x01); psworld->setCollisionBitmask(0x01); psworld->setContactTestBitmask(0x01); root_level->setPhysicsBody(psworld); //车 Sprite* maincar = root_level->getChildByName<Sprite*>("img_maincarbody_17"); //退出 Sprite* mainexit = root_level->getChildByName<Sprite*>("img_next_27"); //旋转物体 Sprite* rota = root_level->getChildByName<Sprite*>("gound_6_6"); //创建主要UI界面 mainui = MainUI::create(); auto maincar_r = MainCar_R::create(); mainui->setCar_R(maincar_r); maincar_r->setMainUI(mainui); maincar_r->setObj(maincar); addChild(maincar_r); addChild(mainui,3000); //退出 Exit* eit = Exit::create(); eit->setObj(mainexit); addChild(eit); //添加地面的刚体 Sprite* gound_1 = root_level->getChildByName<Sprite*>("gound_5_5"); Sprite* gound_2 = root_level->getChildByName<Sprite*>("gound_3_4"); Sprite* gound_3 = root_level->getChildByName<Sprite*>("gound_3_3"); Sprite* gound_4 = root_level->getChildByName<Sprite*>("gound_6_6"); Sprite* gound_5 = root_level->getChildByName<Sprite*>("gound_1_1"); ToolsFunction::setPhyDynamicSpriteBox(gound_2); ToolsFunction::setPhyDynamicSpriteBox(gound_3); ToolsFunction::setPhyDynamicSpriteBox(gound_4); ToolsFunction::setPhyDynamicSpriteBox(gound_5); //四个二维数组点 组成多边形 Vec2 pa[6] = { Vec2(-gound_1->getContentSize().width/2,gound_1->getContentSize().height/2-72), Vec2(-gound_1->getContentSize().width/2,gound_1->getContentSize().height/2-2), Vec2(-gound_1->getContentSize().width/3+198 ,gound_1->getContentSize().height/2-2), Vec2(gound_1->getContentSize().width/2-2,-gound_1->getContentSize().height/2+81), Vec2(gound_1->getContentSize().width/2-2,-gound_1->getContentSize().height/2), }; PhysicsBody* aa = PhysicsBody::createPolygon(pa, 5); aa->setDynamic(false); aa->setLinearDamping(5000.0f); gound_1->setPhysicsBody(aa); //按钮 1 Button* b1 = root_level->getChildByName<Button*>("Button_1"); Button* b2 = root_level->getChildByName<Button*>("Button_2"); Button* b3 = root_level->getChildByName<Button*>("Button_3"); Button* b4 = root_level->getChildByName<Button*>("Button_4"); Sprite* b5 = root_level->getChildByName<Sprite*>("Button_5"); //按钮问题 Sprite* q1 = root_level->getChildByName<Sprite*>("question_1"); Sprite* q2 = root_level->getChildByName<Sprite*>("question_2"); Sprite* q3 = root_level->getChildByName<Sprite*>("question_3"); Sprite* q4 = root_level->getChildByName<Sprite*>("question_4"); Sprite* q5 = root_level->getChildByName<Sprite*>("question_5"); //Sprite* q6 = root_level->getChildByName<Sprite*>("ar_50"); //创建颜色按钮 4个按钮 QuestionButton* btn_1 = QuestionButton::create(); btn_1->setObj(b1); addChild(btn_1); QuestionButton* btn_2 = QuestionButton::create(); btn_2->setObj(b2); addChild(btn_2); QuestionButton* btn_3 = QuestionButton::create(); btn_3->setObj(b3); addChild(btn_3); QuestionButton* btn_4 = QuestionButton::create(); btn_4->setObj(b4); addChild(btn_4); Btn_Standard* btn_5 = Btn_Standard::create(); btn_5->setObj(b5); addChild(btn_5); //给按钮设置问题 4个问题 Btn_QuestionSelect* qbtn_1 = Btn_QuestionSelect::create(); qbtn_1->setObj(q1); qbtn_1->setIBtnMainUI(mainui, btn_1); addChild(qbtn_1); Btn_QuestionSelect* qbtn_2 = Btn_QuestionSelect::create(); qbtn_2->setObj(q2); qbtn_2->setIBtnMainUI(mainui, btn_2); addChild(qbtn_2); Btn_QuestionSelect* qbtn_3 = Btn_QuestionSelect::create(); qbtn_3->setObj(q3); qbtn_3->setIBtnMainUI(mainui, btn_3); addChild(qbtn_3); Btn_QuestionSelect* qbtn_4 = Btn_QuestionSelect::create(); qbtn_4->setObj(q4); qbtn_4->setIBtnMainUI(mainui, btn_4); addChild(qbtn_4); Btn_QuestionSelect* qbtn_5 = Btn_QuestionSelect::create(); qbtn_5->setObj(q5); qbtn_5->setIBtnMainUI(mainui, btn_5); addChild(qbtn_5); //4个按钮添加点击事件 btn_1->getBtn()->addTouchEventListener([=](Ref* reft , Widget::TouchEventType type) { if (type == Widget::TouchEventType::ENDED) { if (ismove) { if (isAnim) { //怪物 auto anim = CSLoader::createTimeline("Node/animation/L3_e.csb"); anim->gotoFrameAndPlay(0, 76, false); root_level->runAction(anim); anim->addFrameEndCallFunc(75, "a", [=]() { move_1 =move_1 + 37.5f; auto mt = MoveTo::create(0.8f, Vec2(gound_3->getPositionX(), move_1)); gound_3->runAction(mt); ismove = true; }); isAnim = false; ismove = false; }else{ if ((move_1+37.5f) < 66.0f) { //move_1 = move_1+37.5f; move_1 = -47.5f; auto mt = MoveTo::create(0.8f, Vec2(gound_3->getPositionX(), move_1)); gound_3->runAction(mt); ismove = true; } //怪物 auto anim = CSLoader::createTimeline("Node/animation/L3_e.csb"); anim->gotoFrameAndPlay(230, 265, false); root_level->runAction(anim); } } } }); btn_2->getBtn()->addTouchEventListener([=](Ref* reft,Widget::TouchEventType type) { if (type == Widget::TouchEventType::ENDED) { if (ismove) { if (isAnim) { //怪物 auto anim = CSLoader::createTimeline("Node/animation/L3_e.csb"); anim->gotoFrameAndPlay(0, 76, false); root_level->runAction(anim); anim->addFrameEndCallFunc(75, "a", [=]() { move_1 =move_1 + 37.5f; auto mt = MoveTo::create(0.8f, Vec2(gound_3->getPositionX(), move_1)); gound_3->runAction(mt); ismove = true; }); isAnim = false; ismove = false; }else{ if ((move_1+37.5f) < 66.0f) { //move_1 = move_1+37.5f; move_1 = -10.0f; auto mt = MoveTo::create(0.8f, Vec2(gound_3->getPositionX(), move_1)); gound_3->runAction(mt); ismove = true; } //怪物 auto anim = CSLoader::createTimeline("Node/animation/L3_e.csb"); anim->gotoFrameAndPlay(230, 265, false); root_level->runAction(anim); } } } }); btn_3->getBtn()->addTouchEventListener([=](Ref* reft,Widget::TouchEventType type) { if (type == Widget::TouchEventType::ENDED) { if (ismove) { if (isAnim) { //怪物 auto anim = CSLoader::createTimeline("Node/animation/L3_e.csb"); anim->gotoFrameAndPlay(0, 76, false); root_level->runAction(anim); anim->addFrameEndCallFunc(75, "a", [=]() { move_1 =move_1 + 37.5f; auto mt = MoveTo::create(0.8f, Vec2(gound_3->getPositionX(), move_1)); gound_3->runAction(mt); ismove = true; }); isAnim = false; ismove = false; }else{ if ((move_1+37.5f) < 66.0f) { //move_1 = move_1+37.5f; move_1 = 27.5f; auto mt = MoveTo::create(0.8f, Vec2(gound_3->getPositionX(), move_1)); gound_3->runAction(mt); ismove = true; } //怪物 auto anim = CSLoader::createTimeline("Node/animation/L3_e.csb"); anim->gotoFrameAndPlay(230, 265, false); root_level->runAction(anim); } } } }); btn_4->getBtn()->addTouchEventListener([=](Ref* reft,Widget::TouchEventType type) { if (type == Widget::TouchEventType::ENDED) { if (ismove) { if (isAnim) { //怪物 auto anim = CSLoader::createTimeline("Node/animation/L3_e.csb"); anim->gotoFrameAndPlay(0, 76, false); root_level->runAction(anim); anim->addFrameEndCallFunc(75, "a", [=]() { move_1 =move_1 + 37.5f; auto mt = MoveTo::create(0.8f, Vec2(gound_3->getPositionX(), move_1)); gound_3->runAction(mt); ismove = true; }); isAnim = false; ismove = false; }else{ if ((move_1+37.5f) < 66.0f) { //move_1 = move_1+37.5f; move_1 = 65.0f; auto mt = MoveTo::create(0.8f, Vec2(gound_3->getPositionX(), move_1)); gound_3->runAction(mt); ismove = true; } //怪物 auto anim = CSLoader::createTimeline("Node/animation/L3_e.csb"); anim->gotoFrameAndPlay(230, 265, false); root_level->runAction(anim); } } } }); btn_5->getBtn()->addTouchEventListener([=](Ref* reft,Widget::TouchEventType type) { if (type == Widget::TouchEventType::ENDED) { auto mov = RotateTo::create(0.8f, 29.22f); rota->runAction(mov); } }); //射箭动画 Node* animanode = root_level->getChildByName<Node*>("FileNode_1"); auto archery = CSLoader::createTimeline("Node/animation/anim_Archery.csb"); archery->gotoFrameAndPlay(0, 155, true); root_level->runAction(archery); archery->addFrameEndCallFunc(95, "a", [=]() { Archery* a = Archery::create(); a->setPosition(Vec2(animanode->getPositionX(), animanode->getPositionY())); addChild(a); }); return true; }
void SinglePlayerScene::setupPhysics() { Size visibleSize = Director::getInstance()->getVisibleSize(); float visibleHeight = visibleSize.height; PhysicsWorld* world = this->getScene()->getPhysicsWorld(); world->setGravity(Vec2(0, (visibleHeight / 320.0f) * -98.0f)); world->setSpeed(GAME_SPEED); world->setDebugDrawMask(DEBUGDRAW_OPTIONS); Vec2 takyanPos = _takyan->getPosition(); PhysicsBody *takyanBody = PhysicsBody::createCircle(_takyan->getContentSize().width * BALL_RADIUS, PhysicsMaterial(0.01f, 1.0f, 1.0f)); //_takyan->getPhysicsBody(); float takyanMass = takyanBody->getMass(); float velocityLimit = takyanMass * 150.0f; takyanBody->setMass(takyanMass * 100.0f); takyanBody->setRotationEnable(false); takyanBody->setVelocityLimit(velocityLimit); _takyan->setPhysicsBody(takyanBody); PHYSICS_MASK(takyanBody, MASK_TAKYAN, MASK_BOUNDS | MASK_KICKER, MASK_BOUNDS | MASK_KICKER); Size takyanSize = _takyan->getContentSize(); takyanSize.height *= 0.8f; PhysicsMaterial material(0.0025f, 0.0f, 1.0f); PhysicsBody *takyanTailBody = PhysicsBody::createCircle(takyanSize.width, material); takyanTailBody->setMass(takyanMass * 0.01); takyanTailBody->setRotationEnable(false); takyanTailBody->setLinearDamping(1.0f); takyanTailBody->setVelocityLimit(velocityLimit * 0.5f); _takyanTail->setPhysicsBody(takyanTailBody); PHYSICS_MASK(takyanTailBody, MASK_TAKYAN_TAIL, 0, MASK_FLOOR); Vec2 diff = _takyanTail->getPosition() - _takyan->getPosition(); Vec2 anch1 = Vec2::ANCHOR_MIDDLE; Vec2 anch2 = Vec2::ANCHOR_MIDDLE; auto jointS = PhysicsJointLimit::construct(takyanBody, takyanTailBody, anch1, anch2, 0, diff.y); jointS->setCollisionEnable(false); world->addJoint(jointS); PhysicsBody *kickerBody = PhysicsBody::createCircle(_kicker->getContentSize().width * KICKER_RADIUS, PhysicsMaterial(1.0f, 1.0f, 1.0f)); //_kicker->getPhysicsBody(); kickerBody->setDynamic(false); kickerBody->setEnable(false); _kicker->setPhysicsBody(kickerBody); PHYSICS_MASK(kickerBody, MASK_KICKER, MASK_TAKYAN, MASK_TAKYAN); int takyans = MASK_TAKYAN | MASK_TAKYAN_TAIL; auto floorBody = PhysicsBody::createBox(_floorBounds->getContentSize(), PhysicsMaterial(1.0f, 0.3f, 0.7f)); //_floorBounds->getPhysicsBody(); floorBody->setDynamic(false); _floorBounds->setPhysicsBody(floorBody); PHYSICS_MASK(floorBody, MASK_BOUNDS, takyans, takyans); auto ceilingBody = PhysicsBody::createBox(_ceilingBounds->getContentSize(), PhysicsMaterial(1.0f, 0.3f, 0.7f)); ceilingBody->setDynamic(false); _ceilingBounds->setPhysicsBody(ceilingBody); PHYSICS_MASK(ceilingBody, MASK_BOUNDS, MASK_TAKYAN, MASK_TAKYAN); Size wallSize = _leftWallBounds->getContentSize(); wallSize.height *= 5.0f; auto leftWallBody = PhysicsBody::createBox(wallSize); //_leftWallBounds->getPhysicsBody(); leftWallBody->setDynamic(false); _leftWallBounds->setPhysicsBody(leftWallBody); PHYSICS_MASK(leftWallBody, MASK_WALLS, takyans, MASK_TAKYAN); auto rightWallBody = PhysicsBody::createBox(wallSize); //_rightWallBounds->getPhysicsBody(); rightWallBody->setDynamic(false); _rightWallBounds->setPhysicsBody(rightWallBody); PHYSICS_MASK(rightWallBody, MASK_WALLS, takyans, MASK_TAKYAN); }