Skip to content

Latest commit

 

History

History
51 lines (35 loc) · 1.93 KB

7.md

File metadata and controls

51 lines (35 loc) · 1.93 KB

Chapter 7: Other Node Types

TMX and Fast TMX

TMX is an XML-based map format originally designed for tile-based maps but meanwhile also suitable for more generic game levels due to its support for various object types. Tile-based maps can have many layers, determined by a z-order. Each tile has a unique position and id.

// reading in a tiled map.
cocos2d::TMXTiledMap* map = cocos2d::TMXTiledMap::create("TileMap.tmx");
addChild(map, 0, kTagTileMap);

// how to get a specific layer and tile
auto layer = map->layerNamed("Layer 0");
auto tile = layer->tileAt(cocos2d::Vec2(1, 63));

// to obtain a specific tiles id
unsigned int gid = layer->tileGIDAt(cocos2d::Vec2(0, 63));

Example Tilemap concepts

Particle

Parallax

A Parallax Node is a special Node type that simulates a parallax scroller. You can think of many games that function this way, Super Mario Bros being a classic example. Parallax Nodes can be moved around by a Sequence and also manually by mouse, touch, accelerameter or keyboard events.

Parallax nodes are a bit more complex than regular nodes. Why? Because they require the use of multiple nodes to function.

An example of adding multiple Nodes to a Parallax node.

// create a void node, a parent node
auto voidNode = ParallaxNode::create();
    
// NOW add the 3 nodes to the 'void' node

// background image is moved at a ratio of 0.4x, 0.5y
voidNode->addChild(background, -1, Vec2(0.4f,0.5f), Vec2::ZERO);
    
// tiles are moved at a ratio of 2.2x, 1.0y
voidNode->addChild(tilemap, 1, Vec2(2.2f,1.0f), Vec2(0,-200) );
    
// top image is moved at a ratio of 3.0x, 2.5y
voidNode->addChild(cocosImage, 2, Vec2(3.0f,2.5f), Vec2(200,800) );

OK, looks and feels familiar, right? Notice a few items! Each Node is given a unique z-order so that they stack on top of each other. Also notice the additional parameters to the addChild() call.