Ejemplo n.º 1
0
JSWindowVarObject::JSWindowVarObject( JSWindowObject *_obj, int _kind )
{
    kind = _kind;
    object = _obj;

    switch ( kind )
    {
    case KIND_JSWindowStatus:
	setName( "status" );
	setDynamic( TRUE );
	break;
    case KIND_JSWindowTop:
	setName( "top" );
	setConst( TRUE );
	break;
    case KIND_JSWindowParent:
	setName( "parent" );
	setConst( TRUE );
	break;
    case KIND_JSWindowName:
	setName( "name" );
	setConst( TRUE );
	setDynamic( TRUE );
	break;
    }
}
Ejemplo n.º 2
0
//第一关(添加5关的关卡刚体)
void GameLayer:: addGangti1(){
    
    //木头
    for (int i = 1; i<=9 ;i++ ) {
        auto str = __String::createWithFormat("wood_%d",i);
        Sprite* woods1 = node->getChildByName<Sprite*>(str->getCString());
//       log("tag = %d",woods1->getTag());
        if (woods1->getTag() ==5) {
            auto body = PhysicsBody::createBox(woods1->getContentSize());
        body->setDynamic(false);
            body->getShape(0)->setRestitution(1);//反弹力
         body->getShape(0)->setFriction(0);//摩擦力
            woods1->setPhysicsBody(body);
            woods1->getPhysicsBody()->setContactTestBitmask(WALL_MASK);
        }

        
    }

    //墙壁
    for (int i =1; i<=4; i++) {
        auto str2 =__String::createWithFormat("wall_%d",i);
        auto wall = node->getChildByName<Sprite*>(str2->getCString());
        if (wall->getTag() == 25) {
            auto body2 = PhysicsBody::createBox(Size(wall->getContentSize().width,wall->getContentSize().height));//den res fri
            
            body2->setDynamic(false);
            body2->getShape(0)->setFriction(0);
            body2->getShape(0)->setRestitution(1);

            
            wall->setPhysicsBody(body2);
            
            wall->getPhysicsBody()->setContactTestBitmask(WALL_MASK);
        }
        
    }
    
    //石头
    for (int i =1; i<= 2; i++) {

        auto str3 =__String::createWithFormat("stone_%d",i);
        auto stone = node->getChildByName<Sprite*>(str3->getCString());
        if (stone->getTag() == 45) {
//            auto body2 = PhysicsBody::createBox(Size(stone->getContentSize().width,stone->getContentSize().height));//den res fri
//
            auto body3 = PhysicsBody::createCircle(stone->getContentSize().width/2);
            
            body3->setDynamic(false);
            body3->getShape(0)->setFriction(0);
            body3->getShape(0)->setRestitution(1.5);//弹力
            stone->setPhysicsBody(body3);
            stone->getPhysicsBody()->setContactTestBitmask(WALL_MASK);
        }
    }
    
    
}
Ejemplo n.º 3
0
void PassGateComponent::onEnter()
{
	Component::onEnter();

	float centerPosition = nextPosY();

	_mainNode = Node::create();
	_mainNode->setPosition(Point(_visibleSize.width / 2, centerPosition));
	this->getOwner()->addChild(_mainNode, 1);

	// center point in the gate for the score updating (check the collision)
	Node* gateCenterNode = Node::create();
	auto gateCenterBody = PhysicsBody::createBox(Size(0, GATE_HEIGHT));
	gateCenterBody->setCollisionBitmask(PASSGATE_CENTER_ID);
	gateCenterBody->setDynamic(false);
	gateCenterBody->setContactTestBitmask(true);
	_mainNode->setPhysicsBody(gateCenterBody);

	// area inside the gate
	Node* gateNode = Node::create();
	auto gateBody = PhysicsBody::createEdgeBox(Size(GATE_WIDTH, GATE_HEIGHT));
	gateBody->setCollisionBitmask(PASSGATE_AREA_ID);
	gateBody->setDynamic(false);
	gateBody->setContactTestBitmask(true);
	gateNode->setPhysicsBody(gateBody);
	_mainNode->addChild(gateNode);

	// height of top 'wall'
	float height1 = _visibleSize.height - SCORE_LAYOUT_BORDER_SIZE * 2 - SCORE_TEXT_SIZE-GATE_HEIGHT;

	// draw graphics of the top 'wall'
	Sprite* topNode = Sprite::create("wall2.png", Rect(Vec2(0, centerPosition), Size(GATE_WIDTH, height1)));
	topNode->setPosition(Point(0, topNode->getBoundingBox().getMaxY()+GATE_HEIGHT/2));
	auto topBody = PhysicsBody::createEdgeBox(topNode->getContentSize());
	topBody->setCollisionBitmask(OBSTACLE_ID);
	topBody->setDynamic(false);
	topBody->setContactTestBitmask(true);
	topNode->setPhysicsBody(topBody);
	_mainNode->addChild(topNode);

	// draw graphics of the bottom 'wall'
	Sprite* bottomNode = Sprite::create("wall2.png", Rect(Vec2(0, centerPosition), Size(GATE_WIDTH, height1)));
	bottomNode->setPosition(Point(0, -(bottomNode->getBoundingBox().getMaxY() + GATE_HEIGHT / 2)));
	auto bottomBody = PhysicsBody::createEdgeBox(bottomNode->getContentSize());
	bottomBody->setCollisionBitmask(OBSTACLE_ID);
	bottomBody->setDynamic(false);
	bottomBody->setContactTestBitmask(true);
	bottomNode->setPhysicsBody(bottomBody);
	_mainNode->addChild(bottomNode);

	//
	auto collisionListener = EventListenerPhysicsContact::create();
	collisionListener->onContactSeparate = CC_CALLBACK_1(PassGateComponent::onContactSeparate, this);
	Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(collisionListener, gateNode);
}
Ejemplo n.º 4
0
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);
	}

}
Ejemplo n.º 5
0
void KDeckDefStatic::Init(System::File::KTabFile2* fileReader)
{
	char buf[1024];
	fileReader->GetInteger("ID", 0, (int*)&m_Id);
	fileReader->GetInteger("HeroID", 0, (int*)&m_heroID);
	fileReader->GetInteger("HeroHp", 0, (int*)&m_heroHp);
	fileReader->GetInteger("Res", 0, (int*)&m_res);
	fileReader->GetInteger("Rnd", 0, (int*)&m_rnd);
	fileReader->GetInteger("HeroStrong", 0, (int*)&m_heroStrong);
	fileReader->GetInteger("ResLucky", 0, (int*)&m_resLucky);
	fileReader->GetInteger("HeroLucky", 0, (int*)&m_heroLucky);
	fileReader->GetInteger("Fate", 0, (int*)&m_fate);
	
	fileReader->GetString("DECK", "", buf, 1023);
	setDeck(buf);
	
	fileReader->GetInteger("DrawNum", 0, (int*)&m_drawNum);
	
	fileReader->GetString("HeroSkill", "", buf, 1023);
	setHeroSkill(buf);


	fileReader->GetString("Dynamic", "", buf, 1023);
	setDynamic(buf);
}
Ejemplo n.º 6
0
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;
}
Ejemplo n.º 7
0
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);
    }
}
void Coin3::spawnCoin3(cocos2d::Layer * layer)
{
    auto coin=Sprite::create("Candy (3).png");
    const float SCALE_TO_DEVICE = Director::getInstance()->getVisibleSize().width / DES_RES_X;
    coin->setScale(SCALE_TO_DEVICE * 2);
    //coin->setScale(2, 2);
    auto ran=CCRANDOM_0_1();
    auto posi_x=origin.x+coin->getContentSize().width* SCALE_TO_DEVICE/2+ran*(visibleSize.width-coin->getContentSize().width* SCALE_TO_DEVICE);
    auto posi_y=origin.y+visibleSize.height;
    
    auto body=PhysicsBody::createBox(coin->getContentSize()*2* SCALE_TO_DEVICE);
    
    body->setDynamic(false);
    body->setCollisionBitmask(COIN3_COLLISION_BITMASK);
    body->setContactTestBitmask(COIN3_CONTACT_BITMASK);
    coin->setPhysicsBody(body);
    
    coin->setPosition(Point(posi_x,posi_y));
    layer->addChild(coin,7);
    auto move_dis=visibleSize.height+coin->getContentSize().height* SCALE_TO_DEVICE+50;
    auto move_duration=COIN3_SPEED*move_dis;
    auto coin_move=MoveBy::create(move_duration,Point(0,-move_dis));
    coin->runAction(Sequence::create(coin_move,RemoveSelf::create(true),nullptr));
    
}
Ejemplo n.º 9
0
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;
}
Ejemplo n.º 10
0
void GameScreen::spawnAsteroid(float dt)
{
    Size visibleSize = Director::getInstance()->getVisibleSize();
    Point origin = Director::getInstance()->getVisibleOrigin();

    int asteroidIndex = (arc4random() % 3) + 1;
    __String* asteroidString = __String::createWithFormat("GameScreen/Asteroid_%i.png", asteroidIndex);

    Sprite* tempAsteroid = Sprite::create(asteroidString->getCString());

    int xRandomPosition = (arc4random() % (int)(visibleSize.width - (tempAsteroid->getContentSize().width / 2))) + (tempAsteroid->getContentSize().width / 2);

    tempAsteroid->setPosition(
        Point(xRandomPosition + origin.x,
              -tempAsteroid->getContentSize().height + origin.y));

    asteroids.push_back(tempAsteroid);

    auto body = PhysicsBody::createCircle(asteroids[asteroids.size() - 1]->getContentSize().width / 2);
    body->setContactTestBitmask(true);
    body->setDynamic(true);
    asteroids[asteroids.size() - 1]->setPhysicsBody(body);
    
    this->addChild(asteroids[asteroids.size() - 1], -1);
}
Ejemplo n.º 11
0
void FireBall::spawnFireBall(cocos2d::Layer * layer)
{
    auto FireBall=cocos2d::Sprite::create("FireBall.png");
    auto ran=CCRANDOM_0_1();
    auto posi_x=ran*visibleSize.width+origin.x;
    auto posi_y=origin.y+visibleSize.height+FireBall->getContentSize().height;
    
    Vector<SpriteFrame*> fireball_an(4);
    fireball_an.pushBack(SpriteFrame::create("FireBall.png", Rect(0,0,58,96)));
    fireball_an.pushBack(SpriteFrame::create("FireBall_2.png", Rect(0,0,58,96)));
    fireball_an.pushBack(SpriteFrame::create("FireBall.png", Rect(0,0,58,96)));
    fireball_an.pushBack(SpriteFrame::create("FireBall_3.png", Rect(0,0,58,96)));
    auto ann=Animation::createWithSpriteFrames(fireball_an,0.1f);
    auto anim_ll=Animate::create(ann);
    auto anim_l=RepeatForever::create(anim_ll);
    FireBall->runAction(anim_l);

    
    auto FireBall_body=cocos2d::PhysicsBody::createBox(FireBall->getContentSize());
    
    FireBall_body->setDynamic(false);
    FireBall_body->setCollisionBitmask(FIREBALL_COLLISION_BITMASK);
    FireBall_body->setContactTestBitmask(FIREBALL_CONTACT_BITMASK);
    FireBall->setPhysicsBody(FireBall_body);
    
    FireBall->setPosition(Point(posi_x,posi_y));
    layer->addChild(FireBall,102);
    auto move_dis=visibleSize.height+FireBall->getContentSize().height+130;
    auto move_duration=FIREBALL_SPEED*move_dis;
    auto FireBall_move=cocos2d::MoveBy::create(move_duration,Point(0,-move_dis));
    FireBall->runAction(Sequence::create(FireBall_move,RemoveSelf::create(true),nullptr));
    
}
Ejemplo n.º 12
0
void Paddle::createPhysicsBody() {
  auto bodySize = getContentSize();

  auto width2 = bodySize.width / 2;
  auto width4 = width2 / 2;
  auto width8 = width4 / 2;
  auto height2 = bodySize.height / 2;
  auto height4 = height2 / 2;
  auto height8 = height4 / 2;

  // Create this oval shape so that, when the ball bounces closer to the edges,
  // its direction changes slightly.
  std::array<Point, 12> bodyPoints = {
    Point(width4,           height2),
    Point(width2 - width8,  height2 - height8),
    Point(width2,           height8),
    Point(width2,           -height8),
    Point(width2 - width8,  -height2 + height8),
    Point(width4,           -height2),
    Point(-width4,          -height2),
    Point(-width2 + width8, -height2 + height8),
    Point(-width2,          -height8),
    Point(-width2,          height8),
    Point(-width2 + width8, height2 - height8),
    Point(-width4,          height2)
  };

  auto body = PhysicsBody::createPolygon(bodyPoints.data(), bodyPoints.size(), MATERIAL);
  body->setDynamic(false);
  body->setContactTestBitmask(BITMASK_CONTACT_TEST);

  setPhysicsBody(body);
}
Ejemplo n.º 13
0
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;
}
Ejemplo n.º 14
0
  NDBT_Attribute(const char* _name,
		 NdbDictionary::Column::Type _type,
		 int _length = 1,
		 bool _pk = false, 
		 bool _nullable = false,
		 CHARSET_INFO *cs= 0,
		 NdbDictionary::Column::StorageType storage = NdbDictionary::Column::StorageTypeMemory,
                 bool dynamic = false,
                 const void* defaultVal = NULL,
                 Uint32 defaultValBytes = 0):
    NdbDictionary::Column(_name)
  {
    assert(_name != 0);
    
    setType(_type);
    setLength(_length);
    setNullable(_nullable);
    setPrimaryKey(_pk);
    if (cs)
    {
      setCharset(cs);
    }
    setStorageType(storage);
    setDynamic(dynamic);
    setDefaultValue(defaultVal, defaultValBytes);
  }
Ejemplo n.º 15
0
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;
}
Ejemplo n.º 16
0
Balloon* Balloon::create(Point* pos)
{
    Balloon* b = new Balloon();

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

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

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

        // Setup Event Listener
        //b->input();

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

        return b;
    }
    CC_SAFE_DELETE(b);

    return NULL;
}
Ejemplo n.º 17
0
Archivo: Devil.cpp Proyecto: ilhaeYe/MB
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();

}
Ejemplo n.º 18
0
void Hanhtinh::spawn_hanhtinh( float dt )
{
	Size visibleSize = Director::getInstance()->getVisibleSize();
	Point origin = Director::getInstance()->getVisibleOrigin();

	int hanhtinh_index = (rand() % 2) + 1;
	__String* hanhtinh_String = __String::createWithFormat("GameScreen/Asteroid_%i.png", hanhtinh_index);
	Sprite* tempHanhtinh = Sprite::create(hanhtinh_String->getCString());

	int yRandomPosition = (rand() % (int)(visibleSize.height - (tempHanhtinh->getContentSize().height / 2))) + 
		(tempHanhtinh->getContentSize().height / 2);

	//int xRandomPosition =  (rand() % (int)(visibleSize.width/2));

	tempHanhtinh->setPosition(Point( visibleSize.width, yRandomPosition ));
	hanhtinh.push_back(tempHanhtinh);

	auto hanhtinh_body = PhysicsBody::createCircle( hanhtinh[hanhtinh.size() -1 ]->getContentSize().width /2 );
	hanhtinh_body->setContactTestBitmask(true);
	hanhtinh_body->setDynamic(true);
	hanhtinh[hanhtinh.size() -1 ]->setPhysicsBody(hanhtinh_body);
	hanhtinh[hanhtinh.size() -1]->setTag(TAG_HANHTINH);
	this->addChild(hanhtinh[hanhtinh.size() - 1], -1);

}
Ejemplo n.º 19
0
void BreakoutMainScene::createBall()
{
    // Create Ball
    cocos2d::log("Create Ball");
    // Tao qua bong va thiet lap khung vat ly cho no
    ball = Sprite::create("breakout_img/breakout_ball.png");
    ball->setAnchorPoint(Vec2::ANCHOR_MIDDLE);
    ball->setPosition(Vec2(WINSIZE.width/2 , WINSIZE.height/2 + 50));

    auto ballBody = PhysicsBody::createCircle(ball->getContentSize().width/2 , PHYSICSBODY_MATERIAL_DEFAULT);
    ballBody->getShape(0)->setDensity(1.0f); // trong luc
    ballBody->getShape(0)->setFriction(0.0f);
    ballBody->getShape(0)->setRestitution(1.0f);
    ballBody->setDynamic(true);
    ballBody->setContactTestBitmask(0x00000001);

    ballBody->setGravityEnable(false); // Khong set gia toc


    // Tao 1 luc tac dong
    Vect force = Vect(900000.0f, -900000.0f);
    ballBody->applyImpulse(force);

    ball->setPhysicsBody(ballBody);

    ball->setTag(Tag::T_Ball);
    this->addChild(ball);
}
Ejemplo n.º 20
0
void Pipe::SpawnPipe(Layer* layer)
{
	CCLOG("SPAWN PIPE");
	Vec2 centerScreen = ((Vec2)visibleSize / 2.0f) + origin;

	auto topPipe = Sprite::create("Pipe.png");
	auto bottomPipe = Sprite::create("Pipe.png");

	auto topPipeBody = PhysicsBody::createBox(topPipe->getContentSize());
	auto bottomPipeBody = PhysicsBody::createBox(bottomPipe->getContentSize());

	auto random = CCRANDOM_0_1();

	if (random < LOWER_SCREEN_PIPE_THRESHOLD)
	{
		random = LOWER_SCREEN_PIPE_THRESHOLD;
	}
	else if ( random > UPPER_SCREEN_PIPE_THRESHOLD)
	{
		random = UPPER_SCREEN_PIPE_THRESHOLD;
	}

	auto topPipePosition = (random * visibleSize.height) + (topPipe->getContentSize().height / 2.0f);
	topPipeBody->setDynamic(false);
	bottomPipeBody->setDynamic(false);

	topPipeBody->setCollisionBitmask(OBSTACLE_COLLISION_BITMASK);
	bottomPipeBody->setCollisionBitmask(OBSTACLE_COLLISION_BITMASK);

	topPipeBody->setContactTestBitmask(true);
	bottomPipeBody->setContactTestBitmask(true);

	topPipe->setPhysicsBody(topPipeBody);
	bottomPipe->setPhysicsBody(bottomPipeBody);

	topPipe->setPosition(Point(visibleSize.width + topPipe->getContentSize().width + origin.x, topPipePosition));
	bottomPipe->setPosition(Point(topPipe->getPositionX(), topPipePosition - (Sprite::create("Ball.png")->getContentSize().height * PIPE_GAP) - topPipe->getContentSize().height));

	layer->addChild(topPipe);
	layer->addChild(bottomPipe);

	auto topPipeAction = MoveBy::create(PIPE_MOVEMENT_SPEED * visibleSize.width, Point(-visibleSize.width * 1.5, 0));
	auto bottomPipeAction = MoveBy::create(PIPE_MOVEMENT_SPEED * visibleSize.width, Point(-visibleSize.width * 1.5, 0));

	topPipe->runAction(topPipeAction);
	bottomPipe->runAction(bottomPipeAction);
}
Ejemplo n.º 21
0
void Block::init(cocos2d::Sprite *sprite, const std::string& name) {
    GameObject::init(name);
    setSprite(sprite);
    setName(name);
    auto world = Physics::getWorld2D();
    auto physicsBody = world->addBody(sprite, name, "block");
    physicsBody->setDynamic(false);
}
Ejemplo n.º 22
0
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;
}
Ejemplo n.º 23
0
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);
}
Ejemplo n.º 24
0
bool AstroObj::init(GameScene* game)
{
    VisualObj::init(game);
    auto body = _rootNode->getPhysicsBody();
    game->physicsWorld()->getForceField()->addGravitySource(body, body->getMass());
    body->setDynamic(false);
    setZs(ZsAstroObjDefault);
    return true;
}
Ejemplo n.º 25
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;
}
void RangeMissile::createMissileBody()
{
	PhysicsBodyParser::getInstance()->parseJsonFile("CustomPhysicsBody.json");

	auto missileBody = PhysicsBodyParser::getInstance()->bodyFormJson(missileSprite, "range_missile", PhysicsMaterial(1, 1, 0));
	missileBody->setDynamic(false);
	missileBody->setContactTestBitmask(true);
	missileBody->setCollisionBitmask(OBSTACLE_COLLISION_BITMASK);

	missileSprite->setPhysicsBody(missileBody);
}
Ejemplo n.º 27
0
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;

}
Ejemplo n.º 28
0
void Level::createRectFixture(TMXLayer* layer, Sprite* spr){
	//TILE SIZE CALCULATION
	auto tileSize = this->map->getTileSize();
	const float ppm = 32.0f; //pixel per meter

	//PHYSICS BODY
	auto body = PhysicsBody::createBox(Size(tileSize.width, tileSize.width));
	body->setDynamic(false);
	body->setMass(PHYSICS_INFINITY);
	body->setContactTestBitmask(0xFFFFFFFF);
	spr->setPhysicsBody(body);
}
Ejemplo n.º 29
0
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);

}
Ejemplo n.º 30
0
void Pipe::SpawnPipe(cocos2d::Layer *layer){
    CCLOG( "SPAWN PIPE !!" );
    auto topPipe = Sprite::create("Pipe.png");
    auto bottomPipe = Sprite::create("Pipe.png");

    auto topPipeBody = PhysicsBody::createBox(topPipe->getContentSize());
    auto bottomPipeBody = PhysicsBody::createBox(bottomPipe->getContentSize());

    auto random = CCRANDOM_0_1();

     if( random < LOWER_SCREEN_PIPE_THRESHOLD ){

        random = LOWER_SCREEN_PIPE_THRESHOLD;
     }
     else if( random > UPPER_SCREEN_PIPE_THRESHOLD ){

        random = UPPER_SCREEN_PIPE_THRESHOLD;
     }

     auto topPipePosition = ( random * visibleSize.height) + (topPipe->getContentSize().height/2 );

     topPipeBody->setDynamic(false);
     bottomPipeBody->setDynamic(false);

     topPipe->setPhysicsBody(topPipeBody);
     bottomPipe->setPhysicsBody(bottomPipeBody);

     topPipe->setPosition(Point(visibleSize.width/2 + topPipe->getContentSize().width + origin.x + CCRANDOM_MINUS1_1() * 250 , topPipePosition));
     bottomPipe->setPosition(Point(topPipe->getPositionX()
                            ,topPipePosition - (Sprite::create("Ball.png")->getContentSize().height * PIPE_GAP) - topPipe->getContentSize().height));

    // topPipe->setPosition(Point(origin.x + CCRANDOM_MINUS1_1(), origin.y));

    // topPipe->setPosition(Point(visibleSize.width/2 + origin.x + visibleSize.width/4,visibleSize.height/2 + origin.y + CCRANDOM_0_1() + visibleSize.height/4));

     layer->addChild(topPipe);
     layer->addChild(bottomPipe);
}