bool guaiwu_js::init() { ////////////////////////////// // 1. super init first if ( !Layer::init() ) { return false; } auto root_layer = CSLoader::createNode("LayerUI/Other/guaiwu.csb"); addChild(root_layer); auto rooaby = root_layer->getChildByName<Node*>("FileNode_1")->getChildByName<Sprite*>("anim_guaiwu_zo0001_2"); PhysicsBody* phybody = PhysicsBody::createBox(Size(rooaby->getContentSize().width-8, rooaby->getContentSize().height-20)); phybody->setTag(400001); phybody->setMass(3000); phybody->setRotationEnable(false); phybody->setCategoryBitmask(0x01); phybody->setCollisionBitmask(0x02); phybody->setContactTestBitmask(0x01); phybody->setDynamic(true); rooaby->setPhysicsBody(phybody); guaiwu = CSLoader::createTimeline("Node/animation/guaiwujs.csb"); guaiwu->gotoFrameAndPlay(0,35, true); root_layer->runAction(guaiwu); scheduleUpdate(); //添加碰撞事件 auto collisionlistener = EventListenerPhysicsContact::create(); collisionlistener->onContactBegin =CC_CALLBACK_1(guaiwu_js::onCollisionBegin, this); collisionlistener->onContactSeparate = CC_CALLBACK_1(guaiwu_js::onContactSeparate,this); Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(collisionlistener, this); return true; }
void SpeedUpProtect::initBody() { auto s = getContentSize(); Vec2 vec[7] = { Vec2(0,0), Vec2(s.width * 0.1, s.height * 0.4), Vec2(s.width * 0.26, s.height * 0.72), Vec2(s.width * 0.5, s.height), Vec2(s.width * 0.74, s.height * 0.72), Vec2(s.width * 0.9, s.height * 0.4), Vec2(s.width,0), }; PhysicsBody* body = PhysicsBody::createPolygon(vec, 7, PHYSICSBODY_MATERIAL_DEFAULT, Vec2(-s.width*0.5,-s.height*0.5)); body->setGravityEnable(false); body->setRotationEnable(false); body->setDynamic(false); body->setMass(10000000); body->setCategoryBitmask(SELF_SCOPE_CATEGORYBITMASK); body->setCollisionBitmask(SELF_SCOPE_COLLISIONBITMASK); body->setContactTestBitmask(SELF_SCOPE_CONTACTTESTBITMASK); setPhysicsBody(body); setInvincible(true); m_ContactPro.hurts = -Ene_Plane_Hp_max - 10; }
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; }
void Hero::initPhysics() { PhysicsBody* pbody = nullptr; pbody = PhysicsBody::createBox(getSize()); pbody->setDynamic(true); pbody->setRotationEnable(false); pbody->setMoment(0); pbody->setMass(0.8); pbody->setContactTestBitmask(1); pbody->setGravityEnable(false); getSprite()->setPhysicsBody(pbody); mEnableGravity = true; mEnableForceField = true; }
BigStone::BigStone(){ Sprite::init(); _isDead = false; PhysicsMaterial material; material.density = 0.f; material.restitution = 0.f; material.friction = 0.f; PhysicsBody* pb = PhysicsBody::createCircle(270,material); //物理挙動の設定 pb->setMass(10000.0f); pb->setDynamic(true); pb->setContactTestBitmask(true); setPhysicsBody(pb); setName("BigStone"); retain(); }
Goal::Goal():clampPos(nullptr){ Sprite::init(); _isDead = false; PhysicsBody* pb = PhysicsBody::createCircle(25); pb->setMass(1000.0f); pb->setDynamic(true); pb->setContactTestBitmask(true); setPhysicsBody(pb); setName("Goal"); /*------- 当たり判定 --------*/ auto c = CollisionDelegate<Goal>::create(this, &Goal::onContact); CollisionFuncManager::getInstance()->addFunc(this->getName(), c); retain(); }
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::createCircle(BIRD_RADIUS); //body->addShape(PhysicsShapeCircle::create(BIRD_RADIUS)); body->setDynamic(true); body->setLinearDamping(0.0f); body->setGravityEnable(false); body->setMass(1.0f); body->setContactTestBitmask(0xFFFFFFFF); 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::createBox(Size(288, landHeight)); //groundBody->addShape(PhysicsShapeBox::create(Size(288, landHeight))); groundBody->setDynamic(false); groundBody->setLinearDamping(0.0f); groundBody->setMass(1.0f); groundBody->setContactTestBitmask(0xFFFFFFFF); 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 = EventListenerPhysicsContactWithBodies::create(bird->getPhysicsBody(),groundNode->getPhysicsBody()); // contactListener->onContactBegin = CC_CALLBACK_1(GameLayer::onContactBegin, this); // this->getEventDispatcher()->addEventListenerWithSceneGraphPriority(contactListener, this); 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; }
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; }
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); }