that’s the .cpp file of my class. Hopefully that will help.
The class TileMapLayer is instantiated once when the scene “HouseScene” is loaded.
`#include “TileMapLayer.h”
#include “AppDelegate.h”
#include “HouseScene.h”
#define kItemMenuWidth 171
#define kItemMenuHeight 68
#define kFakeBkrdOffsetY 40
using namespace cocos2d;
TileMapLayer::TileMapLayer():
avatar(NULL), studioAvatars(NULL), tileMap(NULL), voidNode(NULL), backgroundParalax(NULL), backgroundStudio(NULL),
backgroundStudioFront(NULL), backgroundTower(NULL), backstageArrowMenu(NULL), itemMenu(NULL), moveMenu(NULL),
buyMenu(NULL), interactionStatusLabel(NULL), movingMap(false), zoomingMap(0), playerEnRoute(false),
newWalkWaiting(false), waitingTileCoord(ccp(0,0)), chosenPathNodes(NULL), stageWalking(false), currentContext(0),
activated(false), allowMiniGame(false), currentItem(NULL), currentItemCoord(ccp(0,0)), currentUpperItem(NULL),
onArrivalTask(0), padZoomStatus(0), scrollMaxY(0), scrollMaxX(0), scrollMinX(0), scrollMinY(0)
{
}
TileMapLayer::~TileMapLayer()
{
CCLayer::release();
}
bool TileMapLayer::init()
{
if (!CCLayer::init() )
{
return false;
}
this->setIsTouchEnabled(true);
activated = true;
AppDelegate* myDelegate = (AppDelegate*)CCApplication::sharedApplication;
// Create a void Node, parent Node
voidNode = CCParallaxNode::node();
voidNode->setAnchorPoint(ccp(0.5, 0.5));
voidNode->setPosition(ccp(240, 160));
this->addChild(voidNode, 0);
// If BETTER than LOW spec, show beach bkrd
if (myDelegate->getDeviceSpecGroup() > eDeviceSpec_Low) {
//Texture Format Setting
CCTexture2D::setDefaultAlphaPixelFormat(kTexture2DPixelFormat_RGB565);
//PARALLAX BACKGROUND
backgroundParalax = CCSprite::spriteWithFile("background_800.png");
backgroundParalax->setScale(1.2);
voidNode->addChild(backgroundParalax, 0, ccp(0.2f, 0.2f), ccp(185, 130));
backgroundParalax->setVertexZ(-1002);
myDelegate->revertToDefaultPixelFormat();
}else{
//PARALLAX BACKGROUND
backgroundParalax = CCSprite::spriteWithFile("background_3g_city.png", CCRectMake(0, 0, 256*4, 256*4));
ccTexParams params = {GL_LINEAR,GL_LINEAR,GL_REPEAT,GL_REPEAT};
backgroundParalax->getTexture()->setTexParameters(¶ms);
backgroundParalax->setBlendFunc((ccBlendFunc){GL_ONE,GL_ZERO});
voidNode->addChild(backgroundParalax, 0, ccp(0.2f, 0.2f), ccp(185, 130));
backgroundParalax->setVertexZ(-1002);
}
tileMap = CCTMXTiledMap::tiledMapWithTMXFile("MyStar7.tmx");
tileMap->setTag(1);
voidNode->addChild(tileMap, 0, ccp(1.0f,1.0f), ccp(-480,-320));
int nbChildren = tileMap->getChildren()->count();
for(int i = 0; i < nbChildren; i++){
CCSprite* child = (CCSprite*)(tileMap->getChildren()->objectAtIndex(i));
child->getTexture()->setAntiAliasTexParameters();
}
// If BETTER than LOW spec, show beach bkrd
if (myDelegate->getDeviceSpecGroup() > eDeviceSpec_Low) {
//Texture format settings
CCTexture2D::setDefaultAlphaPixelFormat(kTexture2DPixelFormat_RGB5A1);
// TOWER BACKGROUND
backgroundTower = CCSprite::spriteWithFile("background_large_10.png");
backgroundTower->setAnchorPoint(ccp(0, 0));
backgroundTower->setPosition(ccp(-13, -90));
tileMap->addChild(backgroundTower, -1);
backgroundTower->setVertexZ(-1001);
//Texture format settings
myDelegate->revertToDefaultPixelFormat();
}else{
this->loadBuildingBase3G(6);
}
// Remove the objects entered in Tiled Application
for(int i = 0; i < 7; i++)
this->setTileInLayer(i, ccp(kTileMapSize-1, kTileMapSize-(i+1)), 0);
return true;
}
void TileMapLayer::loadBuildingBase3G(int size)
{
if (backgroundTower != NULL) {
backgroundTower->removeAllChildrenWithCleanup(true);
tileMap->removeChild(backgroundTower, true);
backgroundTower = NULL;
}
CCLog("Resetting tower with size %d", size);
CCString* tmpString = CCString::stringWithFormat("background_3g_tower_%d.png", size);
backgroundTower = CCSprite::spriteWithFile((const char *)tmpString->toChar(), CCRectMake(0, 0, (80 * (size) + 4), 64 * 6));
backgroundTower->setAnchorPoint(ccp(0, 0));
backgroundTower->setPosition(ccp((40 * (12 - size)) -1, 65 - (size * 25)));
ccTexParams buildingParams = {GL_LINEAR,GL_LINEAR,GL_REPEAT,GL_REPEAT};
backgroundTower->getTexture()->setTexParameters(&buildingParams);
backgroundTower->setBlendFunc((ccBlendFunc){GL_ONE,GL_ZERO});
tileMap->addChild(backgroundTower, -1001);
backgroundTower->setVertexZ(-1001);
}
///////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////// TILES ///////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////
#pragma mark Tiles
void TileMapLayer::setTileInLayer(int layerId, cocos2d::CCPoint coord, int type)
{
if(layerId < 0)
return;
CCTMXLayer * layer = (CCTMXLayer*)tileMap->getChildByTag(layerId);
//coord = CCDirector::sharedDirector()->convertToGL(coord);
layer->setTileGID(type, coord);
}
void TileMapLayer::setTileInLayer(int layerId, cocos2d::CCPoint coord, int type, cocos2d::ccColor3B colour)
{
if(layerId < 0)
return;
CCTMXLayer * layer = (CCTMXLayer*)tileMap->getChildByTag(layerId);
coord = CCDirector::sharedDirector()->convertToGL(coord);
layer->setTileGID(type, coord);
CCSprite* tileSprite = this->getTileSpriteFromLayer(layerId, coord);
if (tileSprite != NULL) {
tileSprite->setColor(colour);
}
}
///////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////// TILE SPRITES ////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////
#pragma mark Tile Sprites
CCSprite * TileMapLayer::getTileSpriteFromLayer(int layerId, CCPoint coord){
if(layerId < 0)
return NULL;
CCTMXLayer * layer = (CCTMXLayer *)tileMap->getChildByTag(layerId);
// Get openGL coordinate
coord = CCDirector::sharedDirector()->convertToGL(coord);
// Return sprite from layer
return layer->tileAt(coord);
}`