Cocos2d Study Notes (6) ccparallaxnode and tile Map

Source: Internet
Author: User

If your game requires a larger scenario, you will certainly use ccparallaxnode or tile Map

We know that when we move, we will see objects that are closer to us, moving faster and farther, such as mountains in the distance will move slowly, objects in the farthest distance, such as the sun, almost do not move. This phenomenon is called Parallax. imitating the parallax in the game can make the players feel that the role in the game is indeed moving. Ccparallaxnode can easily create a parallax layer. You can control the parallax rate, position, and level of each layer.

parallaxNode = [CCParallaxNode node]; [parallaxNodesetPosition:ccp(levelSize.width/2.0f,screenSize.height/2.0f)]; float xOffset = 0;// Ground moves at ratio 1,1[parallaxNode addChild:BGLayer1 z:40 parallaxRatio:ccp(1.0f,1.0f) positionOffset:ccp(0.0f,0.0f)];xOffset = (levelSize.width/2) * 0.3f; [parallaxNode addChild:BGLayer2 z:20 parallaxRatio:ccp(0.2f,1.0f)positionOffset:ccp(xOffset, 0)];xOffset = (levelSize.width/2) * 0.8f; [parallaxNode addChild:BGLayer3 z:30 parallaxRatio:ccp(0.7f,1.0f)positionOffset:ccp(xOffset, 0)]; [self addChild:parallaxNode z:10];

We can see that the Z axis has a maximum inspection rate of 40 and a minimum inspection rate of 20. That is to say, the farthest layer is only 0.2 times faster than the X axis of the current layer when moving, the Y axis speed is 1 because this code only shows the parallax on the X axis.

By the way, there is a small tip

id followAction = [CCFollow actionWithTarget:playerCharacter];[layer runAction:followAction];

Ccfollow allows your layer lens to follow the target, but this lens will follow the X and Y axes.

Next let's talk about tile map, software: http://www.mapeditor.org, software usage will not talk about it, relatively simple, and finally will generate. tmx file, in fact, this file is an XML file

Object Creation Method

Cctmxtiledmap * tiledmapwithtmxfile: @ "level2tilemapiphone. tmx"];

The advantage of tile map is that no matter how big your map is, the memory usage is only the sum of different tile. Therefore, using tile map is a very memory-Saving Approach, generally, RPG games use tile map for maps.

CCTMXLayer *groundLayer = [tileMapNode                                     layerNamed:@"RockBoulderLayer"];        // 2    parallaxNode = [CCParallaxNode node];    [parallaxNode setPosition:     ccp(levelSize.width/2,screenSize.height/2)];    float xOffset = 0.0f;        // 3    xOffset = (levelSize.width/2);    [groundLayer retain];    [groundLayer removeFromParentAndCleanup:NO];    [groundLayer setAnchorPoint:CGPointMake(0.5f, 0.5f)];    [parallaxNode addChild:groundLayer z:30 parallaxRatio:ccp(1,1)             positionOffset:ccp(0,0)];    [groundLayer release];

As shown in the above Code, if there are multiple layers in the tile map, you must first retain these layers when adding them to other objects, then remove them from the tilemapnode, and then add the layers before you forget to release them, this allows his current owner to control his lifecycle.

OK. That's all. The next blog begins to write the physical engine box2d!

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.