//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;

}
B2Sprite* B2Sprite::createWithTexture(CCTexture2D *pTexture, const CCRect& rect)
{
    B2Sprite* pRet = new B2Sprite();
    if (pRet && pRet->initWithTexture(pTexture, rect))
    {
        pRet->autorelease();
    }
    else
    {
        CC_SAFE_DELETE(pRet);
    }
    
    return pRet;
}
B2Sprite* B2Sprite::create()
{
    B2Sprite* pRet = new B2Sprite();
    if (pRet && pRet->init())
    {
        pRet->autorelease();
    }
    else
    {
        CC_SAFE_DELETE(pRet);
    }
    
    return pRet;
}
B2Sprite* B2Sprite::create(const char *pszFileName, const CCRect& rect)
{
    B2Sprite* pRet = new B2Sprite();
    if (pRet && pRet->initWithFile(pszFileName, rect))
    {
        pRet->autorelease();
    }
    else
    {
        CC_SAFE_DELETE(pRet);
    }
    
    return pRet;
}
B2Sprite* B2Sprite::createWithSpriteFrameName(const char *pszSpriteFrameName)
{
    B2Sprite* pRet = new B2Sprite();
    if (pRet && pRet->initWithSpriteFrameName(pszSpriteFrameName))
    {
        pRet->autorelease();
    }
    else
    {
        CC_SAFE_DELETE(pRet);
    }
    
    return pRet;
}
//容器中放柱子
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);


}
Beispiel #7
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);
}
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);
}
void HelloWorld::BeginContact(b2Contact *contact){
	B2Sprite *p = (B2Sprite *)contact->GetFixtureA()->GetBody()->GetUserData();
	if(p->getTag()==SPRITE_TAG_GROUND)
	{
		CCDirector::sharedDirector()->getScheduler()->setTimeScale(1.f);
		//mBird->setRotation(-90.f);

		//CCMessageBox("Game Over!", "Game Over!");
		//mBird->setVisible(false);
		this->runAction(CCShake::create(1,5));

		//std::string fullPath;
		//#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)  
		//fullPath = CCFileUtils::sharedFileUtils()->getWritablePath(); 
		//fullPath += "score.abc";

		//unsigned long size;
		//unsigned char *str = CCFileUtils::sharedFileUtils()->getFileData(fullPath,"wt+",&size);
		m_bnew=false;
		string filedata;
		filedata = TDInvFileUtils::getFileByName("score.abc");
		int filescore = 0;
		if (filedata != "")
		{
			filescore = atoi(filedata.c_str());
			if (filescore < testnum)
			{
				m_bnew=true;
				m_bestscore = testnum;
				char str[10];
				char   temp[256];   
				sprintf(str,"%d",testnum); 
				TDInvFileUtils::saveFile(str,"score.abc");
			}
			else
			{
				m_bestscore = filescore;
			}

		}
		else
		{
			m_bestscore = testnum;
			char str[10];
			sprintf(str,"%d",testnum); 
			TDInvFileUtils::saveFile(str,"score.abc");
		}



		//#else   

		//#endif  
		//Sleep(1000);
		if(!m_bhitbar)
		{
//			EFFECT_PLAY(true,MUSIC_HIT);
		}
		else
		{
//			EFFECT_PLAY(true,MUSIC_DIE);
		}
		stopGame();


	}
	if(p == mBird || p == mBird){
		CCScheduler* pScheduler = CCDirector::sharedDirector()->getScheduler();
		pScheduler->setTimeScale(0.f);//实现减速效果
		//scheduleOnce(schedule_selector(HelloWorld::ResumeSpeed), 1);
		long now=millisecondNow();

		while(false){
			if((millisecondNow()-now)>1000)
			{
				break;
			}
		};
		CCDirector::sharedDirector()->getScheduler()->setTimeScale(1.f);
		//CCDirector::sharedDirector()->getScheduler()->setTimeScale(0.2f);
		//mBird->getB2Body()->SetLinearVelocity(b2Vec2(0, -5));
		myflag=1;
		b2Filter myfilter;
		myfilter.maskBits=0x0002;
		mBird->getB2Body()->GetFixtureList()->SetFilterData(myfilter);
		m_istatus=GAMEOVER;
		m_bhitbar=true;
//		EFFECT_PLAY(true,MUSIC_HIT);
		//stopGame();
		// CCMessageBox("Game Over!", "Game Over!");
	}
}