Figure:
Program code:
Header file declaration functions and variables
void registerWithTouchDispatcher(void);bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent);void ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent);void setViewpointCenter(CCPoint position) ;void setSpritePoint(CCPoint position) ;CCPoint tileCoordForPosition(CCPoint position); CCSprite *sprite1; CCTMXTiledMap *map;
Create a tiled map in the init () method to obtain the object coordinates.
map = CCTMXTiledMap::create("map1.tmx");this->addChild(map);CCTMXObjectGroup *object = map->objectGroupNamed("object1");CCDictionary *sprite_object = object->objectNamed("sprite1");
And load the genie
Float x = (CCString *) sprite_object-> objectForKey ("x")-> floatValue (); float y = (CCString *) sprite_object-> objectForKey ("y")-> floatValue (); CCLOG ("sprite postion x: % f, % f", x, y); sprite1 = CCSprite :: create ("yezhu.png"); sprite1-> setPosition (ccp (x, y); this-> addChild (sprite1); setTouchEnabled (true); // enable the touch event
Then reload the touch event
void HelloWorld::registerWithTouchDispatcher(){CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this,0, true);}bool HelloWorld::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent){ return true;}
Process touch coordinates and genie Coordinate Position variables to move the genie following the touch point
Void HelloWorld: ccTouchEnded (CCTouch * pTouch, CCEvent * pEvent) {CCPoint touchlocation = convertTouchToNodeSpace (pTouch); CCPoint touchsprite = sprite1-> getPosition (); CCPoint mpoint; CCLOG ("touchlocationX: % f, Y: % f touchspriteX: % f, Y: % f", touchlocation. x, touchlocation. y, touchsprite. x, touchsprite. y); // click the coordinate X axis minus the absolute value of the X axis of the genie is greater than the value of the clicked coordinate minus the X axis of the genie if (fabs (touchlocation. x-touchsprite.x)> fabs (touchlocation. y-touchsprite.y) {// the X axis of the clicked coordinate is greater than the X axis of the genie in the right if (touchlocation. x> touchsprite. x) {mpoint = ccp (touchsprite. x + 32, touchsprite. y); // the X axis of the genie increments} else // the X axis of the clicked coordinate is smaller than the X axis of the Genie. {mpoint = ccp (touchsprite. x-32, touchsprite. y ); // decrease the X axis of the genie} // click the X axis minus the absolute value of the X axis of the Genie. The value of the absolute value of the X axis is less than that of the clicked coordinate. On the X axis of the genie, subtract the else X axis from the X axis of the genie. upper side if (touchlocation. y> touchsprite. y) {mpoint = ccp (touchsprite. x, touchsprite. y + 32); // step-by-step movement of the genie y axis} else // the y axis of the clicked coordinate is smaller than that of the genie y axis at the lower side of the genie {mpoint = ccp (touchsprite. x, touchsprite. y-32); // spritepoint (mpoint); // sets the new coordinate movement Genie and detects collision CCPoint sp_point = sprite1-> getPosition (); setViewpointCenter (sp_point); // sets the view center point}
Perspective follows the wizard
void HelloWorld::setViewpointCenter(CCPoint position) { CCSize winSize = CCDirector::sharedDirector()->getWinSize(); float x = MAX(position.x, winSize.width / 2); float y = MAX(position.y, winSize.height / 2); x = MIN(x, (map->getMapSize().width*map->getTileSize().width) - winSize.width / 2); y = MIN(y, (map->getMapSize().height*map->getTileSize().height)- winSize.height / 2);CCPoint acturalPosition = ccp(x, y); CCPoint centerView = ccp(winSize.width / 2, winSize.height / 2); CCPoint viewPoint = ccpSub(centerView, acturalPosition);this->setPosition(viewPoint);}
Detects the collision with the stone element. If the stone element property is true, the genie will not be moved; otherwise, the genie will be moved.
Void HelloWorld: setSpritePoint (CCPoint position) {CCPoint tilecoord = this-> tileCoordForPosition (position ); // convert the coordinates to tile coordinates CCTMXLayer * layer = map-> layerNamed ("layer1"); // obtain the map layer int tilegid = layer-> tileGIDAt (tilecoord ); // obtain the GID value CCLOG ("tilegid: % d", tilegid); if (tilegid) {CCDictionary * properties = map-> propertiesForGID (tilegid ); // obtain the if (properties) {const CCString * str = properties-> valueForKey ("ShiTou") set of graphic attribute key values based on the GID value "); // key value name if (str & str-> compare ("true") = 0) // key value return ;}} sprite1-> setPosition (position );}
Convert coordinates to tile coordinates
CCPoint HelloWorld::tileCoordForPosition(CCPoint position){int x = position.x / map->getTileSize().width;int y = ((map->getMapSize().height * map->getTileSize().height) - position.y) / map->getTileSize().height;return ccp(x, y);}