19: Tile map Collision Detection in Cocos2D-x Game Development

Source: Internet
Author: User

Yesterday we had the genie and the background, and the genie could run, but we couldn't let the genie run endlessly. So today we are going to add some obstacles to the map;

First open the TMX map of the genie, then we will customize a 32x32 pixel image to add it to the layer, and then set the attribute of this layer to "true ", next, create a new layer named "meta" in the map. Now use your creativity to add these little items to this layer and save them.

Next we should do some operations in the code to open our Player class:

class Player :public Entity{public:Player(void);~Player(void);public:CREATE_FUNC(Player);virtual bool init();void run();virtual bool SetPlayerPosition(int x,int y) ;void SetViewPointByPosition();CCTMXTiledMap *m_map;void SetTMXTileMap(CCTMXTiledMap *map);CCTMXLayer *meta;CCPoint tileCoordForPosition(CCPoint point);};

Player: Player (void) {} Player ::~ Player (void) {} bool Player: init () {return true;} void Player: run () {CCSpriteFrameCache * freamCache = CCSpriteFrameCache: sharedSpriteFrameCache (); freamCache-> addSpriteFramesWithFile ("run. plist "," run.png "); CCSpriteFrame * frame = NULL; CCArray * freamlist = CCArray: create (); for (int I = 1; I <= 15; I ++) {frame = freamCache-> spriteFrameByName (CCString: createWithFormat ("run0000d.png", I)-> getCString (); freaml Ist-> addObject (frame);} ccanimations * anination = CCAnimation: createWithSpriteFrames (freamlist); anination-> setLoops (-1); anination-> setDelayPerUnit (0.08f ); CCAnimate * animate = CCAnimate: create (anination); m_sprite-> runAction (animate);} void Player: SetViewPointByPosition () {if (m_sprite = NULL) {return ;} CCLayer * parent = (CCLayer *) this-> getParent (); CCSize mapTiledNum = m_map-> getMapSize (); CCSize tiled Size = m_map-> getTileSize (); CCSize mapsize = CCSize: CCSize (mapTiledNum. width * tiledSize. width, mapTiledNum. height * tiledSize. height); CCSize visibleSize = CCDirector: shareddire()-> getWinSize (); CCPoint spritepoint = getPosition (); float x = max (spritepoint. x, visibleSize. width/2); float y = max (spritepoint. y, visibleSize. height/2); x = min (x, mapsize. width-visibleSize.width/2); y = min (y, mapsize. height- VisibleSize. height/2); CCPoint destPos = CCPoint: CCPoint (x, y); CCPoint centerpos = CCPoint: CCPoint (visibleSize. width/2, visibleSize. height/2); CCPoint viewpos = ccpSub (centerpos, destPos); parent-> setPosition (viewpos);} bool Player: SetPlayerPosition (int x, int y) {CCSize spritesize = m_sprite-> getContentSize (); CCPoint dspoint = CCPoint (x + spritesize. width/2, y); CCPoint tiledpos = tileCoordForPosition (ccp (Dspoint. x, dspoint. y); CCLOG ("tiled x = % f, y = % f", tiledpos. x, tiledpos. y); int tiledchild = meta-> tileGIDAt (tiledpos); if (tiledchild! = 0) {CCDictionary * propertydict = m_map-> propertiesForGID (tiledchild); const CCString * prop = propertydict-> valueForKey ("colpolicable "); if (prop-> m_sString.compare ("true") = 0) {return false ;}entity: SetPlayerPosition (x, y); SetViewPointByPosition (); return true ;} void Player: SetTMXTileMap (CCTMXTiledMap * map) {// CC_SAFE_RETAIN (map); // CC_SAFE_RELEASE (m_map); this-> m_map = map; this-> meta = m_map-> layerNamed ("meta"); // set the layer to be invisible this-> meta-> setVisible (false ); this-> meta-> retain ();}
 
// Because the genie coordinates are calculated from the lower left corner, and the tile coordinates are calculated from the upper left corner, we need to use this function to convert the genie coordinates to the required coordinates. CCPoint Player: tileCoordForPosition (CCPoint pos) {CCSize mapTiledNum = m_map-> getMapSize (); CCSize tiledSize = m_map-> getTileSize (); int x = pos. x/tiledSize. width; // The X-coordinate int y = (640-pos. y)/tiledSize. height; // if (x> 0) {x-= 1;} if (y> 0) {y-= 0 ;} return ccp (x, y );}


In this way, our genie will encounter obstacles while running and cannot run.



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.