Invisible layer in Cocos2D - cocos2d-iphone

I am new to cocos2d and i went through many tutorials on tiled map . And than , I have made one tmx map using tiled. In that i made one meta layer. Now, i stuck at one issue, i want to invisible my meta layer in my map .i used this code
[self.meta.visible = no];
meta layer is my first tile layer. i want to invisible this just for better view & i also trying for collision detection .i have also added my layer & set its property like following
CCTMXLayer * Layer;
#property (atomic,retain) CCTMXLayer * Layer;
[self addchild:self.meta];
Now, I don't know how to do this?

Try this code. Use Meta layer name according to your tileMap.
CCTMXLayer *meta = [tileMap layerNamed:#"Meta"];
meta.visible = NO;
See this image, names are case sensitive.

Related

How to add osgEarth Features at runtime?

I'm playing around with osgEarth and while it's crazy easy to add features in the .earth file, I'm struggling to do it at runtime via the API. I want to let the user draw polygons on the map/globe so I need to be able to dynamically define geometry and styles based on user input.
Right now I'm just going for a static implementation to figure out what I need to do, but for the life of me I can't get anything to show up. Here is my sample code. I've already loaded a .earth file that defines the MapNode which is what I'm using here.
// Style
osgEarth::Symbology::Style shapeStyle;
shapeStyle.getOrCreate<osgEarth::Symbology::PolygonSymbol>()->fill()->color() = osgEarth::Symbology::Color::Green;
// Geometry
osgEarth::Symbology::Polygon* polygon = new osgEarth::Symbology::Polygon();
polygon->push_back(0, 0);
polygon->push_back(0, 10);
polygon->push_back(10, 10);
// Feature
osgEarth::Features::Feature* feature = new osgEarth::Features::Feature(polygon, mapNode->getMapSRS(), shapeStyle);
// Node
osgEarth::Annotation::FeatureNode* featureNode = new osgEarth::Annotation::FeatureNode(mapNode, feature);
featureNode->setStyle(shapeStyle);
featureNode->init();
mapNode->addChild(featureNode);
This should draw a green triangle near the middle of the map, but I don't see anything. Am I wrong in assuming that my polygon points are geo coordinates (lon, lat)? Is it wrong to just create my Style and Geometry on the fly like this? What am I doing wrong?
Update: This seems to work fine on a 3D (geocentric) map, but not on a 2D (projected) map which is what I'm after.
After poking around a bit I stumbled upon the osgearth_features example that comes with the SDK which includes examples of creating features programatically. I followed the pattern from the sample and came up with something that works.
// Style
osgEarth::Symbology::Style shapeStyle;
osgEarth::Symbology::PolygonSymbol* fillStyle = shapeStyle.getOrCreate<osgEarth::Symbology::PolygonSymbol>();
fillStyle->fill()->color() = osgEarth::Symbology::Color::Green;
osgEarth::Symbology::LineSymbol* lineStyle = shapeStyle.getOrCreate<osgEarth::Symbology::LineSymbol>();
lineStyle->stroke()->color() = osgEarth::Symbology::Color::Black;
lineStyle->stroke()->width() = 2.0f;
// Geometry
osgEarth::Symbology::Polygon* polygon = new osgEarth::Symbology::Polygon();
polygon->push_back(0, 0, 10000);
polygon->push_back(0, 10, 10000);
polygon->push_back(10, 10, 10000);
// Feature Options (references the geometry)
osgEarth::Drivers::OGRFeatureOptions featureOptions;
featureOptions.geometry() = polygon;
// Model Options (references the feature options and style)
osgEarth::Drivers::FeatureGeomModelOptions geomOptions;
geomOptions.featureOptions() = featureOptions;
geomOptions.styles() = new osgEarth::StyleSheet();
geomOptions.styles()->addStyle( shapeStyle );
geomOptions.enableLighting() = false;
// Model Layer Options (created using the model options)
osgEarth::ModelLayerOptions layerOptions("test polygon", geomOptions);
mapNode->getMap()->addModelLayer(new osgEarth::ModelLayer(layerOptions));
Defining the style and geometry is more or less the same as what I was doing before (I added a line symbol this time), but in this case I'm adding a ModelLayer to the Map. That ModelLayer uses some model options that reference my style and geometry through the feature options.
I don't know if this is the best way to do it or how scalable it is (can I do this over and over thousands of times?), bit it's at least got me going,

Communitacting between CCLayers in one Scene

I have 2 CCLayers that needs to communicate with each other in separate .m files
Level1.m (CCScene with Level1 CCLayer) - Holds tiled map and player sprite
HUDLayer.m (links to top of Level1.m) - Holds all buttons
How can I get the following code in HUDLayer.m to talk to player sprite in Level1.m?
- (void)MoveUpSelected {
CCMoveTo* moveup = [CCMoveBy actionWithDuration:1 position:ccp(0,-100)];
CCSequence* sequence = [CCSequence actions: moveup, nil];
[Player runAction:sequence];
}
Please help I've been stuck on this for days. At least if someone can point me in the right direction. Thanks!
I would advice you to use you scene object to control communication between its layers.
You can create a HUD protocol and set the scene as its delegate. And for each HUD event the scene will react accordingly by accessing the proper layer (stored as its member).
This way you won't have to make this layer coupling.
To access another layer, you need a reference to it. There are many ways to do that. In your case just add one property for each layer to the CCScene class. The layers can then access the scene via their parent:
CCLayer* otherLayer = [(YourActualSceneClass*)self.parent otherLayer];
It is very important that you do not store a reference to the other layer in either layer, or if you do, make sure to make it a weak reference, or nil them in the cleanup method. Otherwise you created a retain cycle.
You'll find more info on accessing other nodes here.

Cocos2D 2.0 - Can one CCSpriteBatchNode be used for multiple classes?

I don't know if this is possible, but I would like to create one big texture atlas and use it on all classes of the application.
Can one CCSpriteBatchNode be used for multiple classes?
Suppose I create this on the main class
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:#"atlasGeral.plist"];
self.batchNodeGeneral = [CCSpriteBatchNode batchNodeWithFile:#"atlasGeral.png"];
[self addChild:self.batchNodeGeneral];
and I have another class creating CCLayers on the main class, that is initialized, before using CCSpriteBatchNode, like this:
-(id) init
{
if( (self=[super init])) {
self.bg = [CCSprite spriteWithFile: #"cCircularBG.png"];
[self addChild:self.bg];
self.dr = [CCSprite spriteWithFile: #"cCircularDR.png"];
[self addChild:self.dr];
}
return self; // self is a CCLayer
}
can this be optimized using the self.batchNodeGeneral from the main class? My idea is to replace these two sprites and others with something like [CCSprite spriteWithSpriteFrameName:...
thanks
I'm not entirely sure I follow, but I'm pretty sure the answer is yes.
CCSpriteBatchNode doesn't have anything to do with classes, it has to do with assets. The important restriction on the use of batch nodes is that every sprite in the batch needs to reference the same texture atlas. So it is perfectly fine to have one batch node for your entire application and have every gameplay class add its own sprites to that batch. This can turn into a practical issue if your texture atlas becomes larger than the maximum texture size on your target hardware (see iOS device specs for details), but if you still want to have global batches and lots of assets it's not too hard to create a batch pool indexed by texture ID, create one batch node per atlas, and whenever you create a new sprite add it to the appropriate batch.
Honestly, I feel like the whole batch node thing is a terrible kludge on Cocos2D's part that could be made almost completely transparent to developers while still retaining its efficiency, but maybe this opinion is not entirely fair since I haven't dug around in the rendering code to understand their motivations. I guess it would mess with expectations of how depth sorting works, etc., but still I don't understand why batching objects for render is made the programmer's responsibility, it should be done by the engine.
Edit to add possible solution:
-(id) initWithMainClass:(MainClass*)mc
{
if( (self=[super init])) {
self.bg = [CCSprite spriteWithSpriteFrameName: #"cCircularBG.png"];
[mc.batchNodeGeneral addChild:self.bg];
self.dr = [CCSprite spriteWithSpriteFrameName: #"cCircularDR.png"];
[mc.batchNodeGeneral addChild:self.dr];
}
return self; // self is a CCLayer
}
`
So when you initialize one of the other classes, you pass the main class instance as a parameter so you can access its fields. Or make the main/manager class a singleton or find another architecture suitable to your needs.

Can I layer a CCSprite between two CCTMXLayers?

I am making a top-down shooter that makes extensive use of TMX maps created with the "Tiled" application. Within my TMX map, I have a "Background" layer with floor tiles, which appears beneath my characters (CCSprites.)
I have another layer in the TMX file called "Foreground" which I would like to appear "above" my CCSprites, giving the illusion of them walking underneath various objects.
I tried using the vertexZ property of the CCNode class to do this:
CCTMXLayer *backgroundLayer = ...
CCSprite *spriteNode = ...
CCTMXLayer *foregroundLayer = ...
[backgroundLayer setVertexZ:1];
[spriteNode setVertexZ:2];
[foregroundLayer setVertexZ:3];
...but it turns out vertexZ actually alters the node's visual appearance within the openGL view. It effectively causes a CCNode to appear larger, or closer to the user when it has a higher vertexZ value. I don't want that- all I want is a sort of layers-of-an-impossibly-thin-cake effect, without any visual differences between the layers.
So I thought I would try altering the zOrder property of the nodes, like this:
[[backgroundLayer parent] reOrderChild:backgroundLayer z:1];
[[spriteNode parent] reOrderChild:backgroundLayer z:2];
[[foregroundLayer parent] reOrderChild:backgroundLayer z:3];
But I realized there's a fundamental problem with what I'm doing here, since my spriteNode is a direct child of the CCScene, but the background and foreground nodes are both children of my CCTMXTiledMap, which itself is a child of the CCScene.
So I'm basically trying to slip a CCSprite between two layers of the map, which, from the CCScene's perspective, are really just two parts of the same layer.
It seems I could create an additional instance of CCTMXTiledMap just to hold the foreground layer, but that also seems like overkill. My other thought was to create CCSprites to serve the same purpose, but it seems like there's got to be a better way.
Yes, I have used Tiled once very lightly and I do believe there is an option to add an Object Layer into your TMXTiledMap (Tiled -> Layer -> Add Object Layer...), then once imported into your build you can link up a CCSprite with the corresponding Object Layer you have created. I would post your question on the cocos2d forum, as people there are more experienced and equipped to answer this with examples.

Cocos2d tiled map add sprites

In the isometric tiled map z-ordering example for cocos2d-iphone, they use an object that is already in the tilemap as the player sprite, which is rendered as moving through the trees.
Is there a way to use a random CCNode, created though code (not in the tmx file, and not part of the sprite sheet), and have it z-ordered correctly with the tilemap? addChild is not supported on a CCTMXLayer, and the gives an error when using that reads:'addChild: is not supported on CCTMXLayer. Instead use setTileGID:at:/tileAt:'.
There's got to be a way to have a CCNode (let's say a simple CCSprite) z-order correctly with a tilemap, either using cocos2d's API, or some z-buffer technique. Any pointers?
The CCTMXLayer doesn't support adding tiles at runtime because it's implemented with all sprites on a single spritesheet. Because of this implementation detail you also can't call setTexture on an individual tile.
The easiest solution would be to have the sprite you want to use on the spritesheet at the beginning. If you can't do this because it's generated at runtime or something, the next best thing (without touching cocos2d code) would be to modify the spritesheet.
Put a dummy tile in the spritesheet you're using for the TMXLayer and then, once you have the image you want to use, write it to the spritesheet using CCRenderTexture or something and use the newly generated Texture as your TMXLayer's texture.
You could also modify the CCTMXLayer to allow for your functionality but it sounds like you want to avoid that.
If you want to add a tile to say tile (x,y) ( (x,y) in Tiled Map editor coordinates ) then use the following code -
myTileMap is the reference to the CCTMXTiledMap object.
CCTMXLayer *layer=[myTileMap layerNamed:#"yourlayer"];
NSAssert(floorLayer !=nil, #"Ground layer not found!");
CGPoint tileAddPosition = [layer positionAt: CGPointMake(x,y)];
//Create your CCNode or CCSprite or whatever...say object name is **tileToBeAdded**
tileToBeAdded.anchorPoint = CGPointZero;
tileToBeAdded.position = tileAddPosition;
[myTileMap addChild:addedTile z:1];