bool Player::init() { if (!Sprite::initWithFile("graphics/luk_sprite.png")) { return false; } this->setAnchorPoint(Vec2::ANCHOR_MIDDLE); cocos2d::Point PBox[8]{cocos2d::Point(-2, -12), cocos2d::Point(-4, -10), cocos2d::Point(-4, -2), cocos2d::Point(-2, 0), cocos2d::Point(2, 0), cocos2d::Point(4, -2), cocos2d::Point(4, -10), cocos2d::Point(2, -12)}; playerRect = Rect(); auto body = PhysicsBody::createPolygon(PBox, 8); body->setEnable(true); body->setDynamic(true); body->setGravityEnable(true); body->setRotationEnable(false); body->setVelocityLimit(30.0); body->setCategoryBitmask(static_cast<int>(Stage::TileType::PLAYER)); //BLOCKSとの接触判定をON body->setCollisionBitmask(static_cast<int>(Stage::TileType::EMPTY)); body->setContactTestBitmask(static_cast<int>(Stage::TileType::BLOCKS)); //body->setContactTestBitmask(INT_MAX); this->setPhysicsBody(body); this->scheduleUpdate(); return true; }
void Devil::InitSprite() { auto body = PhysicsBody::createBox(this->getContentSize(), PhysicsMaterial(100, 1, 1)); body->setDynamic(true); body->setGravityEnable(false); body->setRotationEnable(false); body->setCollisionBitmask(MONSTER_BODY_COLLISION_BITMASK); body->setContactTestBitmask(true); this->setPhysicsBody(body); this->setUserData(this); hpBgSpr = Sprite::create("Monster/hpBg.png"); this->addChild(hpBgSpr, ZINDEX_MONSTER_HP); hpCurrentSpr = Sprite::create("Monster/hpCurrent.png"); this->addChild(hpCurrentSpr, ZINDEX_MONSTER_HP); auto origin = Director::getInstance()->getVisibleOrigin(); hpBgSpr->setPosition(Vec2(this->getContentSize().width / 2, this->getPosition().y + this->getContentSize().height + hpBgSpr->getContentSize().height / 2)); hpCurrentSpr->setPosition(hpBgSpr->getPosition()); hpBgSpr->setOpacity(0); hpCurrentSpr->setOpacity(0); fury = CSLoader::createNode("Effect/Rage/Rage.csb"); fury->setPosition(this->getContentSize().width * 0.5f, this->getContentSize().height * 0.5f); this->addChild(fury); fury->setVisible(false); auto visibleSize = Director::getInstance()->getVisibleSize(); d = Dragon::create(layer); d->setPosition(visibleSize.width*0.7f, visibleSize.height*0.7f); layer->addChild(d, ZINDEX_MONSTER_SPRITE); d->scheduleUpdate(); }
bool Player::init() { if (!Sprite::initWithFile("graphics/luk_sprite.png")) { return false; } //Point PBox[8]{Point(-2, -12), Point(-4, -10), Point(-4, -2), Point(-2, 0), Point(2, 0), Point(4, -2), Point(4, -10), Point(2, -12)}; Point PBox[4]{Point(-4, -12), Point(-4, 0), Point(4, 0), Point(4, -12)}; //auto body = PhysicsBody::createEdgePolygon(PBox, 4,PHYSICSBODY_MATERIAL_DEFAULT,0.5f); auto body = PhysicsBody::createPolygon(PBox, 4); body->setEnable(true); body->setDynamic(true); body->setGravityEnable(true); body->setRotationEnable(false); body->setVelocityLimit(30.0); body->setCategoryBitmask(static_cast<int>(Stage::TileType::PLAYER)); //BLOCKSとの接触判定をON //body->setCollisionBitmask(static_cast<int>(Stage::TileType::BLOCKS)); body->setCollisionBitmask(static_cast<int>(Stage::TileType::NONE)); body->setContactTestBitmask(static_cast<int>(Stage::TileType::BLOCKS)); //body->setContactTestBitmask(INT_MAX); this->setPhysicsBody(body); this->setAnchorPoint(Vec2::ANCHOR_MIDDLE); this->scheduleUpdate(); return true; }
Hero::Hero(cocos2d::Layer *layer) { const float SCALE_TO_DEVICE = Director::getInstance()->getVisibleSize().width / DES_RES_X; visibleSize = Director::getInstance()->getVisibleSize(); origin = Director::getInstance()->getVisibleOrigin(); move_x=origin.x+visibleSize.width/2; move_y=origin.y+visibleSize.height/2; M_hero=Sprite::create("Jump (1).png"); M_hero->setScale(SCALE_TO_DEVICE); M_hero->setPosition(Point(origin.x + visibleSize.width / 2, origin.y + visibleSize.height * 0.2)); auto size=M_hero->getContentSize() * SCALE_TO_DEVICE; size.height=size.height*0.62; size.width=size.width*0.62; auto hero_b = PhysicsBody::createBox(size); //hero_b->setDynamic(false); hero_b->setRotationEnable(false); M_hero->setPhysicsBody(hero_b); hero_b->setCollisionBitmask(HERO_COLLISION_BITMASK); hero_b->setContactTestBitmask(HERO_CONTACT_BITMASK); Vector<SpriteFrame*> trix_ani(4); trix_ani.pushBack(SpriteFrame::create("Jump (1).png", Rect(0,0,140,192))); trix_ani.pushBack(SpriteFrame::create("Jump (2).png", Rect(0,0,140,192))); trix_ani.pushBack(SpriteFrame::create("Jump (3).png", Rect(0,0,140,192))); trix_ani.pushBack(SpriteFrame::create("Jump (4).png", Rect(0,0,140,192))); auto ani=Animation::createWithSpriteFrames(trix_ani,0.1f); auto anim_rr=Animate::create(ani); auto anim_r=RepeatForever::create(anim_rr); M_hero->runAction(anim_r); layer->addChild(M_hero, 100000); }
bool Bullet::init (int tag) { // if (!Sprite::initWithFile("faceball.png")){ if (!Sprite::initWithFile("ball.png")){ return false; } m_tag = tag; this->setColor(_tagColor[tag]); // 大きさをランダムにとりあえず bulletSize = _bulSize[tag]; // this->setContentSize(cocos2d::Size (bulletSize * 20, bulletSize * 20)); // もともと設定されてたもの // なんか、物理エンジンがおかしくなるような気がする // これを消すと落ち着く this->setScale(0.13f * bulletSize); //this->setScale(0.043f * bulletSize); // 密度、反発、摩擦 // まるいやつ。回転がわかりズラい // auto pBall = PhysicsBody::createCircle(bulletSize, // PhysicsMaterial(10.0f, 0.6f, 0.3f)); // しかくいやつ。これは四角すぎる // auto pBall = PhysicsBody::createBox(cocos2d::Size(bulletSize, bulletSize), PhysicsMaterial(1.0f, 0.6f, 0.3f)); Point spritePoints[12] = { Point(-1, 0.333) *= bulletSize, Point(-0.666, 0.5) *= bulletSize, Point(-0.333, 0.666) *= bulletSize, Point(0.333, 0.666) *= bulletSize, Point(0.666, 0.5) *= bulletSize, Point(1, 0.333) *= bulletSize, Point(1, -0.333) *= bulletSize, Point(0.666, -0.5) *= bulletSize, Point(0.333, -0.666) *= bulletSize, Point(-0.333, -0.666) *= bulletSize, Point(-0.666, -0.5) *= bulletSize, Point(-1, -0.333) *= bulletSize }; // for (Point target : spritePoints) { // target *= bulletSize; // } // 密度、反発、摩擦 auto pBall = PhysicsBody::createPolygon(spritePoints, 12, PhysicsMaterial(1.0f, 0.0f, 1.0f)); // auto pBall = PhysicsBody::createPolygon(spritePoints, 12, PhysicsMaterial(1.0f, 0.6f, 0.3f)); pBall->setDynamic(true); pBall->setRotationEnable(true); // 回転率?なのか?これを無限にすると回転しない // pBall->setMoment(PHYSICS_INFINITY); // pBall->setMoment(bulletSize); pBall->setGravityEnable(true); //log("%f",pBall->getMass()); // pBall->setMass(1.0); pBall->setVelocityLimit(800); this->setPhysicsBody(pBall); return true; }
void Ball::createPhysicsBody() { auto bodyRadius = getContentSize().width / 2; auto body = PhysicsBody::createCircle(bodyRadius, MATERIAL); body->setVelocityLimit(VELOCITY_LIMIT); body->setRotationEnable(false); body->setContactTestBitmask(BITMASK_CONTACT_TEST); setPhysicsBody(body); }
bool Player::init() { //セーブされたプレイヤーレベルを取得 auto selectkey = UserDefault::getInstance()->getIntegerForKey(PLAYER_SELECT_KEY); if(selectkey == 0) { selectkey= 2; //デフォルトはイルカ! } _level = selectkey; // auto playerFile = StringUtils::format(PLAYER_FILE_FORMAT, _level); if (!Sprite::initWithFile(playerFile)) { return false; } //1フレームの画像サイズを取得する(4フレームキャラクター) auto frameSize = Size(this->getContentSize().width / FRAME_COUNT, this->getContentSize().height); //テスクチャの大きさを1フレーム分にする this->setTextureRect(Rect(0,0,frameSize.width, frameSize.height)); Vector<SpriteFrame *> frames; for(int i=0; i < FRAME_COUNT; ++i){ //一コマずつアニメーションを作成 auto frame = SpriteFrame::create(playerFile, Rect(frameSize.width * i,0,frameSize.width, frameSize.height)); frames.pushBack(frame); } auto animation = Animation::createWithSpriteFrames(frames); animation->setDelayPerUnit(0.05); this->runAction(RepeatForever::create(Animate::create(animation))); auto body = PhysicsBody::createCircle(this->getContentSize().width /2.0); //剛体の回転を有効 body->setDynamic(true); body->setRotationEnable(true); //カテゴリをセット body->setCategoryBitmask(static_cast<int>(Stage::TileType::PLAYER)); //壁のみ衝突する body->setCollisionBitmask(static_cast<int>(Stage::TileType::WALL)); //すべての剛体と接触判定を行う body->setContactTestBitmask(INT_MAX); // 初期加速度を設定する // _acceleration = INITIAL_ACCELERATION; this->scheduleUpdate(); this->setPhysicsBody(body); return true; }
bool Player::init() { if (!Sprite::initWithFile("player.png")) { return false; } // 1フレームの画像サイズを取得する auto frameSize = Size(this->getContentSize().width / FRAME_COUNT, this->getContentSize().height); // テクスチャの大きさを1フレーム分にする this->setTextureRect(Rect(0, 0, frameSize.width, frameSize.height)); // Vector<SpriteFrame *> frames; for (int i = 0; i < FRAME_COUNT; ++i) { // 1コマずつアニメーションを作成する auto frame = SpriteFrame::create("player.png", Rect(frameSize.width * i, 0, frameSize.width, frameSize.height)); // frames.pushBack(frame); } //アニメーションを作成して動かす auto animation = Animation::createWithSpriteFrames(frames); animation->setDelayPerUnit(0.05); this->runAction(RepeatForever::create(Animate::create(animation))); //剛体を作成 auto material = PhysicsMaterial(); auto body = PhysicsBody::createCircle(this->getContentSize().width / 2.0); //摩擦力 material.friction = 0; // 剛体の回転を無効にする body->setRotationEnable(false); // カテゴリをPLAYERにセットする body->setCategoryBitmask(static_cast<int>(Stage::TileType::PLAYER)); // 壁とのみ衝突する body->setCollisionBitmask(static_cast<int>(Stage::TileType::WALL)); // 全ての剛体と接触判定を行う body->setContactTestBitmask(INT_MAX); this->setPhysicsBody(body); // 初期加速度を設定する _acceleration = INITIAL_ACCELERATION; this->scheduleUpdate(); return true; }
void SceneGameMain::createBirdBox() //鸟 { Size visibleSize = Director::getInstance()->getWinSize(); pBird = Sprite::create("bird.png"); pBird->setTag(0); auto body = PhysicsBody::createBox(Size(80, 60)); pBird->setPhysicsBody(body); body->setRotationEnable(false); body->setDynamic(true); pBird->setPosition(Point(visibleSize.width*0.5,visibleSize.height*0.5)); this->addChild(pBird,1,7); }
bool Player::init() { this->initWithFile("mario_mini.png"); //this->setScale(0.2f); auto pb = PhysicsBody::createBox(Size(this->getContentSize().width - 10, this->getContentSize().height-20)); //log("%f,%f",size.width,size.height); pb->setEnable(true); pb->setMass(1.0f); pb->setVelocity(Vect(0,1)); pb->setVelocityLimit(100.0); pb->setRotationEnable(true); pb->setAngularVelocityLimit(150.0f); pb->setDynamic(true); pb->setContactTestBitmask(0xFFFFFFFF); this->setPosition(Point(228,708)); this->setPhysicsBody(pb); return true; }
Player * Player::create() { Size visibleSize = Director::getInstance()->getVisibleSize(); Point origin = Director::getInstance()->getVisibleOrigin(); Player * player = new Player(); if (player && player->initWithFile("GameScreen/player.png")) { player->autorelease(); player->initPlayer(); player->setPosition(Point(visibleSize.width / 2 + origin.x, visibleSize.height + origin.y)); auto player1Body = PhysicsBody::createBox(player->getContentSize(), PhysicsMaterial(0, 0, 0)); player1Body->setCollisionBitmask(0x000001); player1Body->setRotationEnable(false); //player1Body->setCategoryBitmask(0x02); player1Body->setContactTestBitmask(true); player1Body->setTag(10); player1Body->setDynamic(true); //player1Body->setVelocity(Vect(100, 247)); player1Body->setLinearDamping(0); //player1Body->applyForce(Vect(100, 245)); //Assign the body to sprite player->setPhysicsBody(player1Body); player->setPosition(Vec2(origin.x + visibleSize.width / 2, origin.y + visibleSize.height / 4)); player->setAnchorPoint(Point(0.5f, 0.5f)); player->setScale(.8); //player->pMovement(player); //this->addChild(sprite); return player; } CC_SAFE_DELETE(player); return NULL; }
cocos2d::PhysicsBody* RubeBody::createPhysicsBody() { if (0 == fixtures.size()) { return nullptr; } auto pb = cocos2d::PhysicsBody::create(); pb->setRotationEnable(this->getRotationEnable()); if (this->getBodyType() == 0) { //0 = static, 1 = kinematic, 2 = dynamic pb->setDynamic(false); } for (auto fixture: fixtures) { auto shape = fixture->getShape(this->scale); pb->addShape(shape); } pb->setLinearDamping(this->getLinearDamping()); pb->setAngularDamping(this->getAngularDamping()); //pb->setCollisionBitmask(1); //pb->setCategoryBitmask(1); return pb; }
void BallSprite::InitSprite() { auto body = PhysicsBody::createCircle(this->getContentSize().width / 2, PhysicsMaterial(1, 1, 0)); if (body != nullptr) { body->setDynamic(true); body->setRotationEnable(true); if (getTeam() == 1) { body->setCollisionBitmask(HERO_BALL_COLLISION_BITMASK); } else { body->setCollisionBitmask(MONSTER_BALL_COLLISION_BITMASK); } body->setContactTestBitmask(true); this->setPhysicsBody(body); } this->setScale(_ballSize); }
void Guard::InitSprite() { auto body = PhysicsBody::createBox(this->getContentSize(), PhysicsMaterial(100, 1, 1)); body->setDynamic(true); body->setRotationEnable(false); body->setCollisionBitmask(MONSTER_BODY_COLLISION_BITMASK); body->setContactTestBitmask(true); this->setPhysicsBody(body); this->setUserData(this); hpBgSpr = Sprite::create("Monster/hpBg.png"); this->addChild(hpBgSpr, ZINDEX_MONSTER_HP); hpCurrentSpr = Sprite::create("Monster/hpCurrent.png"); this->addChild(hpCurrentSpr, ZINDEX_MONSTER_HP); auto origin = Director::getInstance()->getVisibleOrigin(); hpBgSpr->setPosition(Vec2(this->getContentSize().width / 2, this->getPosition().y + this->getContentSize().height + hpBgSpr->getContentSize().height / 2)); hpCurrentSpr->setPosition(hpBgSpr->getPosition()); hpBgSpr->setOpacity(0); hpCurrentSpr->setOpacity(0); }
void Brick::InitSprite() { auto body = PhysicsBody::createBox(this->getContentSize(), PhysicsMaterial(100, 1, 1)); body->setDynamic(false); body->setRotationEnable(false); switch (_brickTypeID) { case BRICK_SINGLE_ID: body->setCollisionBitmask(SINGLE_BRICK_COLLISION_BITMASK); break; case BRICK_DOUBLE_ID: body->setCollisionBitmask(DOUBLE_BRICK_COLLISION_BITMASK); break; case BRICK_SOLID_ID: body->setCollisionBitmask(SOLID_BRICK_COLLISION_BITMASK); break; } body->setContactTestBitmask(true); this->setPhysicsBody(body); }
block::block( cocos2d::Layer* layer ) { visibleSize = Director::getInstance( )->getVisibleSize( ); origin = Director::getInstance( )->getVisibleOrigin( ); block1 = Sprite::create( "labki.png" ); Vec2 sizeblock = block1->getContentSize(); block1->setScale(visibleSize.width/sizeblock.x*0.25,visibleSize.height/sizeblock.y*0.25); //block1->setPosition( Vec2( visibleSize.width / 2 + origin.x, visibleSize.height / 2 + origin.y ) ); block1->setPosition(Vec2(visibleSize.width/(float)((float)3/(float)cocos2d::RandomHelper::random_int(4,8))+visibleSize.width,(visibleSize.height*1))); auto block_body = PhysicsBody::createBox(block1->getContentSize(), PhysicsMaterial(0.2f, 0.0f, 0.0f) ); block1->setPhysicsBody(block_body); block_body->setRotationEnable(true); block_body->setContactTestBitmask( true ); block_body->setTag(2); layer->addChild( block1, 10 ); layer1=layer; }
void Game2::createtrap(double x, double y, double z, std::string name){ //create trap Trap *_trampa = Trap::create(); _trampa->setPosition3D(Vec3(x, y, z)); _trampa->setName(name); _trampas->addObject(_trampa); //create body auto _body = PhysicsBody::createEdgeBox(Size(35, 20), PhysicsMaterial(10, 0, 0.9f), 1.0, Vec2(-5, 10)); _body->setContactTestBitmask(true); _body->setDynamic(false); _body->setRotationEnable(false); _body->addMass(30.0); _body->addMoment(2.0); _body->setLinearDamping(0.8f); _trampa->setPhysicsBody(_body); addChild(_trampa); }
void Game2::createplatform(double x, double y, double z, double scale, double bodyscalex, double bodyscaley, double xoffset, double yoffset, std::string name){ //create platform Platform *_plataforma = Platform::create(); _plataforma->setScaleX(scale); _plataforma->setScaleY(10); _plataforma->setName(name); _plataforma->setPosition3D(Vec3(x, y, z)); _plataformas->addObject(_plataforma); //create body auto _body = PhysicsBody::createEdgeBox(Size(bodyscalex, bodyscaley), PhysicsMaterial(10, 0, 0.9f), 1.0, Vec2(xoffset, yoffset)); _body->setContactTestBitmask(true); _body->setDynamic(false); _body->setRotationEnable(false); _body->addMass(30.0); _body->addMoment(2.0); _body->setLinearDamping(0.8f); _plataforma->setPhysicsBody(_body); addChild(_plataforma); }
bool Valkyrie::init(const std::string& name){ if (!Armature::init(name)){ return false; } CCLOG("Valkyrie init"); battle_state = BattleState::NORMALSTATE; action_state = ActionState::STANDBY; //phy auto body = PhysicsBody::createBox(Size(getContentSize().width * SCALE,getContentSize().height * SCALE),common_material); body->setCategoryBitmask(VALKYRIE_CATA_MASK); body->setContactTestBitmask(VALKYRIE_CONTACT_MASK); body->setCollisionBitmask(0xFFFFFFFF); m_range = PhysicsShapeCircle::create(100); m_range->setCategoryBitmask(RANGE_CATA_MASK); m_range->setContactTestBitmask(RANGE_CONTACT_MASK); m_range->setCollisionBitmask(RANGE_COLLISION_MASK); m_range->setMass(0.1f); m_range->setTag(911); body->addShape(m_range); CCLOG("m_range 's tag is %d",m_range->getTag()); body->setRotationEnable(false); body->setDynamic(true); setPhysicsBody(body); setScale(SCALE); return true; }
bool GameScene_Chipmunk::init() { if ( !Scene::init() ) { return false; } //画面サイズサイズを取得 auto window_size = Director::getInstance()->getWinSize(); //床 auto floor = Sprite::create(); floor->setPosition(window_size.width/2, 0); floor->setTextureRect(Rect(0,0,window_size.width,100)); floor->setColor(Color3B(255,255,255)); floor->setTag(spriteType::kFloor); _floor = floor->getSpriteFrame()->getRect(); auto left_wall = Sprite::create(); left_wall->setPosition(10, window_size.height/2); left_wall->setTextureRect(Rect(0,0,10,window_size.height*2)); left_wall->setColor(Color3B(255,255,255)); _leftwall = left_wall->getSpriteFrame()->getRect(); auto right_wall = Sprite::create(); right_wall->setPosition(window_size.width, window_size.height/2); right_wall->setTextureRect(Rect(0,0,10,window_size.height*2)); right_wall->setColor(Color3B(255,255,255)); _rightwall = right_wall->getSpriteFrame()->getRect(); _ceil = Rect(window_size.width/2, window_size.height - 200, window_size.width, 20); //wall->setRotation(10.0f); //反発係数と摩擦係数 auto material = PHYSICSBODY_MATERIAL_DEFAULT; material.restitution = 0.0f; material.friction = 1.0f; auto pWall = PhysicsBody::createBox(floor->getContentSize(), material); //重力干渉を受けるか pWall->setDynamic(false); //回転させるか pWall->setRotationEnable(false); pWall->setContactTestBitmask(true); floor->setPhysicsBody(pWall); addChild(floor); auto pright_wall = PhysicsBody::createBox(right_wall->getContentSize(), material); //重力干渉を受けるか pright_wall->setDynamic(false); //回転させるか pright_wall->setRotationEnable(false); right_wall->setPhysicsBody(pright_wall); addChild(right_wall); auto pleft_wall = PhysicsBody::createBox(left_wall->getContentSize(), material); //重力干渉を受けるか pleft_wall->setDynamic(false); //回転させるか pleft_wall->setRotationEnable(false); left_wall->setPhysicsBody(pleft_wall); addChild(left_wall); //event listener //イベントリスナー作成 auto listener = EventListenerTouchAllAtOnce::create(); //イベントを飲み込むかどうか //listener->setSwallowTouches(true); //タッチメソッド設定 listener->onTouchesBegan = CC_CALLBACK_2(GameScene_Chipmunk::onTouchesBegan, this); listener->onTouchesMoved = CC_CALLBACK_2(GameScene_Chipmunk::onTouchesMoved, this); listener->onTouchesEnded = CC_CALLBACK_2(GameScene_Chipmunk::onTouchesEnded, this); //this->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener, this); //優先度100でディスパッチャーに登録 this->getEventDispatcher()->addEventListenerWithFixedPriority(listener, 100); //衝突検知 auto plistener = EventListenerPhysicsContact::create(); plistener->onContactBegin = CC_CALLBACK_1(GameScene_Chipmunk::onContactBegin, this); this->getEventDispatcher()->addEventListenerWithSceneGraphPriority(plistener, this); auto plistenerend = EventListenerPhysicsContact::create(); plistenerend->onContactPostSolve = CC_CALLBACK_2(GameScene_Chipmunk::onContactPostSolve, this); this->getEventDispatcher()->addEventListenerWithSceneGraphPriority(plistenerend, this); this->fillBoll(); scheduleUpdate(); return true; }
void GamePlaying::ObjectLayerParse(TMXObjectGroup* og,int& priority ){ if(og==nullptr){ return; } auto objs = og->getObjects(); int assertcount=0; for(auto& obj : objs){ ValueMap& map = obj.asValueMap(); std::string type = map["type"].asString(); Vec2 pos(map["x"].asFloat(),map["y"].asFloat()); if(type=="cuttable"){ PossesAndUvs pau=GetPolygonVerticesInfo(map); if(!pau.posses.empty()){ ///ポリゴンの生成 MyPolygon p; std::vector<Vec2>& v = p.vertices; for(auto& pos : pau.posses){ v.push_back(pos); } std::vector<Vec2>& uvs=p.uvs; for(auto& uv : pau.uvs){ uvs.push_back(uv); } p.position=pos+pau.center; auto grave=PhysicsBody::createPolygon(&v[0],v.size(),_pm); DrawNodeWithTex* node = DrawNodeWithTex::create(); auto tex=Director::getInstance()-> getTextureCache()-> addImage("ice.png"); //addImage("soniko_t.png"); node->SetTexture(tex->getName()); node->drawPolygonT(&v[0],&uvs[0],v.size(),cocos2d::Color4F::YELLOW,0,Color4F::WHITE); node->setPosition(p.position); node->setPhysicsBody(grave); node->setTag(_tag++); DrawNode* border=DrawNode::create(); border->drawPolygon(&v[0],v.size(),Color4F(0,0,0,0.0),BORDER_WIDTH,Color4F::WHITE); node->addChild(border); p.tag=node->getTag(); p.texId = tex->getName(); p.wasCut=false; _polygons.push_back(p); addChild(node,priority++); } }else if(type=="scaffold"){ Size size; size.width=map["width"].asInt(); size.height=map["height"].asInt(); PhysicsMaterial bm; bm.density=1.0f; bm.friction=0.3f; bm.restitution=0.0f; auto board=PhysicsBody::createBox(size,bm); auto boardnode = Sprite::create("marble.png",Rect(0,0,size.width,size.height)); auto rot = map.find("rotation"); float angle=0; if(rot!=map.end()){ angle = rot->second.asFloat(); } boardnode->setPhysicsBody(board); board->setDynamic(false); board->setRotationEnable(false); //board->setRotationOffset(angle); boardnode->setPosition(pos+Vec2(size.width/2,size.height/2).rotate(Vec2::forAngle(-angle*M_PI/180.0f))); boardnode->setRotation(angle); addChild(boardnode); } } }
// on "init" you need to initialize your instance bool Game2::init() { ////////////////////////////// // 1. super init first if ( !Layer::init() ) { return false; } Size visibleSize = Director::getInstance()->getVisibleSize(); Vec2 origin = Director::getInstance()->getVisibleOrigin(); def = CCUserDefault::sharedUserDefault(); #pragma region Creaciondevariables _chara = Character::create(); addChild(_chara); Point center = Point(visibleSize.width / 2 + origin.x, visibleSize.height / 2 + origin.y); i = 1; j = 1; k = 1; free = true; moverderecha = false; moverizq = false; arriba = false; rotar = false; bool abrir = false; puntuacion = 0; _chara->vidas = def->getIntegerForKey("vidas"); #pragma endregion muerto = false; camera = Camera::createPerspective(60,visibleSize.width / visibleSize.height,1, 1000); camera->setPosition3D(Vec3(0, 0, 500)); _plataformas = Array::create(); _plataformas->retain(); _trampas = Array::create(); _trampas->retain(); _chara->setPosition3D(Vec3(90.0, 90.0, 0.0)); _chara->setScale(1.0); camera->lookAt(Vec3(0, 0, 0), Vec3(0, 1, 0)); addChild(camera); camera->setScale(3); createplatform(200.0, -300.0, 0.0, 70.0, 1400, 1, 350, 65, "plataforma"); createtrap(300,-230,0,"trampa"); createtrap(335,-230,0,"trampa"); createtrap(370,-230,0,"trampa"); createplatform(600, -50, 0.0, 8.0, 160, 1, 32, 65, "plataforma"); createtrap(700,-230,0,"trampa"); createtrap(735,-230,0,"trampa"); createtrap(770,-230,0,"trampa"); createtrap(805,-230,0,"trampa"); createtrap(840,-230,0,"trampa"); createtrap(875,-230,0,"trampa"); createtrap(910,-230,0,"trampa"); createtrap(945,-230,0,"trampa"); createtrap(980,-230,0,"trampa"); createplatform(1000, -50, 0.0, 8.0, 160, 1, 32, 65, "plataforma"); createplatform(1500, -50, 0.0, 8.0, 160, 1, 32, 65, "plataforma"); createtrap(1600,15,0,"trampa"); createplatform(2000, -50, 0.0, 8.0, 160, 1, 32, 65, "plataforma"); createplatform(2500, -300.0, 0.0, 85.0, 1700, 1, 350, 65, "plataforma"); createtrap(2700,-230,0,"trampa"); createtrap(2735,-230,0,"trampa"); createtrap(2770,-230,0,"trampa"); createtrap(2805,-230,0,"trampa"); createtrap(2840,-230,0,"trampa"); createtrap(2875,-230,0,"trampa"); createtrap(2910,-230,0,"trampa"); createtrap(2945,-230,0,"trampa"); createtrap(2980,-230,0,"trampa"); createtrap(3200,-230,0,"trampa"); createtrap(3310,-230,0,"trampa"); createtrap(3520,-230,0,"trampa"); createtrap(3700,-230,0,"trampa"); createplatform(4200.0, -300.0, 0.0, 8.0, 160, 1, 32, 65, "plataforma"); esfera = Sprite3D::create("char/esfera.c3t"); esfera->setPosition3D(Vec3(4300.0, 200.0, 0)); auto _bodyesf = PhysicsBody::createCircle(128, PHYSICSBODY_MATERIAL_DEFAULT, Vec2(-80, -80)); esfera->setScale(4); _bodyesf->setContactTestBitmask(true); _bodyesf->setDynamic(false); _bodyesf->setRotationEnable(false); _bodyesf->addMass(30.0); esfera->setName("esfera"); esfera->setPhysicsBody(_bodyesf); addChild(esfera); auto keyboardListener = EventListenerKeyboard::create(); keyboardListener->onKeyPressed = CC_CALLBACK_2(Game2::onKeyPresed, this); keyboardListener->onKeyReleased = CC_CALLBACK_2(Game2::onKeyReleased, this); this->getEventDispatcher()->addEventListenerWithSceneGraphPriority(keyboardListener, this); this->scheduleUpdate(); //Physics auto contactListener = EventListenerPhysicsContact::create(); contactListener->onContactBegin = CC_CALLBACK_1(Game2::onContactBegin, this); this->getEventDispatcher()->addEventListenerWithSceneGraphPriority(contactListener, this); hud = new HUD(_chara->vidas, true,800); this->addChild(hud); //xinput auto console = Director::getInstance()->getConsole(); //console->listenOnTCP(6113); struct Console::Command changeforce = { "changeforce", "Cambia la fuerza del salto", [this](int fd, const std::string& args) { if (args.length() == 0) { } else { createplatform((int)args[0], (int)args[1], (int)args[2], (int)args[3], (int)args[4], (int)args[5], (int)args[6], (int)args[7], args); } } }; console->addCommand(changeforce); 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); }
// on "init" you need to initialize your instance bool Practice1::init() { ////////////////////////////// // 1. super init first if (!Layer::init()) { return false; } Size visibleSize = Director::getInstance()->getVisibleSize(); Vec2 origin = Director::getInstance()->getVisibleOrigin(); ///////////////////////////// // 2. add a menu item with "X" image, which is clicked to quit the program // you may modify it. // add a "close" icon to exit the progress. it's an autorelease object auto closeItem = MenuItemImage::create( "CloseNormal.png", "CloseSelected.png", CC_CALLBACK_1(Practice1::menuCloseCallback, this)); closeItem->setPosition(Vec2(origin.x + visibleSize.width - closeItem->getContentSize().width / 2, origin.y + closeItem->getContentSize().height / 2)); // create menu, it's an autorelease object auto menu = Menu::create(closeItem, NULL); menu->setPosition(Vec2::ZERO); this->addChild(menu, 1); ///////////////////////////// // 3. add your codes below... auto label = Label::createWithTTF("練習シーン1", "fonts/07やさしさゴシック.ttf", 24); label->setAnchorPoint(Vec2(0.0f, 0.0f)); label->setPosition(origin.x, origin.y + visibleSize.height); this->addChild(label); nowCount = Label::createWithTTF("現在のカウント: 0", "fonts/07やさしさゴシック.ttf", 24); nowCount->setPosition(origin.x + visibleSize.width/2, origin.y + visibleSize.height/2); this->addChild(nowCount); count = 0; // add this scene to update schedule //this->scheduleUpdate(); //this->schedule(schedule_selector(Practice1::updateByTimer), 1.0f); // create field // 空のスプライトを用意 float fieldWidth = visibleSize.width / 2; float fieldHeight = visibleSize.height + 100.0f; float fieldBlockWidth = fieldWidth / 8; const int fieldObjectNumber = 3; Rect fieldSizes[fieldObjectNumber] = { { 0, 0, fieldBlockWidth, fieldHeight }, { 0, 0, fieldWidth, fieldBlockWidth }, { 0, 0, fieldBlockWidth, fieldHeight }, }; Point fieldPoints[fieldObjectNumber] = { { origin.x + visibleSize.width / 2 - fieldWidth/2 - fieldBlockWidth, origin.x + visibleSize.height }, { origin.x + visibleSize.width / 2 - fieldWidth / 2, origin.x + visibleSize.height }, { origin.x + visibleSize.width / 2 + fieldWidth / 2, origin.x + visibleSize.height }, }; //for (int i = 0; i < fieldObjectNumber; ++i) for (int i = 0; i < 1; ++i) { Sprite* field = Sprite::create("HelloWorld.png"); //Sprite* field = Sprite::create(); //field->setColor(Color3B::WHITE); field->setAnchorPoint(Vec2(0, 1.0)); field->setTextureRect(fieldSizes[i]); field->setPosition(fieldPoints[i]); //auto fieldPhysics = PhysicsBody::createBox(fieldSizes[i].size); auto fieldPhysics = PhysicsBody::createBox(Size(field->getContentSize().width, field->getContentSize().height)); fieldPhysics->setDynamic(false); fieldPhysics->setRotationEnable(false); field->setPhysicsBody(fieldPhysics); this->addChild(field); } auto sprite2 = Sprite::create("EDGE(after).png"); sprite2->setPosition(Vec2(visibleSize.width / 2 + origin.x, visibleSize.height/2 + origin.y)); auto sprite2Physics = PhysicsBody::createBox(Size(sprite2->getContentSize().width, sprite2->getContentSize().height)); sprite2Physics->setDynamic(true); sprite2Physics->setRotationEnable(false); sprite2->setPhysicsBody(sprite2Physics); this->addChild(sprite2); return true; }