[Stupid wood cocos2dx 040] War fog effect Chapter 2 _ accurately obtain the tile position on the screen

Source: Internet
Author: User

Missed the previous chapter?

It doesn't matter. The portal is here:

Chapter 2 of war fog effect _ exploring, not opening all maps!

War fog effect Chapter 2 _ add a map first

In this section, we will make some preparations, but this is very interesting. Let's try to click the screen to get the position of the clicked tile lattice.

(Xiao RuO: I don't understand it !)

 

What do you mean? Flowers? No, it's your heart ~
Reprinted please note, original address: http://www.benmutou.com/blog/archives/482

 

Body:

1. What is the coordinate of the tile?

In this case, we created a 10 × 10 tmx map in the previous section. The position of the tile lattice refers to the position in the 100 grids, the position of the tile lattice is represented by a point in two dimensions.

(Xiao RuO: Hello, I still don't understand it !)

Finally, you will understand:

 

For example, the coordinates of the tile lattice in the upper left corner are (0, 0 ).

(Xiao RuO: Success! Why do you want to put images earlier)

 

2. Go to the topic

Okay. Start encoding!

Modify the cctouchbegan function of helloworldscene. cpp:

bool HelloWorld::ccTouchBegan( CCTouch *pTouch, CCEvent *pEvent ) { CCPoint tiledMapPos = getMapTiledPos( cloudMap, CCDirector::sharedDirector()->convertToGL(pTouch->getLocationInView())); CCLOG("TiledMapPos x = %d, y = %d", tiledMapPos.x, tiledMapPos.y); return true; }

 

The converttogl function is used to convert a screen click to a coordinate system of a Cocos2d-x.

(Small if: I know, the screen click coordinates is in the upper left corner of the origin, while the Cocos2d-x is the Cartesian coordinate system, is in the lower left corner of the origin .)

 

The narration was unexpectedly smart once. Then, use the getmaptiledpos function to convert the click coordinate to the tile coordinate. It's very simple. Don't explain it.

(Xiao RuO: What is getmaptiledpos? Are you not going to explain it ?)

 

To reward voiceover for being smart once, let me make an exception. Let's take a look at this function:

Cocos2d: ccpoint helloworld: getmaptiledpos (cctmxtiledmap * map, ccpoint POS) {ccsize mapsize = map-> getmapsize (); ccsize tiledsize = map-> gettilesize (); int imapheight = mapsize. height * tiledsize. height;/* POS is the coordinate of the Cartesian coordinate system, so the Y axis needs to be corrected */INT x = POS. x/tiledsize. width; int y = (imapheight-pos. y)/tiledsize. height; return ccpointmake (x, y );}

In fact, this is a simple mathematical calculation. The X and Y coordinates are equivalent to the length. The size of the tile lattice is equivalent to the unit size. The length is divided by the unit size to obtain the quantity, the number is the coordinate of the tile.

(Xiao RuO: Hello, what is the Y coordinate ?)

 

For Y coordinates, I made a special processing, because the POs parameter is the coordinates in the Cartesian coordinate system, and the tile map is based on the origin in the upper left corner (the previous image, remember ?), Therefore, we need to first convert pos. Y. Usually, the conversion coordinate is to subtract y from the screen height, but because the tile map may be larger than the screen, we need to use the tile map height here.

(Xiao RuO: Actually, I already understand it. I just don't want to understand it !)

 

Run the project in debug mode and click the screen to view the following output:

 

I clicked three times on the screen and printed three logs:

Tiledmappos x = 0 and Y = 1072693248

Tiledmappos x = 0 and Y = 1073741824

Tiledmappos x = 0 and Y = 1072693248

OK. Everything is normal.

(Xiao RuO: Ah, that's strange! If X is 0, what is the astronomical number of Y ?)

 

Yes, I did. Let's look at the log printing code:

Cclog ("TiledMapPos x = %d, y = %d", tiledMapPos.x, tiledMapPos.y);

 

It is interesting to note that the X and Y attributes of ccpoint are float type, so they cannot be output in integer format. Let's try to change the debugging code to the following:

CCLOG("TiledMapPos x = %f, y = %f", tiledMapPos.x, tiledMapPos.y);

 

I changed % d to % F. Let's continue to run the project in debug mode, and click the screen at will to see the following output:

Tiledmappos x = 0.000000, y = 8.000000

Tiledmappos x = 0.000000, y = 7.000000

Tiledmappos x = 1.000000, y = 9.000000

Tiledmappos x = 3.000000, y = 9.000000

This is really normal. You can test it on your own.

(Xiao RuO: Why do I click the upper left corner, but the output is not (0, 0 )?)

 

By the way, when a tmx map is drawn to the screen, it also follows the Cartesian coordinate system. When a map is drawn to the screen, the lower left corner of the map is aligned with the lower left corner of the screen. Therefore, the local map is larger than the screen, the coordinates of the map (0, 0) are above the screen and invisible.

Now, the preparations are almost complete. In the next section, we will officially begin to write the effects of war fog.

(Xiao RuO: It's time to wait. Hello !)

 

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.