void endElement(void *ctx, const char *name) { CC_UNUSED_PARAM(ctx); CCSAXState curState = m_tStateStack.empty() ? SAX_DICT : m_tStateStack.top(); std::string sName((char*)name); if( sName == "dict" ) { m_tStateStack.pop(); m_tDictStack.pop(); if ( !m_tDictStack.empty()) { m_pCurDict = m_tDictStack.top(); } } else if (sName == "array") { m_tStateStack.pop(); m_tArrayStack.pop(); if (! m_tArrayStack.empty()) { m_pArray = m_tArrayStack.top(); } } else if (sName == "true") { CCString *str = new CCString("1"); if (SAX_ARRAY == curState) { m_pArray->addObject(str); } else if (SAX_DICT == curState) { m_pCurDict->setObject(str, m_sCurKey); } str->release(); } else if (sName == "false") { CCString *str = new CCString("0"); if (SAX_ARRAY == curState) { m_pArray->addObject(str); } else if (SAX_DICT == curState) { m_pCurDict->setObject(str, m_sCurKey); } str->release(); } m_tState = SAX_NONE; }
CCActionInterval* CCAnimate::reverse(void) { CCMutableArray<CCSpriteFrame*> *pOldArray = m_pAnimation->getFrames(); CCMutableArray<CCSpriteFrame*> *pNewArray = new CCMutableArray<CCSpriteFrame*>(pOldArray->count()); if (pOldArray->count() > 0) { CCSpriteFrame *pElement; CCMutableArray<CCSpriteFrame*>::CCMutableArrayRevIterator iter; for (iter = pOldArray->rbegin(); iter != pOldArray->rend(); iter++) { pElement = *iter; if (! pElement) { break; } pNewArray->addObject((CCSpriteFrame*)(pElement->copy()->autorelease())); } } CCAnimation *pNewAnim = CCAnimation::animationWithFrames(pNewArray, m_pAnimation->getDelay()); pNewArray->release(); return CCAnimate::actionWithDuration(m_fDuration, pNewAnim, m_bRestoreOriginalFrame); }
void textHandler(void *ctx, const char *ch, int len) { if (m_tState == SAX_NONE) { return; } CCString *pText = new CCString(); pText->m_sString = std::string((char*)ch,0,len); switch(m_tState) { case SAX_KEY: m_sCurKey = pText->m_sString; break; case SAX_INT: case SAX_REAL: case SAX_STRING: { CCAssert(!m_sCurKey.empty(), "not found key : <integet/real>"); if (m_bInArray) { m_pArray->addObject(pText); } else { m_pCurDict->setObject(pText, m_sCurKey); } break; } } pText->release(); }
void endElement(void *ctx, const XML_Char *name) { std::string sName((char*)name); if( sName == "dict" ) { m_tDictStack.pop(); if ( !m_tDictStack.empty() ) { m_pCurDict = (CCDictionary<std::string, CCObject*>*)(m_tDictStack.top()); } } else if (sName == "array") { CCAssert(m_bInArray, "The plist file is wrong!"); m_pCurDict->setObject(m_pArray, m_sCurKey); m_pArray->release(); m_pArray = NULL; m_bInArray = false; } else if (sName == "true") { CCString *str = new CCString("1"); if (m_bInArray) { m_pArray->addObject(str); } else { m_pCurDict->setObject(str, m_sCurKey); } str->release(); } else if (sName == "false") { CCString *str = new CCString("0"); if (m_bInArray) { m_pArray->addObject(str); } else { m_pCurDict->setObject(str, m_sCurKey); } str->release(); } m_tState = SAX_NONE; }
bool RunningWolf::init(CCSpriteBatchNode * batch) { if (!CCSprite::initWithSpriteFrameName("wolf_0.png")) { return false; } m_speed = 50; m_batch = batch; m_batch->retain(); m_batch->addChild(this); // Set up running animation CCMutableArray<CCSpriteFrame*> runFrames; for (int i = 0; i < 5; ++i) { char name[11]; sprintf(name, "wolf_%i.png",i); name[10] = '\0'; runFrames.addObject(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(name)); } CCAnimation * runAnim = CCAnimation::animationWithFrames(&runFrames, 0.1f); m_runAction = CCRepeatForever::actionWithAction(CCAnimate::actionWithAnimation(runAnim, true)); m_runAction->retain(); // Set up jumping animation CCMutableArray<CCSpriteFrame*> jumpFrames; jumpFrames.addObject(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName("wolf_0.png")); jumpFrames.addObject(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName("wolf_1.png")); CCAnimation * jumpAnim = CCAnimation::animationWithFrames(&jumpFrames, 0.5f); m_jumpAction = CCSequence::actionOneTwo( CCAnimate::actionWithDuration(1.0f, jumpAnim, false), CCCallFunc::actionWithTarget(this, callfunc_selector(RunningWolf::finishJumping))); m_jumpAction->retain(); CCSize winSize = CCDirector::sharedDirector()->getWinSize(); m_batch->getTexture()->setAliasTexParameters(); getTexture()->setAliasTexParameters(); run(); scheduleUpdate(); return true; }
/** * Adds a target and action for a particular event to an internal dispatch * table. * The action message may optionnaly include the sender and the event as * parameters, in that order. * When you call this method, target is not retained. * * @param target The target object—that is, the object to which the action * message is sent. It cannot be nil. The target is not retained. * @param action A selector identifying an action message. It cannot be NULL. * @param controlEvent A control event for which the action message is sent. * See "CCControlEvent" for constants. */ void CCControl::addTargetWithActionForControlEvent(CCObject* target, SEL_MenuHandler action, CCControlEvent controlEvent) { // Create the invocation object CCInvocation *invocation=new CCInvocation(target, action, controlEvent); // Add the invocation into the dispatch list for the given control event CCMutableArray<CCInvocation*> *eventInvocationList = dispatchListforControlEvent(controlEvent); eventInvocationList->addObject(invocation); }
/* * 文字列リストを取得。 */ CCMutableArray<ColorString*> *TagString::getStringList( int line ) { CCMutableArray<ColorString*> *list = new CCMutableArray<ColorString*>; list->autorelease(); for( int i = 0; i < parseList->count(); i++ ) { ColorString* cstr = parseList->getObjectAtIndex( i ); if( cstr->getLine() != line ) continue; list->addObject( cstr ); } return list; }
CCMutableArray<CCObject*>* CCSet::allObjects() { CCMutableArray<CCObject*>* objectArray = new CCMutableArray<CCObject*>(10); objectArray->autorelease(); CCObject* object; CCSetIterator it; for( it = this->begin(); it != this->end(); it++) { object = (*it); objectArray->addObject(object); } return objectArray; }
bool S_CCAnimation::initWithContext(JSContextRef ctx, JSObjectRef obj, size_t argumentCount, const JSValueRef arguments[]) { bool result = false; if (argumentCount > 0) { CCMutableArray<CCSpriteFrame*> frames; // get the array if (JSValueIsObject(ctx, arguments[0])) { JSObjectRef obj = (JSObjectRef)arguments[0]; JSStringRef propName = JSStringCreateWithUTF8CString("length"); if (JSObjectHasProperty(ctx, obj, propName)) { // might be an array int totalElements = JSValueToNumber(ctx, JSObjectGetProperty(ctx, obj, propName, NULL), NULL); for (int i=0; i < totalElements; i++) { // we could definitively improve this!! JSValueRef val = JSObjectGetPropertyAtIndex(ctx, obj, i, NULL); JSStringRef str = JSValueToStringCopy(ctx, val, NULL); int len = JSStringGetLength(str); char *buff = (char *)malloc(len+1); JSStringGetUTF8CString(str, buff, len+1); CCSpriteFrame *frame = CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(buff); if (frame) frames.addObject(frame); JSStringRelease(str); free(buff); } } else { return false; } } else { return false; } if (argumentCount == 2) { float delay = JSValueToNumber(ctx, arguments[1], NULL); result = CCAnimation::initWithFrames(&frames, delay); } else { result = CCAnimation::initWithFrames(&frames); } } else { result = CCAnimation::init(); } if (result) { jsObject = obj; } return result; }
void AnimatePacker::loadAnimate( char *path ) { //解析xml//////////////////////////////////// const char *pszPath = CCFileUtils::fullPathFromRelativePath(path);//从相对路径得到绝对全路径 CCSAXParser parser; AnimateSaxDelegator delegator; if (false == parser.init("UTF-8")) { //TODO return; } parser.setDelegator(&delegator); parser.parse(pszPath); //加载plist//////////////////////////////////// vector<string> plists=delegator.plists; for (int i=0;i<plists.size();i++) { CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile(plists[i].c_str()); } //缓冲animate//////////////////////////////////// vector<Animate> animates=delegator.animates; CCMutableArray<CCSpriteFrame*> *spriteFramesArray = new CCMutableArray<CCSpriteFrame*>(); for (int i=0;i<animates.size();i++) { Animate animate=animates[i]; vector<string> spriteFrames=animate.spriteFrames; for (int j=0;j<spriteFrames.size();j++) { CCSpriteFrame *spriteFrame=CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(spriteFrames[j].c_str()); spriteFramesArray->addObject(spriteFrame); } CCAnimation *animation=CCAnimation::animationWithFrames(spriteFramesArray,animate.delay); CCAnimationCache::sharedAnimationCache()->addAnimation(animation,animate.name.c_str()); spriteFramesArray->removeAllObjects(); } }
void AnimatableDecorator::cacheAnimations() const { for (Value animationValue : animations()) { { Array<Value> frames = animationValue.mapValue()["frames"].arrayValue(); CCMutableArray<CCSpriteFrame*>* spriteFrames = new CCMutableArray<CCSpriteFrame*>(); spriteFrames->autorelease(); for (Value frame : frames) { CCSpriteFrame* spriteFrame = CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(frame.string().c_str()); spriteFrames->addObject(spriteFrame); } float delay = animationValue.mapValue()["delay"].floatValue(); CCAnimation* animation = CCAnimation::animationWithFrames(spriteFrames, delay); CCAnimationCache::sharedAnimationCache()->addAnimation(animation, animationValue.mapValue()["name"].string().c_str()); } } }
void GamingLayer::daZhaoEffect() { CCSpriteFrameCache *cache = CCSpriteFrameCache::sharedSpriteFrameCache(); CCMutableArray<CCSpriteFrame*>* animFrames = new CCMutableArray<CCSpriteFrame*>(4); char str[20] = {}; for (size_t i = 0; i < 12; ++i) { sprintf(str, "%s_%d.png", "zibao", i); CCSpriteFrame *frame = cache->spriteFrameByName(str); animFrames->addObject(frame); } CCAnimation* animation = CCAnimation::animationWithFrames(animFrames, 0.2); CCAnimate* animate = CCAnimate::actionWithAnimation(animation); CCSprite * dazhao = CCSprite::spriteWithSpriteFrameName("zibao_0.png"); dazhao->setPosition(ccp(1024/ 2, 600 / 2)); addChild(dazhao,100); CCCallFuncN *callback = CCCallFuncN::actionWithTarget(this, callfuncN_selector(GamingLayer::RemovedaZhaoEffect)); CCFiniteTimeAction *seq = CCSequence::actions(animate, callback, NULL); dazhao->runAction(seq); }
void textHandler(void *ctx, const char *ch, int len) { CC_UNUSED_PARAM(ctx); if (m_tState == SAX_NONE) { return; } CCSAXState curState = m_tStateStack.empty() ? SAX_DICT : m_tStateStack.top(); CCString *pText = new CCString(); pText->m_sString = std::string((char*)ch,0,len); switch(m_tState) { case SAX_KEY: m_sCurKey = pText->m_sString; break; case SAX_INT: case SAX_REAL: case SAX_STRING: { CCAssert(!m_sCurKey.empty(), "not found key : <integet/real>"); if (SAX_ARRAY == curState) { m_pArray->addObject(pText); } else if (SAX_DICT == curState) { m_pCurDict->setObject(pText, m_sCurKey); } break; } default: break; } pText->release(); }
CCMutableArray<StringLabel*>* TagString::parse( bool useTag ) { string cut = ""; int start_idx = 0; float zen_width = 0; // 全角幅を取得 { StringLabel* label_zen = StringLabel::init( "1", ccc3(0,0,0), baseFontSize ); #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) zen_width = (int)(label_zen->getContentSize().width); #else zen_width = label_zen->getContentSize().width; #endif } ///////////////////////////////////////////// // 変数タグの置換 start_idx = 0; for( ;; ) { int tag_start1 = tagStr.find( "<param=", start_idx ); if( tag_start1 == string::npos ) break; int tag_start2 = tagStr.find( ">", tag_start1 ); string param_str = tagStr.substr( tag_start1+7, tag_start2-(tag_start1+7) ); // 変数の値取得 string value = ParamList::shared()->getValue( param_str ); if( !value.empty() ) { // 文字列の置換 tagStr.replace( tag_start1, tag_start2-tag_start1+1, value ); start_idx = 0; continue; } start_idx = tag_start2 + 1; } //tagStr->getStringWidth() ///////////////////////////////////////////// // 自動改行チェック if( autoKaigyouFlg ) { vector< string > list = StringComparator::comparate( tagStr ); bool tag_start = false; float total_size = 0; string conv_str = ""; for( int i = 0; i < list.size(); i++ ) { if (useTag) { if( list[i] == "<" ) { tag_start = true; } if( list[i] == ">" ) { tag_start = false; conv_str.append( list[i] ); continue; } if( tag_start ) { // 改行チェック if( list[i] == "b" ) { if( list[i+1] == "r" ) { total_size = 0; } } conv_str.append( list[i] ); continue; } } float strw = 0; // 半角 if( list[i].length() == 1 ) { StringLabel* label = StringLabel::init( list[i].c_str(), ccc3(0,0,0), baseFontSize ); strw = label->getContentSize().width; } // 全角 else { strw = zen_width; } total_size += strw; // 改行を挿入 if( width > 0 && total_size > width ) { //CCLog( "kaigyou2 total_size=%d", total_size ); // 改行コードを入れる conv_str.append( "<br>" ); total_size = strw; } else { if (list[i] == "\n") total_size = 0; } conv_str.append( list[i] ); } tagStr = conv_str; } ///////////////////////////////////////////// // 改行、カラー、フォントサイズ、アンカー解析 int line = 0; start_idx = 0; int active_anchor = 0; int active_font_size = baseFontSize; ccColor3B active_color = baseColor; //CCLog( "tag_str=%s", tagStr.c_str() ); bool brflg = false; for( ;; ) { // 開始タグ判定 int tag_start = tagStr.find( "<", start_idx ); if( tag_start != string::npos && useTag ) { // 表示文字列を取得 cut = tagStr.substr( start_idx, tag_start - (start_idx) ); //CCLog( "cut=%s actuve=%d", cut.c_str(), active_anchor ); if( !cut.empty() || brflg ) { parseList->addObject( new ColorString( cut, active_color, active_font_size, line, active_anchor ) ); } int tag_close = tagStr.find( ">", tag_start ); // タグ文字列取得 string param_str = tagStr.substr( tag_start+1, tag_close-(tag_start+1) ); // アンカー if( param_str.find( "left" ) != string::npos ) { active_anchor = ALIGN_LEFT; } if( param_str.find( "center" ) != string::npos ) { active_anchor = ALIGN_CENTER; } if( param_str.find( "right" ) != string::npos ) { active_anchor = ALIGN_RIGHT; } // 改行 if( param_str.find( "br" ) != string::npos ) { line += 1; brflg = true; } // カラー設定 if( param_str.find( "color=" ) != string::npos ) { // 色情報の切り出し string color_str = param_str.substr( 6, param_str.length()-6 ); // 色成分に分解 char *csv = const_cast<char*>( color_str.c_str() ); int red = CommonUtils::StrToInt( strtok( csv, ":" ) ); int green = CommonUtils::StrToInt( strtok( NULL, ":" ) ); int blue = CommonUtils::StrToInt( strtok( NULL, ":" ) ); active_color = ccc3( red, green, blue ); } // フォントサイズ if( param_str.find( "size=" ) != string::npos ) { string size_str = param_str.substr( 5, param_str.length()-5 ); active_font_size = CommonUtils::StrToInt( size_str ); } // カラー 終了タグ if( param_str.find( "/color" ) != string::npos ) { active_color = baseColor; } // アンカー 終了タグ if( param_str.find( "/center" ) != string::npos ) { active_anchor = ALIGN_LEFT; } if( param_str.find( "/right" ) != string::npos ) { active_anchor = ALIGN_LEFT; } // フォントサイズ 終了タグ if( param_str.find( "/size" ) != string::npos ) { active_font_size = baseFontSize; } start_idx = tag_close + 1; } else { /* for( ;; ) { // 改行コードがある場合 tag_start = tagStr.find( "\n", start_idx ); if (tag_start != string::npos) { // 表示文字列を取得 cut = tagStr.substr( start_idx, tag_start - (start_idx) ); if( !cut.empty() ) { parseList->addObject( new ColorString( cut, active_color, active_font_size, line, active_anchor ) ); line += 1; } start_idx = tag_start + 1; } else { break; } } */ cut = tagStr.substr( start_idx, tagStr.length() - (start_idx) ); if( !cut.empty() ) { //CCLog( "cut=%s", cut.c_str() ); parseList->addObject( new ColorString( cut, active_color, active_font_size, line, active_anchor ) ); } break; } } int linecnt = getLineCount(); float basex = 0; float basey = 0; // アライン設定 if( baseAlign == TEXT_ALIGN_CENTER_TOP ) { basex = ( ( width - getStringWidth() )/2 ); } if( baseAlign == TEXT_ALIGN_RIGHT_TOP ) { basex = width - getStringWidth(); } if( baseAlign == TEXT_ALIGN_LEFT_MIDDLE ) { basey = ( ( height - getStringHeight() )/2 ); } if( baseAlign == TEXT_ALIGN_CENTER_MIDDLE ) { basex = ( ( width - getStringWidth() )/2 ); basey = ( ( height - getStringHeight() )/2 ); } if( baseAlign == TEXT_ALIGN_RIGHT_MIDDLE ) { basex = width - getStringWidth(); basey = ( ( height - getStringHeight() )/2 ); } if( baseAlign == TEXT_ALIGN_CENTER_BOTTOM ) { basex = ( ( width - getStringWidth() )/2 ); basey = height - getStringHeight(); } if( baseAlign == TEXT_ALIGN_RIGHT_BOTTOM ) { basex = width - getStringWidth(); basey = height - getStringHeight(); } if( baseAlign == TEXT_ALIGN_LEFT_BOTTOM ) { basey = height - getStringHeight(); } float dispx = basex; float dispy = basey; // 返却用のリスト CCMutableArray<StringLabel*>* ret = new CCMutableArray<StringLabel*>; ret->autorelease(); // ラベルを生成 for( int i = 0; i < linecnt; i++ ) { CCMutableArray<ColorString*> *list = getStringList( i ); float lh = getStringLineHeight( i ); float lw = getStringLineWidth( i ); // 下合わせ dispy += lh; for( int j = 0; j < list->count(); j++ ) { ColorString* cstr = list->getObjectAtIndex( j ); // アライン if( j == 0 ) { if( cstr->getAlign() == ALIGN_LEFT ) dispx = 0; if( cstr->getAlign() == ALIGN_CENTER ) dispx = ((width-lw)/2); if( cstr->getAlign() == ALIGN_RIGHT ) dispx = width - lw; } // テキストを設定 StringLabel* label = cstr->getStrLabel(); label->setAlign( TEXT_ALIGN_LEFT_BOTTOM ); //label->setPosition( dispx, dispy ); label->setOffsetX( dispx ); label->setOffsetY( dispy ); ret->addObject( label ); // ラベルサイズ取得 CCSize size = label->getContentSize(); dispx += size.width; } dispx = basex; } return ret; }
void startElement(void *ctx, const char *name, const char **atts) { CC_UNUSED_PARAM(ctx); CC_UNUSED_PARAM(atts); std::string sName((char*)name); if( sName == "dict" ) { m_pCurDict = new CCDictionary<std::string, CCObject*>(); if(! m_pRootDict) { // Because it will call m_pCurDict->release() later, so retain here. m_pRootDict = m_pCurDict; m_pRootDict->retain(); } m_tState = SAX_DICT; CCSAXState preState = SAX_NONE; if (! m_tStateStack.empty()) { preState = m_tStateStack.top(); } if (SAX_ARRAY == preState) { // add the dictionary into the array m_pArray->addObject(m_pCurDict); } else if (SAX_DICT == preState) { // add the dictionary into the pre dictionary CCAssert(! m_tDictStack.empty(), "The state is wrong!"); CCDictionary<std::string, CCObject*>* pPreDict = m_tDictStack.top(); pPreDict->setObject(m_pCurDict, m_sCurKey); } m_pCurDict->release(); // record the dict state m_tStateStack.push(m_tState); m_tDictStack.push(m_pCurDict); } else if(sName == "key") { m_tState = SAX_KEY; } else if(sName == "integer") { m_tState = SAX_INT; } else if(sName == "real") { m_tState = SAX_REAL; } else if(sName == "string") { m_tState = SAX_STRING; } else if (sName == "array") { m_tState = SAX_ARRAY; m_pArray = new CCMutableArray<CCObject*>(); CCSAXState preState = m_tStateStack.empty() ? SAX_DICT : m_tStateStack.top(); if (preState == SAX_DICT) { m_pCurDict->setObject(m_pArray, m_sCurKey); } else if (preState == SAX_ARRAY) { CCAssert(! m_tArrayStack.empty(), "The state is worng!"); CCMutableArray<CCObject*>* pPreArray = m_tArrayStack.top(); pPreArray->addObject(m_pArray); } m_pArray->release(); // record the array state m_tStateStack.push(m_tState); m_tArrayStack.push(m_pArray); } else { m_tState = SAX_NONE; } }
void HelloWorld::update(ccTime dt) { CCMutableArray<CCSprite*> *projectilesToDelete = new CCMutableArray<CCSprite*>; CCMutableArray<CCSprite*>::CCMutableArrayIterator it, jt; for (it = _projectiles->begin(); it != _projectiles->end(); it++) { CCSprite *projectile =*it; CCRect projectileRect = CCRectMake( projectile->getPosition().x - (projectile->getContentSize().width/2), projectile->getPosition().y - (projectile->getContentSize().height/2), projectile->getContentSize().width, projectile->getContentSize().height); CCMutableArray<CCSprite*>*targetsToDelete = new CCMutableArray<CCSprite*>; for (jt = _targets->begin(); jt != _targets->end(); jt++) { CCSprite *target =*jt; CCRect targetRect = CCRectMake( target->getPosition().x - (target->getContentSize().width/2), target->getPosition().y - (target->getContentSize().height/2), target->getContentSize().width, target->getContentSize().height); if (CCRect::CCRectIntersectsRect(projectileRect, targetRect)) { targetsToDelete->addObject(target); } } for (jt = targetsToDelete->begin(); jt != targetsToDelete->end(); jt++) { CCSprite *target =*jt; _targets->removeObject(target); this->removeChild(target, true); } if (targetsToDelete->count() >0) { projectilesToDelete->addObject(projectile); } targetsToDelete->release(); } for (it = projectilesToDelete->begin(); it != projectilesToDelete->end(); it++) { CCSprite* projectile =*it; _projectiles->removeObject(projectile); this->removeChild(projectile, true); } projectilesToDelete->release(); }
void HelloWorld::updateGame(ccTime dt) { CCMutableArray<CCSprite*> *projectilesToDelete = new CCMutableArray<CCSprite*>; CCMutableArray<CCSprite*>::CCMutableArrayIterator it, jt; for (it = _projectiles->begin(); it != _projectiles->end(); it++) { CCSprite *projectile =*it; CCRect projectileRect = CCRectMake( projectile->getPosition().x - (projectile->getContentSize().width/2), projectile->getPosition().y - (projectile->getContentSize().height/2), projectile->getContentSize().width, projectile->getContentSize().height); CCMutableArray<CCSprite*>*targetsToDelete =new CCMutableArray<CCSprite*>; for (jt = _targets->begin(); jt != _targets->end(); jt++) { CCSprite *target =*jt; CCRect targetRect = CCRectMake( target->getPosition().x - (target->getContentSize().width/2), target->getPosition().y - (target->getContentSize().height/2), target->getContentSize().width, target->getContentSize().height); if (CCRect::CCRectIntersectsRect(projectileRect, targetRect)) { targetsToDelete->addObject(target); } } for (jt = targetsToDelete->begin(); jt != targetsToDelete->end(); jt++) { CCSprite *target =*jt; _targets->removeObject(target); this->removeChild(target, true); _projectilesDestroyed++; if (_projectilesDestroyed >= 5) { GameOverScene *gameOverScene = GameOverScene::node(); gameOverScene->getLayer()->getLabel()->setString("You Win!"); CCDirector::sharedDirector()->replaceScene(gameOverScene); } } if (targetsToDelete->count() > 0) { projectilesToDelete->addObject(projectile); } targetsToDelete->release(); } for (it = projectilesToDelete->begin(); it != projectilesToDelete->end(); it++) { CCSprite* projectile =*it; _projectiles->removeObject(projectile); this->removeChild(projectile, true); } projectilesToDelete->release(); }
void HelloWorld::update(float dt) { CCMutableArray<CCSprite*> *projectilesToDelete = new CCMutableArray<CCSprite*>; CCMutableArray<CCSprite*>::CCMutableArrayIterator it, jt, testt; for (it = _projectiles->begin(); it != _projectiles->end(); it++) { CCSprite *projectile =*it; CCRect projectileRect = CCRectMake( projectile->getPosition().x - (projectile->getContentSize().width/2), projectile->getPosition().y - (projectile->getContentSize().height/2), projectile->getContentSize().width, projectile->getContentSize().height); CCMutableArray<CCSprite*>*targetsToDelete =new CCMutableArray<CCSprite*>; //CCLOG("UPDATE2!!"); for (jt = _targets->begin(); jt != _targets->end(); jt++) { CCSprite *target =*jt; CCRect targetRect = CCRectMake( target->getPosition().x - (target->getContentSize().width/2), target->getPosition().y - (target->getContentSize().height/2), target->getContentSize().width, target->getContentSize().height); if (CCRect::CCRectIntersectsRect(projectileRect, targetRect)) { targetsToDelete->addObject(target); //CCLOG("HIT!!"); CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("enemydeath.wav"); m_score->add_scroe(1); m_score->update_logic(dt); } } for (jt = targetsToDelete->begin(); jt != targetsToDelete->end(); jt++) { CCSprite *target =*jt; _targets->removeObject(target); this->removeChild(target, true); _targetsKilled++; //CCLOG("%d killed!", _targetsKilled); if (_targetsKilled == GAMELENGTHVAL*10) { _targetsKilled = 0; CCDirector::sharedDirector()->replaceScene(WinScene::scene()); } } if (targetsToDelete->count() >0) { projectilesToDelete->addObject(projectile); } targetsToDelete->release(); } for (it = projectilesToDelete->begin(); it != projectilesToDelete->end(); it++) { CCSprite* projectile =*it; _projectiles->removeObject(projectile); this->removeChild(projectile, true); } projectilesToDelete->release(); }
void HelloWorld::testCollisions(ccTime dt) { CCMutableArray<CCSprite*> *projectilesToDelete = new CCMutableArray<CCSprite*>; CCMutableArray<CCSprite *>::CCMutableArrayIterator it, jt; // iterate through projectiles for (it = _projectiles->begin(); it != _projectiles->end(); it++) { CCSprite *projectile = *it; CCRect projectileRect = CCRectMake(projectile->getPosition().x - (projectile->getContentSize().width/2), projectile->getPosition().y - (projectile->getContentSize().height/2), projectile->getContentSize().width, projectile->getContentSize().height); CCMutableArray<CCSprite*> *targetsToDelete = new CCMutableArray<CCSprite*>; // iterate through enemies, see if any intersect with current projectile for (jt = _enemies->begin(); jt != _enemies->end(); jt++) { CCSprite *target = *jt; CCRect targetRect = CCRectMake( target->getPosition().x - (target->getContentSize().width/2), target->getPosition().y - (target->getContentSize().height/2), target->getContentSize().width, target->getContentSize().height); if (CCRect::CCRectIntersectsRect(projectileRect, targetRect)) { targetsToDelete->addObject(target); } } // delete all hit enemies for (jt = targetsToDelete->begin(); jt != targetsToDelete->end(); jt++) { _enemies->removeObject(*jt); this->removeChild((*jt), true); } if (targetsToDelete->count() > 0) { projectilesToDelete->addObject(projectile); } targetsToDelete->release(); } // remove all the projectiles that hit. for (it = projectilesToDelete->begin(); it != projectilesToDelete->end(); it++) { CCSprite *projectile = *it; _projectiles->removeObject(projectile, true); this->removeChild(projectile, true); } for (jt = _enemies->begin(); jt != _enemies->end(); jt++) { CCSprite *target = *jt; CCRect targetRect = CCRectMake( target->getPosition().x - (target->getContentSize().width/2), target->getPosition().y - (target->getContentSize().height/2), target->getContentSize().width, target->getContentSize().height); if (CCRect::CCRectContainsPoint(targetRect, _player->getPosition())) { this->lose(); } } }