void TileMapEditTest::updateMap(float dt) { // IMPORTANT // The only limitation is that you cannot change an empty, or assign an empty tile to a tile // The value 0 not rendered so don't assign or change a tile with value 0 CCTileMapAtlas* tilemap = (CCTileMapAtlas*) getChildByTag(kTagTileMap); // // For example you can iterate over all the tiles // using this code, but try to avoid the iteration // over all your tiles in every frame. It's very expensive // for(int x=0; x < tilemap.tgaInfo->width; x++) { // for(int y=0; y < tilemap.tgaInfo->height; y++) { // ccColor3B c =[tilemap tileAt:ccg(x,y)); // if( c.r != 0 ) { // ////----UXLOG("%d,%d = %d", x,y,c.r); // } // } // } // NEW since v0.7 ccColor3B c = tilemap->tileAt(ccg(13,21)); c.r++; c.r %= 50; if( c.r==0) c.r=1; // NEW since v0.7 tilemap->setTile(c, ccg(13,21) ); }
Parallax1::Parallax1() { // Top Layer, a simple image CCSprite* cocosImage = CCSprite::spriteWithFile(s_Power); // scale the image (optional) cocosImage->setScale( 2.5f ); // change the transform anchor point to 0,0 (optional) cocosImage->setAnchorPoint( ccp(0,0) ); // Middle layer: a Tile map atlas CCTileMapAtlas *tilemap = CCTileMapAtlas::tileMapAtlasWithTileFile(s_TilesPng, s_LevelMapTga, 16, 16); tilemap->releaseMap(); // change the transform anchor to 0,0 (optional) tilemap->setAnchorPoint( ccp(0, 0) ); // Anti Aliased images tilemap->getTexture()->setAntiAliasTexParameters(); // background layer: another image CCSprite* background = CCSprite::spriteWithFile(s_back); // scale the image (optional) background->setScale( 1.5f ); // change the transform anchor point (optional) background->setAnchorPoint( ccp(0,0) ); // create a void node, a parent node CCParallaxNode* voidNode = CCParallaxNode::node(); // NOW add the 3 layers to the 'void' node // background image is moved at a ratio of 0.4x, 0.5y voidNode->addChild(background, -1, ccp(0.4f,0.5f), CCPointZero); // tiles are moved at a ratio of 2.2x, 1.0y voidNode->addChild(tilemap, 1, ccp(2.2f,1.0f), ccp(0,-200) ); // top image is moved at a ratio of 3.0x, 2.5y voidNode->addChild(cocosImage, 2, ccp(3.0f,2.5f), ccp(200,800) ); // now create some actions that will move the 'void' node // and the children of the 'void' node will move at different // speed, thus, simulation the 3D environment CCActionInterval* goUp = CCMoveBy::actionWithDuration(4, ccp(0,-500) ); CCActionInterval* goDown = goUp->reverse(); CCActionInterval* go = CCMoveBy::actionWithDuration(8, ccp(-1000,0) ); CCActionInterval* goBack = go->reverse(); CCFiniteTimeAction* seq = CCSequence::actions(goUp, go, goDown, goBack, NULL); voidNode->runAction( (CCRepeatForever::actionWithAction((CCActionInterval*) seq) )); addChild( voidNode ); }
NS_CC_BEGIN // implementation CCTileMapAtlas CCTileMapAtlas * CCTileMapAtlas::create(const char *tile, const char *mapFile, int tileWidth, int tileHeight) { CCTileMapAtlas *pRet = new CCTileMapAtlas(); if (pRet->initWithTileFile(tile, mapFile, tileWidth, tileHeight)) { pRet->autorelease(); return pRet; } CC_SAFE_DELETE(pRet); return NULL; }
Parallax2::Parallax2() { setIsTouchEnabled( true ); // Top Layer, a simple image CCSprite* cocosImage = CCSprite::spriteWithFile(s_Power); // scale the image (optional) cocosImage->setScale( 2.5f ); // change the transform anchor point to 0,0 (optional) cocosImage->setAnchorPoint( ccp(0,0) ); // Middle layer: a Tile map atlas CCTileMapAtlas* tilemap = CCTileMapAtlas::tileMapAtlasWithTileFile(s_TilesPng, s_LevelMapTga, 16, 16); tilemap->releaseMap(); // change the transform anchor to 0,0 (optional) tilemap->setAnchorPoint( ccp(0, 0) ); // Anti Aliased images tilemap->getTexture()->setAntiAliasTexParameters(); // background layer: another image CCSprite* background = CCSprite::spriteWithFile(s_back); // scale the image (optional) background->setScale( 1.5f ); // change the transform anchor point (optional) background->setAnchorPoint( ccp(0,0) ); // create a void node, a parent node CCParallaxNode* voidNode = CCParallaxNode::node(); // NOW add the 3 layers to the 'void' node // background image is moved at a ratio of 0.4x, 0.5y voidNode->addChild(background, -1, ccp(0.4f,0.5f), CCPointZero); // tiles are moved at a ratio of 1.0, 1.0y voidNode->addChild(tilemap, 1, ccp(1.0f,1.0f), ccp(0,-200) ); // top image is moved at a ratio of 3.0x, 2.5y voidNode->addChild( cocosImage, 2, ccp(3.0f,2.5f), ccp(200,1000) ); addChild(voidNode, 0, kTagNode); }
//------------------------------------------------------------------ // // TileMapEditTest // //------------------------------------------------------------------ TileMapEditTest::TileMapEditTest() { CCTileMapAtlas* map = CCTileMapAtlas::create(s_TilesPng, s_LevelMapTga, 16, 16); // Create an Aliased Atlas map->getTexture()->setAliasTexParameters(); CCSize s = map->getContentSize(); CCLOG("ContentSize: %f, %f", s.width,s.height); // If you are not going to use the Map, you can free it now // [tilemap releaseMap); // And if you are going to use, it you can access the data with: schedule(schedule_selector(TileMapEditTest::updateMap), 0.2f); addChild(map, 0, kTagTileMap); map->setAnchorPoint( ccp(0, 0) ); map->setPosition( ccp(-20,-200) ); }
//------------------------------------------------------------------ // // TileMapTest // //------------------------------------------------------------------ TileMapTest::TileMapTest() { CCTileMapAtlas* map = CCTileMapAtlas::create(s_TilesPng, s_LevelMapTga, 16, 16); // Convert it to "alias" (GL_LINEAR filtering) map->getTexture()->setAntiAliasTexParameters(); CCSize s = map->getContentSize(); CCLOG("ContentSize: %f, %f", s.width,s.height); // If you are not going to use the Map, you can free it now // NEW since v0.7 map->releaseMap(); addChild(map, 0, kTagTileMap); map->setAnchorPoint( ccp(0, 0.5f) ); CCScaleBy *scale = CCScaleBy::create(4, 0.8f); CCActionInterval *scaleBack = scale->reverse(); CCFiniteTimeAction* seq = CCSequence::create(scale, scaleBack, NULL); map->runAction(CCRepeatForever::create((CCActionInterval *)seq)); }