void* LoadingLayer::updateInfo(void* args) { // 开线程加载plist文件,将plist加载到CCSpriteFrameCache CCSpriteFrameCache *cache = CCSpriteFrameCache::sharedSpriteFrameCache(); CCTextureCache* teCache = CCTextureCache::sharedTextureCache(); for (int i=0; i<m_staticYPlist->count() * 0.5; i++) { const char* m_pngFullPath = ((CCString* )m_staticYPlist->objectAtIndex(i))->getCString(); const char* m_plistFullPath = ((CCString* )m_staticYPlist->objectAtIndex(i + m_staticYPlist->count() * 0.5))->getCString(); // CCLog("m_pngFullPath:%s", m_pngFullPath); // CCLog("m_plistFullPath:%s", m_plistFullPath); CCTexture2D* texture = teCache->textureForKey(m_pngFullPath); cache->addSpriteFramesWithFile(m_plistFullPath, texture); // 删除纹理 teCache->removeTextureForKey(m_pngFullPath); } // 可以异步加载数据 m_target为空,就跳过 if (m_instance->m_target) { if (m_instance->m_callFuncData) { (m_instance->m_target->*m_instance->m_callFuncData)(); } } m_instance->isOver = true; // m_taxiBus->runAction(CCSequence::create( // CCMoveTo::create(0.7, ccp(m_moveLength, m_taxiBus->getPosition().y)), // CCCallFunc::create(m_instance, callfunc_selector(LoadingLayer::loadingSuccessful)), // NULL)); return NULL; }
void SwarmGame::transformToZombie(EntitySprite& entity) { EntityType toType; if (transformToType(entity.m_entity->type(), toType)) { MobileEntity* zombie = static_cast<MobileEntity*>(EntityFactory::instance().createEntity(toType)); if ( NULL != zombie ) { m_ZombieSwarm.addEntity(zombie); CCTexture2D* entityTexture = NULL; CCTextureCache* textureCache = CCTextureCache::sharedTextureCache(); entityTexture = textureCache->textureForKey(xmlDataManager::instance().getEntitySprite (toType)); int mapHeight = m_map.height() * c_blockSize; //create new sprite SpriteEntity* sprite = SpriteEntity::spriteWithEntity(zombie); sprite->setAnchorPoint(entity.m_sprite->getAnchorPoint()); sprite->setScaleX(entity.m_sprite->getScaleX()); sprite->setScaleY(entity.m_sprite->getScaleY()); sprite->setPosition(entity.m_sprite->getPosition()); m_mapLayer->removeChild(entity.m_sprite, true); entity.m_sprite = sprite; entity.m_sprite->setMaxHealth( zombie->hitPoints() ); entity.m_sprite->setIsVisible(true); m_mapLayer->addChild(entity.m_sprite); //TODO: add more properties m_map.removeEntity(entity.m_entity); m_map.placeEntity(zombie, entity.m_entity->x(), entity.m_entity->y()); m_ZombieSwarm.removeTarget(entity.m_entity); m_HumanSwarm.removeEntity(entity.m_entity, true); //m_map->removeEntity(entity.m_entity); m_HumanSwarm.addTarget (zombie); entity.m_entity = zombie; } } }
void SwarmGame::initialiseSwarm (EntityType type, int numEntities) { CCTextureCache* textureCache = CCTextureCache::sharedTextureCache(); CCSize size( float(m_map.width()), float(m_map.height()) ); EntityFactory& factory = EntityFactory::instance(); MobileEntity* entity = NULL, *mapEntity = NULL; MapNode* node = NULL; int x, y; int mapHeight = m_map.height() * c_blockSize; for ( int i = 0; i < numEntities; i++ ) { if ( (entity = static_cast<MobileEntity*>(factory.createEntity (type))) ) { // random position x = rand() % static_cast<int>(size.width); y = rand() % static_cast<int>(size.height); if ( (node = m_map.nodeAt(x, y)) ) { if ( node->isBlocking() ) { // if we chose a spot that already has an entity, try again... i--; delete entity; continue; } } // if we get here, we can place the new entity m_map.placeEntity<MobileEntity>(entity, x, y); // add it to the swarm CCTexture2D* entityTexture = NULL; if (entity->isHuman()) m_HumanSwarm.addEntity(entity); else { m_ZombieSwarm.addEntity(entity); m_HumanSwarm.addTarget(entity); } // create an entity sprite structure for the visualisation EntitySprite es; entityTexture = textureCache->textureForKey(xmlDataManager::instance().getEntitySprite (entity->type())); es.m_entity = dynamic_cast<MobileEntity*>(entity); es.m_sprite = SpriteEntity::spriteWithEntity(entity); es.m_sprite->setAnchorPoint( ccp(0.5, 0.5) ); es.m_sprite->setScaleX( c_blockSize/es.m_sprite->getContentSize().width ); es.m_sprite->setScaleY( c_blockSize/es.m_sprite->getContentSize().height ); es.m_sprite->setPosition( ccp(es.m_entity->x() * c_blockSize + c_blockSize/2, mapHeight - ((es.m_entity->y()+1) * c_blockSize + c_blockSize/2)) ); es.m_sprite->setMaxHealth( entity->hitPoints() ); m_mapLayer->addChild(es.m_sprite); m_entitySprites.push_back(es); } } }
void CCSprite::setUrl(const char *url) { #ifdef THREAD_IMAGE if(!g_ListenThread){ mSpriteProcessArray = CCArray::create(); mSpriteProcessArray->retain(); // never release g_ThreadQueueLock = s3eThreadLockCreate(); g_ListenThread = s3eThreadCreate(SpriteListenThread, NULL, NULL); } unschedule(schedule_selector(CCSprite::setImageFromImage)); schedule(schedule_selector(CCSprite::setImageFromImage)); #endif setVisible(false); if(!mOriginalPosSet){ mOriginalX = getPositionX(); mOriginalY = getPositionY(); mOriginalPosSet = true; } if(mRequest){ mRequest->stop(); mRequest = NULL; } CCTextureCache *cache = CCTextureCache::sharedTextureCache(); #ifdef THREAD_IMAGE const char *filePath = getFilePathFromURL(url); if(mFilePath) mFilePath->release(); mFilePath = CCString::create(filePath); mFilePath->retain(); CCTexture2D* texture = cache->textureForKey(filePath); #else CCTexture2D* texture = cache->textureForKey(url); #endif if(texture){ int tmp = mCountShow; mCountShow = 0; setTextureAndSize(texture); mCountShow = tmp; if(mDelegate) mDelegate->didSpriteReceiveFile(this); return; } #ifdef THREAD_IMAGE // Find in disk // still crash in device dont know why if(s3eFileCheckExists(filePath)){ if(mWillSetImage) delete mWillSetImage; mWillSetImage = NULL; //scheduleOnce(schedule_selector(CCSprite::createNewSpriteProcess), 0.0f); createNewSpriteProcess(CCString::create(filePath)); return; } #endif mRequest = HttpRequest::create(); mRequest->delegate = this; #ifdef THREAD_IMAGE mRequest->mSaveFile = new CCString(filePath); mRequest->mType = HTTP_REQUEST_SAVE_FILE; #else mRequest->mType = HTTP_REQUEST_FILE; #endif mRequest->callURL(url); }