コード例 #1
0
ファイル: Bullet.cpp プロジェクト: eiaeii/StarDestroyer
bool Bullet::init(int iType)
{
	if (!Sprite::init())
	{
		return false;
	}
	if (iType == BulletType::Bullet_Player_main)
	{
		this->initWithFile("bullet/pet_bullet.png");
		this->setTag(ObjectType::TYPE_PLAYER_BULLET);
		auto body = PhysicsBody::createCircle(5.0f);
		body->setCategoryBitmask(Gategorybitmask::GATEGORYBITMASK_PLAYER);
		body->setContactTestBitmask(0xFFFFFFFF);
		body->setCollisionBitmask(Collisionbitmask::COLLISIONBITMASK_PLAYER_BULLET);
		this->setPhysicsBody(body);
		body->setVelocity(Vec2(0, 500));
	}
	else if (iType == BulletType::Bullet_Player_second)
	{
		this->initWithFile("bullet/assisent1_01.png");
		this->setTag(ObjectType::TYPE_PLAYER_BULLET);
		auto body = PhysicsBody::createCircle(5.0f);
		body->setCategoryBitmask(Gategorybitmask::GATEGORYBITMASK_PLAYER);
		body->setContactTestBitmask(0xFFFFFFFF);
		body->setCollisionBitmask(Collisionbitmask::COLLISIONBITMASK_PLAYER_BULLET);
		this->setPhysicsBody(body);
		body->setVelocity(Vec2(0, 1000));
	}
	return true;
}
コード例 #2
0
ファイル: GameLayer.cpp プロジェクト: ahjsrhj/MyBird
void GameLayer::createPips() {
	for (int i = 0; i < 2; i++) {
		auto pipeUp = Sprite::createWithSpriteFrame(
			AtlasLoader::getInstance()->getSpriteFrameByName("pipe_up"));
		auto pipeDown = Sprite::createWithSpriteFrame(
			AtlasLoader::getInstance()->getSpriteFrameByName("pipe_down"));

		pipeUp->setPosition(Vec2(0, -(PIPE_HEIGHT + PIPE_DISTANCE) / 2));
		pipeDown->setPosition(Vec2(0, (PIPE_HEIGHT + PIPE_DISTANCE) / 2));

		//创建物理属性
		auto pipeUpBody = PhysicsBody::createBox(pipeUp->getContentSize(), PHYSICSSHAPE_MATERIAL_DEFAULT, Vec2::ZERO);
		//设置为静态刚体
		pipeUpBody->setDynamic(false);
		pipeUpBody->getShape(0)->setRestitution(0.0f);
		//设置刚体密度
		pipeUpBody->getShape(0)->setDensity(1.0f);
		pipeUpBody->setCategoryBitmask(OBST_MASK);
		pipeUpBody->setCollisionBitmask(BIRD_MASK | OBST_MASK);
		pipeUpBody->setContactTestBitmask(BIRD_MASK | OBST_MASK);
		pipeUp->setPhysicsBody(pipeUpBody);

		auto pipeDownBody = PhysicsBody::createBox(pipeDown->getContentSize(), PHYSICSSHAPE_MATERIAL_DEFAULT, Vec2::ZERO);
		//设置为静态刚体
		pipeDownBody->setDynamic(false);
		pipeDownBody->getShape(0)->setRestitution(0.0f);
		pipeDownBody->getShape(0)->setDensity(1.0f);
		//设置碰撞掩码
		pipeDownBody->setCategoryBitmask(OBST_MASK);
		pipeDownBody->setCollisionBitmask(BIRD_MASK | OBST_MASK);
		pipeDownBody->setContactTestBitmask(BIRD_MASK | OBST_MASK);

		pipeDown->setPhysicsBody(pipeDownBody);

		auto singlePipe = Node::create();
		singlePipe->addChild(pipeUp);
		singlePipe->addChild(pipeDown);

		if (i == 0) {
			singlePipe->setPosition(Vec2(SCREEN_WIDTH * 2 + PIPE_WIDHT / 2, 250));
		} else {
			singlePipe->setPosition(Vec2(SCREEN_WIDTH * 2.5f + PIPE_WIDHT, this->getRandomHeight()));
		}

		singlePipe->setTag(PIPE_NEW);

		this->addChild(singlePipe);
		this->pipes.pushBack(singlePipe);
	}

}
コード例 #3
0
ファイル: Fighter.cpp プロジェクト: shenjingithub/Lost-Roads
Fighter* Fighter::createWithSpriteFrameName(const char* spriteFrameName)
{
	Fighter* fighter=new Fighter();
	
	if(fighter&&fighter->initWithSpriteFrameName(spriteFrameName)){
		fighter->autorelease();

		ParticleSystem *ps=ParticleSystemQuad::create("particle/fire.plist");
	
		//·É»úÏÂÃæ
		ps->setPosition(Vec2(fighter->getContentSize().width/2,0));
	//	ps->setRotation(180.0f);
		ps->setScale(0.5f);
		fighter->addChild(ps);
		////////////////////////////////////////////////////////////////////
		Vec2 verts[]={
			Vec2(-43.5,15.5),
			Vec2(-23.5,33),
			Vec2(28.5,34),
			Vec2(48,17.5),
			Vec2(0,-39.5)};

		auto body=PhysicsBody::createPolygon(verts,5);

		body->setCategoryBitmask(0x01);
		body->setCollisionBitmask(0x02);
		body->setContactTestBitmask(0x01);

		fighter->setPhysicsBody(body);
		return fighter;

	}
	CC_SAFE_DELETE(fighter);
	return nullptr;
}
コード例 #4
0
ファイル: MayBay.cpp プロジェクト: tinyit/planeTap
MayBay * MayBay::Create(){
    MayBay * obj = new MayBay();
    auto SpFrameCache = SpriteFrameCache::getInstance();
    SpFrameCache->addSpriteFramesWithFile("plane/plane.plist");
    if(!obj->initWithSpriteFrame(SpFrameCache->getSpriteFrameByName("planeRed1.png"))){
        CCLOG("Cant not create maybay");
    }
    else{
        Vector<SpriteFrame*> vecFrm(3);
        char str[50]={0};
        for(int i=1; i<=3 ;i++){
            sprintf(str,"planeRed%d.png",i);
            auto spFrm = SpFrameCache->getSpriteFrameByName(str);
            vecFrm.pushBack(spFrm);
            
        }
        auto animation = Animation::createWithSpriteFrames(vecFrm,0.1f);
        auto ani= Animate::create(animation);
        obj->runAction(RepeatForever::create(ani));
    }
    auto physic = PhysicsBody::createBox(Size(Vec2(obj->getContentSize().width-10,obj->getContentSize().height-10)),PhysicsMaterial(1,0,1));
    physic->setCollisionBitmask(0x001);
    physic->setCategoryBitmask(0x001);
    physic->setContactTestBitmask(true);
    physic->setTag(10);
    obj->setPhysicsBody(physic);
    obj->getPhysicsBody()->setGravityEnable(false);
    obj->setFly();
    return obj;
}
コード例 #5
0
ファイル: Player.cpp プロジェクト: Tetu-fs/mirumirrormini
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;
}
コード例 #6
0
ファイル: Balloon.cpp プロジェクト: ricky-flyguy/RMZ_2014
Balloon* Balloon::createWithForce(Point* pos, Point* force)
{
    Balloon* b = new Balloon();

    if (b && b->initWithFile("balloon.png"))
    {
        // Set auto release.
        b->autorelease();
        // Set Scale
        b->setScale(0.15f);

        // Set Position
        b->setPosition(*pos);

        // CHANGE TO A DEFINED NUMBER AFTER
        //this->setTag(0);
        auto body = PhysicsBody::createCircle(b->getBoundingBox().size.width/2.5f);
        body->setMass(1);
        body->setDynamic(true);
        body->setContactTestBitmask(2);
        body->setCategoryBitmask(0x01); //0001
        b->setPhysicsBody(body);

        // give forces
        b->getPhysicsBody()->applyImpulse(*force);

        b->schedule(schedule_selector(Balloon::update));

        return b;
    }
    CC_SAFE_DELETE(b);

    return NULL;
}
コード例 #7
0
ファイル: Stage.cpp プロジェクト: ZenexHentai/KawazJet
Sprite* Stage::addPhysicsBody(cocos2d::TMXLayer *layer, cocos2d::Vec2 &coordinate)
{
    // タイル1枚の大きさを取り出す
    auto tileSize = _tiledMap->getTileSize();
    
    // タイルのスプライトを取り出す
    auto sprite = layer->getTileAt(coordinate);
    if (sprite) {
        // タイルのIDを取り出す
        auto gid = layer->getTileGIDAt(coordinate);
        // タイルのプロパティをmapで取り出す
        auto property = _tiledMap->getPropertiesForGID(gid);
        if (property.isNull() || property.getType() != Value::Type::MAP) {
            return nullptr;
        }
        auto properties = property.asValueMap();
        // プロパティの中からcategoryの値をintとして取り出す
        auto category = properties.at("category").asInt();
        
        auto material = PhysicsMaterial();
        material.friction = 0;
        material.restitution = 0.1;
        
        // 剛体を設置する
        auto physicsBody = PhysicsBody::createBox(sprite->getContentSize(), material);
        // 剛体を固定する
        physicsBody->setDynamic(false);
        // 剛体にカテゴリをセットする
        physicsBody->setCategoryBitmask(category);
        // 剛体と接触判定を取るカテゴリを指定する
        physicsBody->setContactTestBitmask(static_cast<int>(TileType::PLAYER));
        // 剛体と衝突を取るカテゴリを指定する
        physicsBody->setCollisionBitmask(static_cast<int>(TileType::PLAYER));
        
        // アニメーションを付ける
        // プロパティにanimationの値があったら
        if (!properties["animation"].isNull()) {
            auto animationSprite = properties["animation"].asString();
            auto animationCount = properties["animationCount"].asInt();
        
            sprite->removeFromParent();
            this->addChild(sprite);
            
            Vector<SpriteFrame *> frames;
            auto scale = 0.5;
            for (int i = 0; i < animationCount; ++i) {
                auto frame = SpriteFrame::create(animationSprite, Rect(tileSize.width * i * scale, 0, tileSize.width * scale, tileSize.height * scale));
                frames.pushBack(frame);
            }
            auto animation = Animation::createWithSpriteFrames(frames);
            animation->setDelayPerUnit(0.15);
            sprite->runAction(RepeatForever::create(Animate::create(animation)));
        }
        sprite->setAnchorPoint(Vec2::ANCHOR_MIDDLE);
        sprite->setPhysicsBody(physicsBody);
        
        return sprite;
    }
    return nullptr;
}
コード例 #8
0
ファイル: Player.cpp プロジェクト: raa0121/mirumirrormini
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;
}
コード例 #9
0
bool RoleSupplyDoubleGun::init() {
	bool bRet = false;
	do {
		CC_BREAK_IF(!Sprite::initWithSpriteFrameName("ufo1.png"));

		// set physical body
		auto body = PhysicsBody::createBox(getContentSize());
		body->setGroup(PHYSICAL_BODY_SUPPLY_GROUP);
		body->setCategoryBitmask(PHYSICAL_BODY_SUPPLY_BITMASK_CATEGORY);
		body->setContactTestBitmask(PHYSICAL_BODY_SUPPLY_BITMASK_CONTACT_TEST);
		body->setCollisionBitmask(PHYSICAL_BODY_SUPPLY_BITMASK_COLLISION);
		setPhysicsBody(body);

		// set position
		auto bigBoomSize = getContentSize();
		auto winSize = Director::getInstance()->getWinSize();
		int minX = bigBoomSize.width/2;
		int maxX = winSize.width-bigBoomSize.width/2;
		int rangeX = maxX-minX;
		int actualX = (rand()%rangeX)+minX;
		setPosition(Point(actualX, winSize.height+bigBoomSize.height/2));

		// run action
		auto move1 = MoveBy::create(0.5, Point(0, -150));
		auto move2 = MoveBy::create(0.3, Point(0, 100));
		auto move3 = MoveBy::create(1.0, Point(0, 0-winSize.height-bigBoomSize.height/2));
		auto actionDone = RemoveSelf::create(true);
		auto sequence = Sequence::create(move1, move2, move3, actionDone, nullptr);
		runAction(sequence);

		bRet = true;
	} while (0);

	return bRet;
}
コード例 #10
0
void Character::InitCharacterSprite(char* char_name){
    Isbomb=false;
    Char_name=char_name;
    this->character=CCSprite::create(char_name);
    
    
    
    storm = Sprite::create("k.png");
    storm->setVisible(false);
    storm->setScale(0.6f);
    auto stormBody = PhysicsBody::createBox( storm->getContentSize());
    // stormBody->setDynamic(false);
    stormBody->setCollisionBitmask( STORM_COLLISION_BITMASK );
    stormBody->setCategoryBitmask(8);
    stormBody->setContactTestBitmask(1);
    //    //stormBody->
    storm->setPhysicsBody(stormBody);
    //stormBody->
    
    
    numberOfBomb=0;
    
    this->addChild(storm);
    this->addChild(character);
}
コード例 #11
0
ファイル: DamagePowerUp.cpp プロジェクト: Redruk/MadShooter
bool DamagePowerUp::init() 
{
    if ( !PowerUp::init() ){
        return false;
    }
    
    std::ostringstream stream;
    stream << "powerups/DP" << ((int)_damageLevel) << ".png";
    setTexture(stream.str());
    setOpacity(0);
    auto fadeIn = FadeIn::create(1.0f);
    runAction(fadeIn);
    
    // [[-- sets the physic body
    auto physicBody = PhysicsBody::createBox( Size( getContentSize().width , getContentSize().height ) ,
                                              PhysicsMaterial(0, 0.02, 0) );
    physicBody->setCategoryBitmask(Type::POWERUP);
    physicBody->setCollisionBitmask( Entity::Type::SHIP );
    physicBody->setContactTestBitmask(Type::SHIP);
    
    //physicBody->setTag();
    setPhysicsBody( physicBody );
    // --]]
    
    _stats.damage = 5;
    
    // Tells Cocos2d-x to call the Node's update function
    scheduleUpdate();
    
    return true;
}
コード例 #12
0
ファイル: GameLayer.cpp プロジェクト: whumr/bird
void GameLayer::createPips() {
    // Create the pips
    for (int i = 0; i < PIP_COUNT; i++) {
        Size visibleSize = Director::getInstance()->getVisibleSize();
        Sprite *pipUp = Sprite::createWithSpriteFrame(AtlasLoader::getInstance()->getSpriteFrameByName("pipe_up"));
        Sprite *pipDown = Sprite::createWithSpriteFrame(AtlasLoader::getInstance()->getSpriteFrameByName("pipe_down"));
        Node *singlePip = Node::create();
        
        // bind to pair
        pipDown->setPosition(0, PIP_HEIGHT + PIP_DISTANCE);
		singlePip->addChild(pipDown, 0, DOWN_PIP);
        singlePip->addChild(pipUp, 0, UP_PIP);
        singlePip->setPosition(visibleSize.width + i*PIP_INTERVAL + WAIT_DISTANCE, this->getRandomHeight());
		auto body = PhysicsBody::create();
		auto shapeBoxDown = PhysicsShapeBox::create(pipDown->getContentSize(),PHYSICSSHAPE_MATERIAL_DEFAULT, Point(0, PIP_HEIGHT + PIP_DISTANCE));
		body->addShape(shapeBoxDown);
		body->addShape(PhysicsShapeBox::create(pipUp->getContentSize()));
		body->setDynamic(false);
		
		//ÉèÖÃÅöײ
		body->setCategoryBitmask(ColliderTypePip);  
		body->setCollisionBitmask(ColliderTypeBird);  
		body->setContactTestBitmask(ColliderTypeBird);  
		
		singlePip->setPhysicsBody(body);
        singlePip->setTag(PIP_NEW);
        
        this->addChild(singlePip);
        this->pips.push_back(singlePip);
    }
}
コード例 #13
0
ファイル: Brick.cpp プロジェクト: amongpixels/arkanoid
void arkanoid::Brick::initPhysicsBody() {
  auto physicsBody = cocos2d::PhysicsBody::createBox(this->getContentSize(), arkanoid::Brick::material);
  physicsBody->setCategoryBitmask(COLLISION_GROUP_BRICK);
  physicsBody->setCollisionBitmask(COLLIDE_WITH_ALL);
  physicsBody->setContactTestBitmask(COLLIDE_WITH_ALL);
  physicsBody->setDynamic(false);
  
  this->setPhysicsBody(physicsBody);
}
コード例 #14
0
bool PhysicsCollisionTest::init()
{
	BackLayer::init();

	Size visibleSize = Director::getInstance()->getVisibleSize();

	//创建边框
	auto frame = PhysicsBody::createEdgeBox(Size(visibleSize.width, visibleSize.height));
	frame->setDynamic(false);
	auto framesp = Sprite::create();
	framesp->setPhysicsBody(frame);
	framesp->setPosition(Vec2(visibleSize.width / 2, visibleSize.height / 2));
	addChild(framesp);

	//创建物体1,设置其cate和collision
	auto body1 = PhysicsBody::createBox(Size(57, 57));
	body1->setCategoryBitmask(0x02);//0010
	body1->setCollisionBitmask(0x01 | 0x04); //可以和0001,0101碰撞
	auto sp1 = Sprite::create("Icon.png");
	sp1->setPosition(Vec2(visibleSize.width / 2, visibleSize.height / 2));
	sp1->setPhysicsBody(body1);
	addChild(sp1);

	//创建物体2,它是一个静态物体,并且设置CollisionBitmask为0,表示不被允许与任何物体碰撞
	auto body2 = PhysicsBody::createBox(Size(57, 57));
	body2->setDynamic(false);
	body2->setCollisionBitmask(0x00);
	auto sp2 = Sprite::create("Icon.png");
	sp2->setPosition(Vec2(visibleSize.width / 2, visibleSize.height / 4));
	sp2->setPhysicsBody(body2);
	addChild(sp2);

	//创建物体3,设置其cate和collision
	auto body3 = PhysicsBody::createBox(Size(57, 57));
	body3->setCategoryBitmask(0x04); //0100
	body3->setCollisionBitmask(0x01 | 0x02);
	auto sp3 = Sprite::create("Icon.png");
	sp3->setPosition(Vec2(visibleSize.width / 2, sp3->getContentSize().height / 2));
	sp3->setPhysicsBody(body3);
	addChild(sp3);

	return true;
}
コード例 #15
0
ファイル: SpecialBody.cpp プロジェクト: Chonger8888/project
void ExplisonScreen::initBody()
{
	auto body = PhysicsBody::createBox(m_WinSize);
	body->setGravityEnable(false);
	body->setMass(100000);
	body->setCategoryBitmask(SELF_SCOPE_CATEGORYBITMASK);
	body->setCollisionBitmask(0);
	body->setContactTestBitmask(SELF_SCOPE_CONTACTTESTBITMASK);
	setPhysicsBody(body);
}
コード例 #16
0
void HelloWorld:: addNewSpriteAtPosition(Point p){
    auto sprite=Sprite::create("CloseNormal.png");
    sprite->setTag(1);
    auto body=PhysicsBody::createCircle(sprite->getContentSize().width/2);
    body->setCategoryBitmask(1);
    body->setCollisionBitmask(1);//决定了 能否发生碰撞
    body->setContactTestBitmask(1);
    sprite->setPhysicsBody(body);
    sprite->setPosition(p);
    this->addChild(sprite);
}
コード例 #17
0
ファイル: RoleEnemyBig.cpp プロジェクト: turingwjy/AirWar
bool RoleEnemyBig::init() {
	bool bRet = false;
	do {
		CC_BREAK_IF(!Sprite::initWithSpriteFrameName("enemy3_n1.png"));

		// set physical body
		auto body = PhysicsBody::createBox(getContentSize());
		body->setGroup(PHYSICAL_BODY_ENEMY_GROUP);
		body->setCategoryBitmask(PHYSICAL_BODY_ENEMY_BITMASK_CATEGORY);
		body->setContactTestBitmask(PHYSICAL_BODY_ENEMY_BITMASK_CONTACT_TEST);
		body->setCollisionBitmask(PHYSICAL_BODY_ENEMY_BITMASK_COLLISION);
		setPhysicsBody(body);

		// set position
		auto enemy1Size = getContentSize();
		auto winSize = Director::getInstance()->getWinSize();
		int minX = enemy1Size.width / 2;
		int maxX = winSize.width - enemy1Size.width / 2;
		int rangeX = maxX - minX;
		int actualX = (rand() % rangeX) + minX;
		setPosition(Point(actualX, winSize.height + enemy1Size.height / 2));

		// run action
		float minDuration, maxDuration;
		minDuration = 2.0f;
		maxDuration = 4.0f;

		int rangeDuration = maxDuration - minDuration;
		int actualDuration = (rand() % rangeDuration) + minDuration;

		auto actionMove = MoveTo::create(actualDuration,
				Point(actualX, 0 - getContentSize().height / 2));
		auto actionDone = RemoveSelf::create(true);
		auto sequence = Sequence::create(actionMove, actionDone, nullptr);
		runAction(sequence);

		auto enemy3SpriteFrame_1 =
				SpriteFrameCache::getInstance()->getSpriteFrameByName(
						"enemy3_n1.png");
		auto enemy3SpriteFrame_2 =
				SpriteFrameCache::getInstance()->getSpriteFrameByName(
						"enemy3_n2.png");
		auto animation = Animation::create();
		animation->setDelayPerUnit(0.2f);
		animation->addSpriteFrame(enemy3SpriteFrame_1);
		animation->addSpriteFrame(enemy3SpriteFrame_2);
		auto animate = Animate::create(animation);
		runAction(RepeatForever::create(animate));

		bRet = true;
	} while (0);

	return bRet;
}
コード例 #18
0
ファイル: GameMainLayer.cpp プロジェクト: mengtest/DreamerRun
//设置一个跟GameLayer一样大小的物理世界
void GameMainLayer::initPhysics() {
    Size nodeSize = m_uiNode->getContentSize();
    this->setContentSize(nodeSize);
    auto body = PhysicsBody::createEdgeBox(nodeSize, PHYSICSBODY_MATERIAL_DEFAULT, 3);
    m_uiNode->setPhysicsBody(body);
    m_uiNode->setTag(10);
    body->setCategoryBitmask(1);    // 0001
    body->setContactTestBitmask(-1); // 0100
    body->setCollisionBitmask(-1);   // 0011
    
}
コード例 #19
0
ファイル: Player.cpp プロジェクト: irukasoft/IrukaJet
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;

}
コード例 #20
0
ファイル: Player.cpp プロジェクト: koro9koro/Flappygame
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;
}
コード例 #21
0
ファイル: Coin.cpp プロジェクト: ffpwserver/Parkour
void Coin::initBody(){
	auto bodySize = getSprite()->getContentSize();
	
	bodySize.width -= 20;
	bodySize.height -= 15;

	auto phyBody = PhysicsBody::createEdgeBox(bodySize);
	phyBody->setCategoryBitmask(1);
	phyBody->setCollisionBitmask(1);
	phyBody->setContactTestBitmask(1);
	
	this->setPhysicsBody(phyBody);
}
コード例 #22
0
ファイル: SpecialBody.cpp プロジェクト: Chonger8888/project
void PlaneProtect::initBody()
{
	auto body = PhysicsBody::createCircle(getContentSize().width * 0.5);
	body->setGravityEnable(false);
	body->setMass(10000000);
	body->setCategoryBitmask(SELF_SCOPE_CATEGORYBITMASK);
	body->setCollisionBitmask(0);
	body->setContactTestBitmask(SELF_SCOPE_CONTACTTESTBITMASK);
	setPhysicsBody(body);
	setMaxBlood(Protect_Hp);
	m_ContactPro.hurts = -Ene_Plane_Hp_max - 10;
	
	setUnitId(UnitId::eFanghudun);
}
コード例 #23
0
ファイル: UT_Map.cpp プロジェクト: cyrusNMA/test01
bool UT_Map::init(){
    
    if(!Layer::init()){
        return false;
    }
    
    //seeds the random number generator
    srand((unsigned int)time(nullptr));
    
    visibleSize = Director::getInstance()->getVisibleSize();
    origin = Director::getInstance()->getVisibleOrigin();
    
    //create wall
    //If Full Paint FPS Droped to 12
    //
    test_map = new GameMap();
    test_map->BuildMap("res/map/block_01.jpg" , Size(18, 10), (int)PhysicsCategory::Wall, (int)PhysicsCategory::None, (int)PhysicsCategory::Player);
    this->addChild(test_map);

    log("GetPlayerPos() ? (%f, %f)",test_map->GetPlayerPos().x , test_map->GetPlayerPos().y);
    
    //create wall crasher
    if(true){
        body = cocos2d::DrawNode::create();
        body->drawSolidRect(Vec2(0,0), Vec2(10,25), Color4F(0.1, 0, 1, 1));
        body->setPosition(test_map->GetPlayerPos());
        test_map->addChild(body);
        body->setName("Player_body");
        body->setZOrder(1);
        
        auto body_phyBody = PhysicsBody::createBox(Size(body->getContentSize().width , body->getContentSize().height),PhysicsMaterial(0.1f, 1.0f, 0.0f));
        body_phyBody->setDynamic(true);
        body_phyBody->setCategoryBitmask((int)PhysicsCategory::Player);
        body_phyBody->setCollisionBitmask((int)PhysicsCategory::None);
        body_phyBody->setContactTestBitmask((int)PhysicsCategory::Wall);
        body->setPhysicsBody(body_phyBody);
    }
    
    
    this->schedule(schedule_selector(UT_Map::Scheduler));
    
    
    //Set the Physics Collision notification
    auto contactListener = EventListenerPhysicsContact::create();
    contactListener->onContactBegin = CC_CALLBACK_1(UT_Map::onContactBegan, this);
    this->getEventDispatcher()->addEventListenerWithSceneGraphPriority(contactListener, this);
    
    return true;
}
コード例 #24
0
ファイル: Enemy.cpp プロジェクト: eiaeii/StarDestroyer
bool Enemy::init()
{
	if (!Sprite::init())
	{
		return false;
	}
	this->initWithFile("enemy/enemy1_1.png");
	this->setTag(ObjectType::TYPE_ENEMY);
	auto body = PhysicsBody::createCircle(this->getContentSize().width / 2);
	body->setCategoryBitmask(Gategorybitmask::GATEGORYBITMASK_ENEMY);
	body->setContactTestBitmask(0xFFFFFFFF);
	body->setCollisionBitmask(Collisionbitmask::COLLISIONBITMASK_ENEMY);
	this->setPhysicsBody(body);
	return true;
}
コード例 #25
0
ファイル: Enemy.cpp プロジェクト: twils1337/CPP
Enemy::Enemy(int iD, Object_T oT, Enemy_T et, float x, float y, string bod, string attk, int hp, int dmg, bool ded)
:Type(et), body_fn(bod), attack_fn(attk), attack_dmg(dmg), dead(ded)
{
    ID = iD;
    ot = oT;
    body=Sprite::create(body_fn);
    body->setPosition(x,y);
    auto physicsBody = cocos2d::PhysicsBody::createBox(getBody()->getContentSize(),
                                                       cocos2d::PhysicsMaterial(0,1,0));
    physicsBody->setCollisionBitmask(ID);
    physicsBody->setCategoryBitmask(1);
    physicsBody->setContactTestBitmask(true);
    physicsBody->setGravityEnable(false);
    physicsBody->setDynamic(true);
    body->setPhysicsBody(physicsBody);
    health = hp;
}
コード例 #26
0
KickBonus::KickBonus(Game * game) : BonusInterface(game)
{
    name = BONUS_KICK;
    
    durationMin = Definitions::TIME_KICKBONUS_MIN;
    durationMax = Definitions::TIME_KICKBONUS_MAX;

    sprite = Sprite::create("bonus_kick.png");
    sprite->setScale(0.5);
    sprite->setName(LABEL_BONUS);
    sprite->setUserData(this);
    
    auto body = cocos2d::PhysicsBody::createCircle( sprite->getContentSize().width/2, MATERIAL_PLAYER);
    body->setCategoryBitmask(BITMASK_BONUS);
    body->setContactTestBitmask(BITMASK_PLAYER | BITMASK_INVISIBLE_PLAYER);
    sprite->setPhysicsBody(body);

    
}
コード例 #27
0
Sprite* EnemyShip::create(char *filename)
{

	ship = Sprite::create(filename);

	ship->setRotation(180);

	auto physicsBody = PhysicsBody::createBox(ship->getContentSize(), PhysicsMaterial(0.1, 1.0, 0));

	physicsBody->setDynamic(true);

	physicsBody->setCategoryBitmask(0x09);
	physicsBody->setCollisionBitmask(0);
	physicsBody->setContactTestBitmask(0x05);

	ship->setPhysicsBody(physicsBody);

	return ship;
}
コード例 #28
0
ファイル: GameScene24.cpp プロジェクト: EMGAME/Sister
Sprite* GameScene24::createBorder(Point pos) {

    auto border = Sprite::create("level_24/border.png");

    Size borderSize = border->getContentSize();

    auto body = PhysicsBody::createBox(borderSize);
    body->setDynamic(false);
    body->getShape(0)->setFriction(0.0f);
    body->getShape(0)->setRestitution(1.001f);
    body->getShape(0)->setDensity(1.0f);

    body->setCategoryBitmask(1);    // 0001
    body->setCollisionBitmask(1);   // 0001
    body->setContactTestBitmask(1); // 0001
    border->setPhysicsBody(body);
    border->setPosition(pos);

    return border;
}
コード例 #29
0
ファイル: SpecialBody.cpp プロジェクト: Chonger8888/project
void Wall::initBody(const Size& size,const PhysicsMaterial& pm )
{
	auto body = PhysicsBody::createBox(size,pm,Point::ZERO);
	setPhysicsBody(body);
	body->setGravityEnable(false);
	body->setMass(100000);
	body->setDynamic(false);
	body->setCategoryBitmask(WALL_CATEGORYBITMASK);
	body->setCollisionBitmask(WALL_COLLISIONBITMASK);
	body->setContactTestBitmask(WALL_CONTACTTESTBITMASK);
	setInvincibleAtLine();	//检查过,切换场景会被删

	SelfPro ua;
	ua.isInvincible = 1;

	ContactPro rs;

	setSelfPro(ua);
	setContactPro(rs);
}
コード例 #30
0
ファイル: Attack.cpp プロジェクト: rickystone/Match2
void Attack::callback2(Node* sender)
{
    Sprite* xx = (Sprite*)sender;
    Point pt = xx->getPosition();
    
    Sprite* sprite = Sprite::createWithSpriteFrameName("zidan.png");
    auto body = PhysicsBody::createCircle(sprite->getContentSize().width/2);
    body->setCategoryBitmask(0x04);
    body->setCollisionBitmask(0x04);
    body->setGroup(10);
    sprite->setPhysicsBody(body);
    sprite->setPosition(pt);
    sprite->setRotation(xx->getRotation());
    addChild(sprite,0,10);
    
    auto seq = Sequence::create(DelayTime::create(0.5f), CallFunc::create( std::bind(&Attack::fuckxx, this, sprite)),
                                DelayTime::create(1.0f),
                                RemoveSelf::create(),
                                NULL);
    this->runAction(seq);
}