//1 2 4 6
//0 1 2 3
int HelloWorld::addGround(int index) {
	B2Sprite* pGround = B2Sprite::create("ground.png");
	CCSize groundSize = pGround->getContentSize();
	mgroundSize = pGround->getContentSize();
	//m_pGround[index]->setTag(index);
	b2BodyDef bodyDef;
	bodyDef.type = b2_staticBody;

	bodyDef.position = b2Vec2((groundSize.width*(index*2)/2.0f)/RATIO,
		groundSize.height/2.0f/RATIO);

	b2Body *groundBody = mWorld->CreateBody(&bodyDef);

	b2PolygonShape groundShape;
	groundShape.SetAsBox(groundSize.width/2.0f/RATIO, groundSize.height/2.0f/RATIO);
	b2FixtureDef groundFixtureDef;
	groundFixtureDef.shape = &groundShape;
	groundFixtureDef.filter.categoryBits = 0x0002;
	groundBody->CreateFixture(&groundFixtureDef);

	pGround->setB2Body(groundBody);
	pGround->setPTMRatio(RATIO);
	addChild(pGround,3,SPRITE_TAG_GROUND);
	m_pGroundVec.push_back(pGround);
	int iwin=int(mScreenSize.width/(groundSize.width)+2);
	CCLOG("addGround win x:%f,groundSize.width:%f,mfac:%f,iwin:%d",mScreenSize.width,
		groundSize.width,mfac,iwin);
	return iwin;

}
//容器中放柱子
void HelloWorld::addBar(float dt){
	float offset = -rand()%6/mfac;
	float speed = 0;
	// 下面的柱子
	B2Sprite *downBar = B2Sprite::create("down_bar.png");
	CCSize downBarSize = downBar->getContentSize();


	b2BodyDef downBarBodyDef;
	downBarBodyDef.type = b2_kinematicBody;
	//x轴屏幕右方外两像素的位置,y轴是柱子一半
	downBarBodyDef.position = b2Vec2(mScreenSize.width/RATIO+2,
		downBarSize.height/RATIO/2+offset);
	downBarBodyDef.linearVelocity = b2Vec2(speed, 0);
	b2Body *downBarBody = mWorld->CreateBody(&downBarBodyDef);
	b2PolygonShape downBarShape;
	downBarShape.SetAsBox(downBarSize.width/2/RATIO,
		downBarSize.height/2/RATIO);
	b2FixtureDef downBarFixtureDef;
	downBarFixtureDef.restitution = 0;
	downBarFixtureDef.shape = &downBarShape;
	downBarFixtureDef.filter.categoryBits = 0x0004;
	downBarBody->CreateFixture(&downBarFixtureDef);

	downBar->setB2Body(downBarBody);
	downBar->setPTMRatio(RATIO);
	mBarContainer-> addChild(downBar,1,SPRITE_TAG_BAR);


	m_pDownBarVec.push_back(downBar);

	//上面的柱子
	B2Sprite *upBar = B2Sprite::create("up_bar.png");
	CCSize upBarSize = upBar->getContentSize();

	b2BodyDef upBarBodyDef;
	upBarBodyDef.type = b2_kinematicBody;
	upBarBodyDef.position = b2Vec2(mScreenSize.width/RATIO+2,
		downBarSize.height/RATIO+offset+gBardis/RATIO+upBarSize.height/2/RATIO);
	upBarBodyDef.linearVelocity = b2Vec2(speed, 0);
	b2Body *upBarDody = mWorld->CreateBody(&upBarBodyDef);
	b2PolygonShape upBarShape;
	upBarShape.SetAsBox(upBarSize.width/2/RATIO, upBarSize.height/2/RATIO);
	b2FixtureDef upBarFixtureDef;
	upBarFixtureDef.restitution = 0;
	upBarFixtureDef.shape = &upBarShape;
	upBarFixtureDef.filter.categoryBits = 0x0004;
	upBarDody->CreateFixture(&upBarFixtureDef);
	upBar->setB2Body(upBarDody);
	upBar->setPTMRatio(RATIO);
	mBarContainer->addChild(upBar,1,SPRITE_TAG_BAR);


}
void HelloWorld::addBar(float dt){
    float offset = -rand()%5;
    
    // 下面的柱子
    B2Sprite *downBar = B2Sprite::create("down_bar.png");
    CCSize downBarSize = downBar->getContentSize();
    
    b2BodyDef downBarBodyDef;
    downBarBodyDef.type = b2_kinematicBody;
    downBarBodyDef.position = b2Vec2(mScreenSize.width/RATIO+2,
                                     downBarSize.height/RATIO/2+offset);
    downBarBodyDef.linearVelocity = b2Vec2(-5, 0);
    b2Body *downBarBody = mWorld->CreateBody(&downBarBodyDef);
    
    b2PolygonShape downBarShape;
    downBarShape.SetAsBox(downBarSize.width/2/RATIO,
                          downBarSize.height/2/RATIO);
    b2FixtureDef downBarFixtureDef;
    downBarFixtureDef.shape = &downBarShape;
    downBarBody->CreateFixture(&downBarFixtureDef);
    
    downBar->setB2Body(downBarBody);
    downBar->setPTMRatio(RATIO);
    mBarContainer-> addChild(downBar);

    
    //上面的柱子
    B2Sprite *upBar = B2Sprite::create("up_bar.png");
    CCSize upBarSize = upBar->getContentSize();
    
    b2BodyDef upBarBodyDef;
    upBarBodyDef.type = b2_kinematicBody;
    upBarBodyDef.position = b2Vec2(mScreenSize.width/RATIO+2,
                                   downBarSize.height/RATIO+offset+2+upBarSize.height/2/RATIO);
    upBarBodyDef.linearVelocity = b2Vec2(-5, 0);
    b2Body *upBarDody = mWorld->CreateBody(&upBarBodyDef);
    
    b2PolygonShape upBarShape;
    upBarShape.SetAsBox(upBarSize.width/2/RATIO, upBarSize.height/2/RATIO);
    b2FixtureDef upBarFixtureDef;
    upBarFixtureDef.shape = &upBarShape;
    upBarDody->CreateFixture(&upBarFixtureDef);
    
    upBar->setB2Body(upBarDody);
    upBar->setPTMRatio(RATIO);
    mBarContainer-> addChild(upBar);
}
Example #4
0
void HelloWorld::addGround(){
    B2Sprite *ground = B2Sprite::create("ground.png");
    CCSize size = ground->getContentSize();
    ground->setScaleX(2.0);
    b2BodyDef bDef;
    bDef.type = b2_staticBody;
    bDef.position = b2Vec2(size.width/RATIO, size.height/2/RATIO);
    b2Body* groundBody = world->CreateBody(&bDef);
    
    b2PolygonShape groundShape;
    groundShape.SetAsBox(size.width/RATIO, size.height/2/RATIO);
    b2FixtureDef groundFixtureDef;
    groundFixtureDef.shape = &groundShape;
    groundBody->CreateFixture(&groundFixtureDef);
    
    ground->setB2Body(groundBody);
    ground->setPTMRatio(RATIO);
    
    addChild(ground);
}