COCOS2DX 3.3 tilemap Zoom slide and click the object exactly

Source: Internet
Author: User

There have been a more basic Tilemap notes, these two days with the next 3.3 tilemap found that some things have not applied. So wrote a note, immediately feel oneself Meng Meng da.

Children's shoes that have not been played at all can see the basics


The main achievement goal:

1. Ability to scale tiledmap 3 times times

2. Mouse to Slide Tiledmap

3. In the case of zooming and sliding, clicking on a tile will determine the actual grid coordinates.


Zooming and swiping are no longer explained in detail here, there are many examples where the complete code is presented at the end. Mainly about the implementation of the third article.

As you can see, I built a Points object group and then set up two objects startpos and Endpos

Startpos probably covered 4 tiled [0,24] [1,24] [0,25] [1,25]

and set a custom attribute for startpos: ID. All I need is to get the ID value of this object when I click on the above 4 blocks of tiled.






First, I wrote a function to read the map object.

void HelloWorld::p arsetilemap () {    cclog ("Parsetilemap");    if (_tiledmap = = NULL)        return;        _objectsgroup = _tiledmap->getobjectgroup ("points");    Tilex 0  Tiley    ValueMap startpos = _objectsgroup->getobject ("Startpos");    std::string name = startpos["Name"].asstring ();    float Pointx = startpos["x"].asfloat ();    float pointy = startpos["y"].asfloat ();    float pointwidth = startpos["width"].asfloat ();    float pointheight = startpos["height"].asfloat ();    Size winsize = director::getinstance ()->getwinsize ();    Point mapPoint = _tiledmap->getposition ();    Point centerpos = Coverttiledpointtocenterpoint (Point (0,24));    Point tilepos = Covertpointtotiledpoint (Point (Pointx,pointy));    Point tilePos2 = Covertpointtotiledpoint (Centerpos);}

Very smooth get Startpos object Pointx and pointy, but the value is (0,296), not expected (1,768) suddenly a bit messy-_-!! The

Until I looked at the source Tmxlayer Getpositionat only found that the original (0,296) is already through the "resolution conversion".

VEC2 Tmxlayer::getpositionat (const vec2& POS) {    VEC2 ret = Vec2::zero;    Switch (_layerorientation)    {case    tmxorientationortho:        ret = getpositionfororthoat (POS);        break;    Case Tmxorientationiso:        ret = getpositionforisoat (POS);        break;    Case Tmxorientationhex:        ret = Getpositionforhexat (POS);        break;    Case tmxorientationstaggered:        ret = Getpositionforstaggeredat (POS);        break;    }    Cclog ("%f,%f,%f", Ret.x,ret.y,cc_content_scale_factor ());        ret = <span style= "color: #ff0000;" >CC_POINT_PIXELS_TO_POINTS</span> (ret);    return ret;}


Look at the definition of the macro:

/** @def cc_point_pixels_to_points converts a rect in PIXELS to POINTS */#define CC_POINT_PIXELS_TO_POINTS (__pixels__) 
   VEC2 ((__pixels__). X/cc_content_scale_factor (), (__pixels__). Y/cc_content_scale_factor ())/** @def CC_CONTENT_ Scale_factoron Mac It returns 1;on IPhone it returns 2 if Retinadisplay is on. Otherwise it returns 1*/#define Cc_content_scale_factor () director::getinstance ()->getcontentscalefactor ()


From the source can be seen:

1. We can be sure that the ratio of tilemap above pixel:point is 1:1, but the equipment above is not necessarily.

Mac device above Pixel:point = 1:1, iphone Retina is 2:1, the low resolution will be larger. Let's just put this ratio to the pixel size factor.

2. The cc_point_pixels_to_points can convert the coordinate value above the TILEMAP to the coordinate value above the device.

3. This setscale getscale to do something similar, but to separate the calculations.


In addition, since our Tilemap can slide the click, we need to do two extra points to convert the Tilemap absolute coordinates from the touch point:

1. Take the coordinates of the tilemaplayer as an offset

BOOL Helloworld::ontouchbegan (Touch *touch, Event  *event) {    cclog ("Helloworld::ontouchbegan");    Point touchPoint = Touch->getlocation ();    Point mapPoint = _tiledmap->getposition ();  <span style= "color: #ff0000;" > Point  realpoint = touchpoint-mappoint;</span> point    tilepoint = Covertpointtotiledpoint (realpoint );    Cclog ("Tilepoint x:%f y:%f", tilepoint.x,tilepoint.y);        Trygetobjectpropertybyposition (realpoint);    return true;}


2. Multiply the zoom factor
Parameters: Touch Coordinate string helloworld::trygetobjectpropertybyposition (point position) {Float scale = _tiledmap->getscale ();    float factor = Cc_content_scale_factor (); <span style= "color: #ff0000;"    > Float realpointx = position.x/scale; float Realpointy = position.y/scale;</span> for (Auto item: _objectsgroup->getobjects ()) {Va        Luemap curobject = Item.asvaluemap ();        float Pointx = curobject["x"].asfloat ();        float pointy = curobject["y"].asfloat ();        float pointwidth = curobject["width"].asfloat ();                float pointheight = curobject["height"].asfloat (); if (Realpointx >= pointx and Realpointx <= Pointx + pointwidth and Realpointy >= pointy and Realpointy &            lt;= Pointy + pointheight) {string objectId = curobject["id"].asstring ();            Cclog ("Helloworld:trygetobjectpropertybyposition:%s", Objectid.c_str ());        return objectId; }} return "";}


The next step is to reverse the conversion from the touch point to the Tilemappoint method is very simple.

Point Helloworld::coverttiledpointtocenterpoint (Point P) {    float scale = _tiledmap->getscale ();    float factor = Cc_content_scale_factor ();        Cclog ("Helloworld::coverttiledpointtocenterpoint scale:%f factor:%f", scale,factor);        int offsetX = _tiledmap->gettilesize (). Width/(2 * factor);    int offsetY = _tiledmap->gettilesize (). Height/(2 * factor);    tmxlayer* layer = _tiledmap->layernamed ("background");    Point point = Layer->getpositionat (p);        Point = Point (point.x  + offsetx,point.y-offsety);    Point = Point (Point.x * scale, POINT.Y * scale);    return point;}

Source







COCOS2DX 3.3 tilemap Zoom slide and click the object exactly

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.