Other node types

Source: Internet
Author: User
Tags addchild

Other node types

Respect for the original: http://cn.cocos2d-x.org/tutorial/show?id=2497

You are using sprites, tags, actions, and your game has made some progress. In addition to the basic node types described in the previous chapters, Cocos2d-x also provides more advanced node types to help build special features. Maybe you want to make a game based on tile maps? Or is it a 2D side-scroller game? Or, you might want to add a particle effect to the game? Cocos2d-x provides a node object to help you achieve these goals!

Tile map

The tile map is a map made up of tiles. Each tile can have independent behavior. TMX is an XML-based map format that was originally designed to design tiled maps, but because it supports a variety of object types, it is also suitable for more common games. The TMX object can be easily created by:

// reading in a tiled map. Auto map = tmxtiledmap::create ("tilemap.tmx"0,// 

Tile maps can have multiple layers, determined by z-order. You can access a specific layer through the layer name:

// How to get a specific layerauto map = tmxtiledmap::create ("tilemap.tmx"= Map->getlayer ("Layer0"= Layer->gettileat (VEC2 (1,63 ));

Each tile has a unique location and ID. So it's easy to pick a particular tile from it. You can access any tile by ID:

int gid = Layer->gettilegidat (VEC2 (0,  

Take the layout of the tile map as an example:

How do I create a tile map? There are many tools that can be created. Tiled is a very popular tool. It is fast-growing and has an excellent user community. The above screen is an example of a tiled project.

Particle systems

Your game may require some similar effects like burning flames, spells cast, or explosions. How can these complex effects be achieved? Or, can it be achieved? The answer is yes. Use the particle system. The word "particle system" refers to a computer graphics technique that uses a large number of very small sprites or other graphical objects to simulate a vague phenomenon that is difficult to achieve using traditional rendering techniques. Examples of highly chaotic systems, natural phenomena, or chemical reactions caused by processes. Here are some examples of particle effects:

Tools for creating particle effects

You can choose to always create a particle effect manually so that each property fits your liking. But we have some third-party tools that you can use to create particle effects. Including:

    • Particle Designer: A very powerful particle effect editor on Mac
    • V-play Particle Editor: This is a cross-platform particle editor on the Cocos2d-x
    • PARTICLE2DX: An online Web particle designer

These tools typically export a. plist file, cocos2d-x read the file to use the particle model you created in the game. Just like all the other classes we've touched, we're using the Create () method:

// Create by plist fileAuto Particlesystem = particlesystem::create ("spinningpeas.plist" 

Built-in particle effects

Are you ready to add particle effects to your game? We hope so! Are you still not comfortable with creating custom particle effects? For the sake of convenience, you can choose some built-in particle effects. Take a look at this list:

    • Particlefire: Point particle system. Use Gravity mode.

    • Particlefireworks: Point particle system. Use Gravity mode.

    • Particlesun: Point particle system. Use Gravity mode.

    • Particlegalaxy: Point particle system. Use Gravity mode.

    • Particleflower: Point particle system. Use Gravity mode.

    • Particlemeteor: Point particle system. Use Gravity mode.

    • Particlespiral: Point particle system. Use Gravity mode.

    • Particleexplosion: Point particle system. Use Gravity mode.

    • Particlesmoke: Point particle system. Use Gravity mode.

    • Particlesnow: Point particle system. Use Gravity mode.

    • Particlerain: Point particle system. Use Gravity mode.

To use Particlefireworks as an example, you can easily use built-in effects:

Auto emitter =Ten

The result is a particle effect similar to this:

But what if the particle effect isn't exactly what you want it to look like? Yes, you can do it manually! We still use the fireworks example above, by manually changing its properties to work in more depth:

Auto emitter =particlefireworks::create ();//Set the DurationEmitter->setduration (particlesystem::D uration_infinity);//radius ModeEmitter->Setemittermode (Particlesystem::mode::radius);//radius mode:100 pixels from cEnteremitter->setstartradius ( -); Emitter->setstartradiusvar (0); Emitter-Setendradius (Particlesystem::start_radius_equal_to_end_radius); emitter->setendradiusvar (0);//Not used when start = = EndAddChild (Emitter,Ten);

Disparity

A parallax node is a special type of node that simulates a rolling parallax. What did you say? A view of ... What the? Yes, parallax. In short, you can think of a parallaxnode as a special effect, a special effect that allows us to view an object from a different location, and the position or direction of the object will be different. For some simple examples, such as when we look from the viewfinder or from the camera lens. You can think of many games that use this feature, like the classic Super Mario. Parallaxnode objects can be moved by sequence or manually by mouse, touch, accelerometer, or keyboard events.

Parallax nodes are slightly more complex than normal nodes. Why is it? Because parallax nodes need to use multiple nodes to work. Only one parallaxnode is not working properly. You need at least two additional node objects to complete a parallaxnode. Often, in a real cocos2d-x craze, Parallaxnode can be easily created:

// Create Parallaxnode

Because you need multiple node objects, they are also easy to add:

//Create ParallaxnodeAuto Paranode =parallaxnode::create ();//background image is moved at a ratio of 0. 4x, 0. 5yParanode->addchild (Background,-1,VEC2 (0.4f,0.5f), Vec2::zero); //Tiles is moved at a ratio of 2. 2x, 1. 0yParanode->addchild (Middle_layer,1,VEC2 (2.2f,1.0f), VEC2 (0,- $) ); //top image is moved at a ratio of 3. 0x, 2. 5yParanode->addchild (top layer,2,VEC2 (3.0f,2.5f), VEC2 (, -) );

OK, both look and feel familiar, right? Pay attention to a few things! Each of the added node objects is assigned a unique Z-order value, so that their z-order values are superimposed and higher. Also notice the parameters of the other two Vec2 types called in Addchild (). The two parameters are proportional and offset, respectively. These parameters can be thought of as the speed ratio of the parent node.

It is difficult to express parallaxnode in textual form, so run the sample Code "Programmer Guide Sample" To learn about its application in practice!

Other node types

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.