Principle of obstacle implementation in cocos2d-x game

Source: Internet
Author: User

Maybe everyone in cocos2d-x learning, may seldom use map. (When I first learned cocos2d) I also tried to use maps, but I couldn't use them to do anything. In my opinion, what was this map?It's no different from loading a background image using the genie method! Then I did not use the map because I did not know the map function. Later, I found that the background alone cannot meet the needs of the actual project (specifically

I won't explain what it means ). So I went back and studied the map. As for my current learning situation, I think it is not difficult to do the map. If I want to study it specially.

Let me tell you two points about the map coordinate system and the screen coordinate system (1): I will not talk much about what others have said (for example, what is the Y axis to the next up and so on. Normally,); I want to say that the screen coordinates are the basic elements (points), while the map coordinate system is the basic element of the Square: Like this:


A grid. Each grid represents a coordinate. The size of the grid is determined by ourselves when we create a map!

(Second, I'm sorry to forget it. I'll try again later !) <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> Expires + expires/CwLS + zbj4tPO80rfWz + expires/N + expires/Wy/expires + expires/expires + wrc7vo6zTzs + 3s6G + expires + ax48z5teO0 + sLro6zT0NDo0qq1xL/release + PHN0cm9uZz66w8/Cw + a/release + sLrtPO80s/release + CjxwPjxzdHJvbmc + pgltzybzcm9 "http://www.bkjia.com/uploadfile/Collfiles/20131225/2013122509343853.png" alt = "\">

The red area is a layer in the map. You can use map-> layername ("Layer"); to obtain the Layer. Then, you can see which red grids represent the sprite of the Layer. We can use the map lattice coordinates to obtain these genie. (then you may ask how to easily obtain coordinates? To show you a picture :)

As you can see, this (7, 21) is an genie in the map. This is a good way to get it;


To implement obstacles, we must first convert the coordinates of the protagonist botnets into map lattice coordinates (and dynamically convert them );

Void JsLayer: update (float dt) {conversion process! CCSize mapTiledNum = _ map-> getMapSize (); CCSize tiledSize = _ map-> getTileSize (); CCPoint point1 = this-> _ jsSprite-> getPosition (); point1.x = point1.x-this-> speed; this-> _ jsSprite-> setPosition (ccp (point1.x, point1.y); int x = this-> _ jsSprite-> getPosition (). x/tiledSize. width; int y = this-> _ jsSprite-> getPosition (). y/tiledSize. height;/* the default Y coordinate of the Cocos2d-x is from bottom to top, so do a subtraction operation */y = mapTiledNum. height-y; int TiledGid = _ map-> layerNamed ("zhangai")-> tileGIDAt (ccp (x, y); if (tiledGid! = 0) {CCDictionary * propertiesDict = _ map-> propertiesForGID (tiledGid ); /* Get the zhezhao attribute value of the grid */const CCString * prop = propertiesDict-> valueForKey ("zhezhao");/* determine whether the zhezhao attribute is true. If yes, do not move players */if (prop-> m_sString.compare ("true") = 0) {this-> unscheduleUpdate ();}}}

So we can: Is it easy!





Related Article

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.