コード例 #1
0
ファイル: HelloWorldScene.cpp プロジェクト: amy60618/android
void HelloWorld::tick(float dt) {
	int velocityIterations = 8;
	int positionIterations = 1;
	
	m_world->Step(dt, velocityIterations, positionIterations);
	for (b2Body* b = m_world->GetBodyList(); b; b = b->GetNext()) {
		if (b->GetUserData() != NULL) {
			CCSprite* sprite = (CCSprite*)(b->GetUserData());
			sprite->setPosition(ccp(b->GetPosition().x * PTM_RATIO, b->GetPosition().y * PTM_RATIO));
			sprite->setRotation( -1 * CC_RADIANS_TO_DEGREES(b->GetAngle()));
			
		}
	}
	if (m_releasingArm && m_bulletJoint != NULL) {
		if (m_armJoint->GetJointAngle() <= CC_DEGREES_TO_RADIANS(10)) {
			m_releasingArm = false;
			m_world->DestroyJoint(m_bulletJoint);
			m_bulletJoint = NULL;

			CCDelayTime* delayAction = CCDelayTime::create(5.0f);
			CCCallFunc* callSelectorAction = CCCallFunc::create(this, callfunc_selector(HelloWorld::resetBullet));
			this->runAction(CCSequence::create(delayAction, callSelectorAction, NULL));
		}
	}
	if (m_bulletBody && m_bulletJoint == NULL) {
		b2Vec2 position = m_bulletBody->GetPosition();
		CCPoint myPosition = this->getPosition();
		CCSize screenSize = CCDirector::sharedDirector()->getWinSize();
		if (position.x > screenSize.width/2.0f/PTM_RATIO) {
			myPosition.x = -MIN(screenSize.width*2.0f - screenSize.width,
				position.x*PTM_RATIO - screenSize.width/2.0f);
			this->setPosition(myPosition);
		}
	}
    std::set<b2Body*>::iterator pos;
	for (pos = contactListener->contacts.begin(); pos != contactListener->contacts.end(); ++pos) {
		b2Body* body = *pos;
		for (vector<b2Body*>::iterator iter = targets->begin(); iter != targets->end(); ++iter) {
			if (body == *iter) {
				iter = targets->erase(iter);
				break;
			}
		}
		for (vector<b2Body*>::iterator iter = enemies->begin(); iter != enemies->end(); ++iter) {
			if (body == *iter) {
				iter = enemies->erase(iter);
				break;
			}
		}
		CCNode* contactNode = (CCNode*)body->GetUserData();
		CCPoint position = contactNode->getPosition();
		removeChild(contactNode, true);
		m_world->DestroyBody(body);

		CCParticleSun *explosion = CCParticleSun::create();
		explosion->retain();
		explosion->setTexture(CCTextureCache::sharedTextureCache()->addImage("fire.png"));
//		explosion->initWithTotalParticles(200);
		explosion->setAutoRemoveOnFinish(true);
		explosion->setStartSizeVar(10.0f);
		explosion->setSpeed(70.0f);
		explosion->setAnchorPoint(ccp(0.5f, 0.5f));
		explosion->setPosition(position);
		explosion->setDuration(1.0f);
		addChild(explosion, 11);
		explosion->release();
	}
	contactListener->contacts.clear();
}
コード例 #2
0
void HelloWorld::tick(ccTime dt)
{
	int velocityIterations = 8;
	int positionIterations = 1;
	world->Step(dt, velocityIterations, positionIterations);
	for (b2Body* b = world->GetBodyList(); b; b=b->GetNext())
	{
		if(b->GetUserData() != NULL)
		{
			CCSprite *myActor = (CCSprite *)b->GetUserData();
			myActor->setPosition(ccp(b->GetPosition().x * PTM_RATIO, b->GetPosition().y * PTM_RATIO));
			myActor->setRotation(-1 * CC_RADIANS_TO_DEGREES(b->GetAngle()));
		}
	}

	//Arm is being released
	if (releasingArm && bulletJoint)
	{
		if(armJoint->GetJointAngle() <= CC_DEGREES_TO_RADIANS(10))
		{
			releasingArm = false;
			world->DestroyJoint(bulletJoint);
			bulletJoint = NULL;
			this->schedule(schedule_selector(HelloWorld::resetBullet), 5.0f);
		}
	}

	//Bullet is moving
	if (bulletBody != nullptr && bulletJoint == NULL)
	{
		b2Vec2 position = bulletBody->GetPosition();
		CCPoint myPosition = this->getPosition();
		CCSize screenSize = CCDirector::sharedDirector()->getWinSize();

		if(position.x > screenSize.width / 2.0f / PTM_RATIO)
		{
			myPosition.x = -MIN(960.0f - screenSize.width, position.x * PTM_RATIO - screenSize.width / 2.0f);
			this->setPosition(myPosition);
		}
	}

	set<b2Body*>::iterator pos;
	for(pos = contactListener->contacts.begin(); pos != contactListener->contacts.end(); ++pos)
	{
		b2Body *body = *pos;
		CCNode *contactNode = (CCNode *) body->GetUserData();
		CCPoint position = contactNode->getPosition();
		this->removeChild(contactNode,true);
		world->DestroyBody(body);
		for(vector<b2Body*>::size_type i = 0; i >= targets->size(); i++)
		{
			try
			{
				if(targets->at(i) == body)
				{
					if(i == targets->size()-1)
					{
						printf("asdf");
					}
					targets->erase(targets->begin() + i);
				}
			}
			catch(exception e)
			{
				enemies->clear();
				break;
			}
		}
		for(vector<b2Body*>::size_type i = 0; i >= enemies->size(); i++)
		{
			try
			{
				if(enemies->at(i) == body)
				{
					if(i == enemies->size()-1)
					{
						printf("asdf");
					}
					enemies->erase(enemies->begin() + i);
				}
			}
			catch(exception e)
			{
				enemies->clear();
				break;
			}
		}

		CCParticleSun* explosion = new CCParticleSun();
		explosion->initWithTotalParticles(200);
		//explosion->setTotalParticles(200);
		explosion->setAutoRemoveOnFinish(true);
		explosion->setStartSize(10.0f);
		explosion->setSpeed(70.0f);
		explosion->setAnchorPoint(ccp(0.5f,0.5f));
		explosion->setPosition(position);
		explosion->setDuration(1.0f);
		CCTexture2D *tex = new CCTexture2D();
		CCImage *img = new CCImage();
		img->initWithImageFile("fire.png");
		tex->initWithImage(img);
		explosion->setTexture(tex);
		this->addChild(explosion, 11);
		explosion->release();
	}

	contactListener->contacts.clear();
}
コード例 #3
0
void HelloWorld::tick(ccTime dt)
{
		
	int velocityIterations = 8;
	int positionIterations = 1;
    
	
	m_pWorld->Step(dt, velocityIterations, positionIterations);
	
    //更新位置和角度
    for (b2Body* b = m_pWorld->GetBodyList(); b; b = b->GetNext())
	{
		if (b->GetUserData() != NULL)
        {
            CCSprite * actor = (CCSprite*)b->GetUserData();
            actor->setPosition(ccp(b->GetPosition().x*PTM_RATIO,b->GetPosition().y*PTM_RATIO));
            actor->setRotation(-1*CC_RADIANS_TO_DEGREES(b->GetAngle()));
    
        }	
	}
    
    
    //发射子弹
    if(m_bReleaseArm && m_pBulletJoint != NULL)
    {
        if(m_pArmJoin->GetJointAngle() <= CC_DEGREES_TO_RADIANS(10))//差不多回到原始位置再发射子弹
        {
            m_bReleaseArm = false;
            m_pWorld->DestroyJoint(m_pBulletJoint);
            m_pBulletJoint = NULL;
            
            CCDelayTime *delayAction = CCDelayTime::actionWithDuration(5.0f);
            CCCallFunc *callSelectorAction = CCCallFunc::actionWithTarget(this, callfunc_selector(HelloWorld::resetBullet));
            this->runAction(CCSequence::actions(delayAction, callSelectorAction, NULL));
            
        }
    }
    
    //镜头跟随
    if(m_pBulletBody && m_pBulletJoint == NULL)
    {
        b2Vec2 position = m_pBulletBody->GetPosition();
        CCPoint myPosition = this->getPosition();
        CCSize screenSize = CCDirector::sharedDirector()->getWinSize();
        
        if(position.x > screenSize.width/2.0f/PTM_RATIO)
        {
            myPosition.x = -min(screenSize.width, position.x*PTM_RATIO);
            this->setPosition(myPosition);
        }
    }
    
    //消灭敌人
    std::set<b2Body*>::iterator pos;
    for(pos = m_pListener->contracts.begin();pos != m_pListener->contracts.end();)
    {
        b2Body * body = *pos;
        CCSprite * enemy = (CCSprite*)body->GetUserData();
        
        
        //爆炸效果
        CCPoint position = ccp(body->GetWorldCenter().x*PTM_RATIO,body->GetWorldCenter().y*PTM_RATIO);
        CCParticleSun *explosion = CCParticleSun::node();
		explosion->retain();
		explosion->setTexture(CCTextureCache::sharedTextureCache()->addImage("fire.png"));
		explosion->initWithTotalParticles(20);
		explosion->setIsAutoRemoveOnFinish(true);
		explosion->setStartSizeVar(10.0f);
		explosion->setSpeed(70.0f);
		explosion->setAnchorPoint(ccp(0.5f, 0.5f));
		explosion->setPosition(position);
		explosion->setDuration(1.0f);
		addChild(explosion, 11);
		explosion->release();

        

        //移除
        this->removeChild(enemy, true);
        m_pWorld->DestroyBody(body);
        body = NULL;
        
        m_pListener->contracts.erase(*pos);
        pos++;
        
        
    }
    
    
  
}