CCParticleSun* CCParticleSun::createWithTotalParticles(unsigned int numberOfParticles) { CCParticleSun* pRet = new CCParticleSun(); if (pRet && pRet->initWithTotalParticles(numberOfParticles)) { pRet->autorelease(); } else { CC_SAFE_DELETE(pRet); } return pRet; }
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(); }
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++; } }