ccp之间是不可以直接进行+,-的,要用ccpSub和ccpAdd。

1. 
http://www.cnblogs.com/buaashine/archive/2012/11/12/2765691.html 
上面有好多的关于数学的方面的知识,cocos2dx可能会用到的

2.学到了   根据tilemap坐标得到层上物体的id

int oneTiled=flagLayer->tileGIDt(tilePos);

还可以取得id对应的属性
CCDictionary* propertiesOnOneTile = tileMap->propertiesForGID(oneTileId);
const CCString* collide = propertiesOnOneTile->valueForKey("Collide");
const CCString* fruit = propertiesOnOneTile->valueForKey("fruit");

if(collide&&collide->compare("true")==0){
;
}
  else if(fruit && fruit->compare("true") == 0){
 //是一个西瓜
   先取得对应的图层,然后再去掉西瓜,去掉障碍物
   CCTMXLayer* fruitLayer = tileMap->layerNamed("foreground");
  fruitLayer->removeTileAt(tilePos); // 吃掉西瓜 得分
flagLayer->removeTileAt(tilePos); // 移除障碍

CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("pickup.wav");

this->_score++;
char str[40];
sprintf(str,"score:%d",_score);
scoreBord->setString(str);





3.源码在后面,以后用到这种逻辑的游戏的时候回来看。
#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__


#include "cocos2d.h"


class HelloWorld : public cocos2d::CCLayer
{


private:
cocos2d::CCSprite* ninja;
cocos2d::CCTMXTiledMap*  tileMap;
cocos2d::CCTMXLayer* flagLayer;
cocos2d::CCLabelTTF* scoreBord;
int _score;
void setMapPosForView(cocos2d::CCPoint playerPos);


public:
    // Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
    virtual bool init();  


    // there's no 'id' in cpp, so we recommend returning the class instance pointer
    static cocos2d::CCScene* scene();
    
    // a selector callback
    void menuCloseCallback(CCObject* pSender);
    
    // implement the "static node()" method manually
    CREATE_FUNC(HelloWorld);
bool ccTouchBegan(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent);
void ccTouchMoved(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent);
void ccTouchEnded(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent);
cocos2d::CCPoint cocoscoord2tilemapcoord(cocos2d::CCPoint pos);


};


#endif // __HELLOWORLD_SCENE_H__


#include "HelloWorldScene.h"
#include "SimpleAudioEngine.h"


USING_NS_CC;


#define GET_TILE_WIDTH tileMap->getTileSize().width
#define GET_TILE_HEIGHT tileMap->getTileSize().height




#define MAP_WIDTH (tileMap->getTileSize().width * tileMap->getMapSize().width)
#define MAP_HEIGHT (tileMap->getTileSize().height * tileMap->getMapSize().height)


#define WIN_WIDTH  (CCDirector::sharedDirector()->getWinSize().width)
#define WIN_HEIGHT  (CCDirector::sharedDirector()->getWinSize().height)


CCScene* HelloWorld::scene()
{
    // 'scene' is an autorelease object
    CCScene *scene = CCScene::create();
    
    // 'layer' is an autorelease object
    HelloWorld *layer = HelloWorld::create();


    // add layer as a child to scene
    scene->addChild(layer);


    // return the scene
    return scene;
}




// on "init" you need to initialize your instance
bool HelloWorld::init()
{
    //////////////////////////////
    // 1. super init first
    if ( !CCLayer::init() )
    {
        return false;
    }


tileMap = CCTMXTiledMap::create("MyTileMap.tmx");


CCTMXLayer* backLayer = tileMap->layerNamed("Tile Layer 1");


CCAssert(backLayer, "Can not find layer named by (Tile Layer 1)");


this->addChild(tileMap); // 地图加到layer上面


CCPoint anchorPos = tileMap->getAnchorPoint();
CCPoint mapPos = tileMap->getPosition();


CCTMXObjectGroup* og = tileMap->objectGroupNamed("spritePositions");
CCDictionary* posInfoDict = og->objectNamed("ninjaBirthPoint");
int x = posInfoDict->valueForKey("x")->intValue();
int y = posInfoDict->valueForKey("y")->intValue();




ninja = CCSprite::create("Player.png");
ninja->setPosition(ccp(x, y));
tileMap->addChild(ninja);


this->setMapPosForView(ccp(x, y));


this->setTouchEnabled(true); // 是layer具有响应触摸事件的能力
CCDirector::sharedDirector()->getTouchDispatcher()
->addTargetedDelegate(this, 0, true);


flagLayer = tileMap->layerNamed("flag_layer");
flagLayer->setVisible(false);




scoreBord = CCLabelTTF::create("socre:0", "Arial", 30);
scoreBord->setPosition(ccp(WIN_WIDTH - 60, 30));
this->addChild(scoreBord, 30);
this->_score = 0;


CocosDenshion::SimpleAudioEngine::sharedEngine()->playBackgroundMusic("TileMap.wav", true);


    return true;
}


bool HelloWorld::ccTouchBegan(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent){
return true;
}


void HelloWorld::ccTouchMoved(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent){
return;
}


CCPoint HelloWorld::cocoscoord2tilemapcoord(CCPoint pos)
{
CCPoint coord;
coord.x = (int)(pos.x / GET_TILE_WIDTH); 
coord.y = (int)((MAP_WIDTH - pos.y) / GET_TILE_HEIGHT);


return coord;
}


void HelloWorld::ccTouchEnded(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent){
CCPoint touchPos = pTouch->getLocation();
touchPos = tileMap->convertToNodeSpace(touchPos); // 从相对于屏幕的位置转换为相对于map的位置


CCPoint origPos = ninja->getPosition(); //ninja是map的孩子,所以相对于地图的位置
CCPoint diff = touchPos - origPos;
CCPoint ninjaDiff = ccp(0, 0);


if (abs(diff.x) > abs(diff.y)){
if (diff.x > 0){
ninjaDiff.x = tileMap->getTileSize().width;
}
else
{
ninjaDiff.x = -tileMap->getTileSize().width;;
}
}
else{
if (diff.y > 0){
ninjaDiff.y = tileMap->getTileSize().height;
}
else
{
ninjaDiff.y = -tileMap->getTileSize().height;
}
}


CCPoint newPos = origPos + ninjaDiff;


CCPoint tilePos = this->cocoscoord2tilemapcoord(newPos);

int oneTileId = flagLayer->tileGIDAt(tilePos);// 根据tilemap坐标得到层上物体的id
 
if (oneTileId == 0){// 不会碰撞
CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("move.wav");
ninja->setPosition(newPos);
this->setMapPosForView(newPos);
return;
}


CCDictionary* propertiesOnOneTile = tileMap->propertiesForGID(oneTileId);
const CCString* collide = propertiesOnOneTile->valueForKey("Collide");
const CCString* fruit = propertiesOnOneTile->valueForKey("fruit");


//判断如果新位置是碰撞属性true,不可以移动
if (collide && collide->compare("true") == 0) //碰撞
{
CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("hit.wav");
; // 不移动
}
else if (fruit && fruit->compare("true") == 0) //是一个西瓜
{
CCTMXLayer* fruitLayer = tileMap->layerNamed("foreground");
fruitLayer->removeTileAt(tilePos); // 吃掉西瓜 得分
flagLayer->removeTileAt(tilePos); // 移除障碍


CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("pickup.wav");


this->_score ++;
char str[40];
sprintf(str, "score:%d", _score);
scoreBord->setString(str);


}
else
{
CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("move.wav");
ninja->setPosition(newPos);
this->setMapPosForView(newPos);
}


return;
}


void HelloWorld::setMapPosForView(cocos2d::CCPoint playerPos){


CCPoint orig = playerPos;
CCPoint dest = ccp(WIN_WIDTH / 2, WIN_HEIGHT / 2);
CCPoint distance = ccpSub(dest, orig);
CCPoint newMapPos = ccp(0, 0) + distance;


newMapPos.x = (newMapPos.x > 0? 0:newMapPos.x);
newMapPos.y = (newMapPos.y > 0? 0:newMapPos.y);


newMapPos.x = (newMapPos.x < WIN_WIDTH - MAP_WIDTH? 
WIN_WIDTH-MAP_WIDTH:newMapPos.x);
newMapPos.y = (newMapPos.y < WIN_HEIGHT - MAP_HEIGHT? 
WIN_HEIGHT - MAP_HEIGHT:newMapPos.y);


tileMap->setPosition(newMapPos);


}




void HelloWorld::menuCloseCallback(CCObject* pSender)
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
CCMessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");
#else
    CCDirector::sharedDirector()->end();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
    exit(0);
#endif
#endif
}

你可能感兴趣的:(ccp之间是不可以直接进行+,-的,要用ccpSub和ccpAdd。)