声明:本代码是跟着别人的教程走的,贴出来以便自己复习用,其他没有什么大用。
#ifndef __HELLOWORLD_SCENE_H__ #define __HELLOWORLD_SCENE_H__ #include "cocos2d.h" class HelloWorld : public cocos2d::CCLayer { public: ~ HelloWorld(); // 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); //地图变量 cocos2d::CCTMXTiledMap *tileMap; //忍者变量 cocos2d::CCSprite *nijia; //障碍物层 cocos2d::CCTMXLayer *flagLayer; //前景层 cocos2d::CCTMXLayer *forgroundLayer; //积分标签 cocos2d::CCLabelTTF *scoreBord; int _score; //模拟小人移动 void MapMoveForward(cocos2d::CCPoint ForwardPos); // 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); //移动视图 void MoveView(cocos2d::CCPoint touchPos); //坐标转换 cocos2d::CCPoint cocosCoord2TiledMapCoord(cocos2d::CCPoint cocosPoint); //是不是障碍物 bool IsObstacles(cocos2d::CCPoint curPoint); //是不是西瓜 bool IsXiGua(cocos2d::CCPoint curPoint); }; #endif // __HELLOWORLD_SCENE_H__
#include "HelloWorldScene.h" #include "SimpleAudioEngine.h" USING_NS_CC; #define WIN_WIDTH (CCDirector::sharedDirector()->getVisibleSize().width) #define WIN_HEIGHT (CCDirector::sharedDirector()->getVisibleSize().height) #define MAP_WIDTH (tileMap->getMapSize().width * tileMap->getTileSize().width) #define MAP_HEIGHT (tileMap->getMapSize().height * tileMap->getTileSize().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; } 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 } // 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("BackMap"); CCAssert(backLayer,"can not find layer BackMap"); //加载地图 this->addChild(tileMap); //前景层 forgroundLayer = tileMap->layerNamed("forground"); //获取障碍物层 flagLayer = tileMap->layerNamed("flag_layer"); flagLayer->setVisible(false); //找到忍者位置 CCTMXObjectGroup *og = tileMap->objectGroupNamed("player"); CCDictionary * posInfoDict = og->objectNamed("nijiaBirthPoint"); int x = posInfoDict->valueForKey("x")->intValue(); int y = posInfoDict->valueForKey("y")->intValue(); //创建忍者 nijia = CCSprite::create("Player.png"); nijia->setAnchorPoint(ccp(0.5,0.5)); nijia->setPosition(ccp(x,y)); tileMap->addChild(nijia); //触摸事件触发器 CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this,true); //创建积分标签 scoreBord = CCLabelTTF::create("score:0","Arial",30); scoreBord->setPosition(ccp(WIN_WIDTH-60,30)); this->addChild(scoreBord); _score = 0; //播放背景音乐 CocosDenshion::SimpleAudioEngine::sharedEngine()->playBackgroundMusic("TileMap.wav",true); return true; } //析构函数 HelloWorld::~HelloWorld() { } //模拟小人移动 void HelloWorld::MapMoveForward(cocos2d::CCPoint ForwardPos) { //移动地图 CCPoint srcPos = tileMap->getPosition(); CCPoint newMapPos = srcPos + ForwardPos; //防止坐标超出边界 newMapPos.x = newMapPos.x>0 ? 0 : newMapPos.x; newMapPos.x = newMapPos.x<WIN_WIDTH - MAP_WIDTH ? WIN_WIDTH - MAP_WIDTH : newMapPos.x; newMapPos.y = newMapPos.y>0 ? 0 : newMapPos.y; newMapPos.y = newMapPos.y<WIN_HEIGHT - MAP_HEIGHT ? WIN_HEIGHT - MAP_HEIGHT : newMapPos.y; tileMap->setPosition(newMapPos); } //触摸开始 bool HelloWorld::ccTouchBegan(cocos2d::CCTouch *pTouch,cocos2d::CCEvent *pEvent) { return true; } //触摸中 void HelloWorld::ccTouchMoved(cocos2d::CCTouch *pTouch,cocos2d::CCEvent *pEvent) { } //触摸结束 void HelloWorld::ccTouchEnded(cocos2d::CCTouch *pTouch,cocos2d::CCEvent *pEvent) { //触摸坐标 CCPoint touchPos = pTouch->getLocation(); //移动视图 MoveView(touchPos); } //移动视图 void HelloWorld::MoveView(cocos2d::CCPoint touchPos) { //检验忍者坐标 CCPoint ninAbsPoint = nijia->convertToWorldSpace(ccp(0,0)); //判断忍者移动方向 if (abs(touchPos.x - ninAbsPoint.x) > abs(touchPos.y - ninAbsPoint.y))//横向移动 { if (touchPos.x - ninAbsPoint.x > 0) { //障碍物检测 if (!IsObstacles(ccp(nijia->getPositionX() + tileMap->getTileSize().width,nijia->getPositionY()))) { //检测西瓜 if (IsXiGua(ccp(nijia->getPositionX() + tileMap->getTileSize().width,nijia->getPositionY()))) { //清除图层 forgroundLayer->removeTileAt(cocosCoord2TiledMapCoord(ccp(nijia->getPositionX() + tileMap->getTileSize().width,nijia->getPositionY()))); flagLayer->removeTileAt(cocosCoord2TiledMapCoord(ccp(nijia->getPositionX() + tileMap->getTileSize().width,nijia->getPositionY()))); //分值变化 _score += 1; char scoreStr[20]; sprintf(scoreStr,"score : %d",_score); scoreBord->setString(scoreStr); //播放吃西瓜音乐 CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("pickup.wav"); } //忍者向右移动 nijia->setPosition(ccp(nijia->getPositionX() + tileMap->getTileSize().width,nijia->getPositionY())); if (ninAbsPoint.x >= WIN_WIDTH/2)//地图向左移动 { MapMoveForward(ccp(- tileMap->getTileSize().width,0)); } //播放移动音乐 CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("move.wav"); } } else if(touchPos.x - ninAbsPoint.x < 0) { //障碍物检测 if (!IsObstacles(ccp(nijia->getPositionX() - tileMap->getTileSize().width,nijia->getPositionY()))) { //检测西瓜 if (IsXiGua(ccp(nijia->getPositionX() - tileMap->getTileSize().width,nijia->getPositionY()))) { //清除图层 forgroundLayer->removeTileAt(cocosCoord2TiledMapCoord(ccp(nijia->getPositionX() - tileMap->getTileSize().width,nijia->getPositionY()))); flagLayer->removeTileAt(cocosCoord2TiledMapCoord(ccp(nijia->getPositionX() - tileMap->getTileSize().width,nijia->getPositionY()))); //分值变化 _score += 1; char scoreStr[20]; sprintf(scoreStr,_score); scoreBord->setString(scoreStr); //播放吃西瓜音乐 CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("pickup.wav"); } //忍者移动 nijia->setPosition(ccp(nijia->getPositionX() - tileMap->getTileSize().width,nijia->getPositionY())); if (ninAbsPoint.x <= WIN_WIDTH/2 )//地图向右移动 { MapMoveForward(ccp(tileMap->getTileSize().width,0)); } //播放移动音乐 CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("move.wav"); } } } else//纵向移动 { if (touchPos.y - ninAbsPoint.y > 0) { //障碍物检测 if (!IsObstacles(ccp(nijia->getPositionX(),nijia->getPositionY() + tileMap->getTileSize().width))) { //检测西瓜 if (IsXiGua(ccp(nijia->getPositionX(),nijia->getPositionY() + tileMap->getTileSize().width))) { //清除图层 forgroundLayer->removeTileAt(cocosCoord2TiledMapCoord(ccp(nijia->getPositionX(),nijia->getPositionY() + tileMap->getTileSize().width))); flagLayer->removeTileAt(cocosCoord2TiledMapCoord(ccp(nijia->getPositionX(),nijia->getPositionY() + tileMap->getTileSize().width))); //分值变化 _score += 1; char scoreStr[20]; sprintf(scoreStr,_score); scoreBord->setString(scoreStr); //播放吃西瓜音乐 CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("pickup.wav"); } //忍者移动 nijia->setPosition(ccp(nijia->getPositionX(),nijia->getPositionY() + tileMap->getTileSize().width)); if (ninAbsPoint.y >= WIN_HEIGHT/2)//地图向下移动 { MapMoveForward(ccp(0,-tileMap->getTileSize().height)); } } } else if(touchPos.y - ninAbsPoint.y < 0) { //障碍物检测 if (!IsObstacles(ccp(nijia->getPositionX(),nijia->getPositionY() - tileMap->getTileSize().width))) { //检测西瓜 if (IsXiGua(ccp(nijia->getPositionX(),nijia->getPositionY() - tileMap->getTileSize().width))) { //清除图层 forgroundLayer->removeTileAt(cocosCoord2TiledMapCoord(ccp(nijia->getPositionX(),nijia->getPositionY() - tileMap->getTileSize().width))); flagLayer->removeTileAt(cocosCoord2TiledMapCoord(ccp(nijia->getPositionX(),nijia->getPositionY() - tileMap->getTileSize().width))); //分值变化 _score += 1; char scoreStr[20]; sprintf(scoreStr,nijia->getPositionY() - tileMap->getTileSize().width)); if (ninAbsPoint.y <= WIN_HEIGHT/2)//地图向上移动 { MapMoveForward(ccp(0,tileMap->getTileSize().height)); } //播放移动音乐 CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("move.wav"); } } } } //坐标转换 cocos2d::CCPoint HelloWorld::cocosCoord2TiledMapCoord(cocos2d::CCPoint cocosPoint) { CCPoint tiledMapPoint; tiledMapPoint.x = (int)(cocosPoint.x / tileMap->getTileSize().width); tiledMapPoint.y = (int)((MAP_HEIGHT - cocosPoint.y) / tileMap->getTileSize().height); return tiledMapPoint; } //是不是障碍物 bool HelloWorld::IsObstacles(cocos2d::CCPoint curPoint) { bool isObstacles = false; //坐标转换 CCPoint tileMapPoint = cocosCoord2TiledMapCoord(curPoint); //瓦片 I D int tiledID = flagLayer->tileGIDAt(tileMapPoint); if (tiledID==0) return false; //获取瓦片所有属性 CCDictionary *tileProperty = tileMap->propertiesForGID(tiledID); const CCString *collideStr = tileProperty->valueForKey("collide"); //检测属性是否为true if (collideStr->compare("true") == 0) { isObstacles = true; } return isObstacles; } //是不是西瓜 bool HelloWorld::IsXiGua(cocos2d::CCPoint curPoint) { bool isXiGua = false; //坐标转换 CCPoint tileMapPoint = cocosCoord2TiledMapCoord(curPoint); //瓦片 I D int tiledID = flagLayer->tileGIDAt(tileMapPoint); if (tiledID==0) return false; //获取瓦片所有属性 CCDictionary *tileProperty = tileMap->propertiesForGID(tiledID); const CCString *fruitStr = tileProperty->valueForKey("fruit"); //检测属性是否为true if (fruitStr->compare("xigua") == 0) { isXiGua = true; } return isXiGua; }