cocos2d小游戏---忍者吃西瓜

cocos2d小游戏---忍者吃西瓜,第1张

概述声明:本代码是跟着别人的教程走的,贴出来以便自己复习用,其他没有什么大用。 #ifndef __HELLOWORLD_SCENE_H__#define __HELLOWORLD_SCENE_H__#include "cocos2d.h"class HelloWorld : public cocos2d::CCLayer{public: ~ HelloWorld(); //

声明:本代码是跟着别人的教程走的,贴出来以便自己复习用,其他没有什么大用。



#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 cc@R_404_4608@ed(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 instancebool 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::cc@R_404_4608@ed(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;}
总结

以上是内存溢出为你收集整理的cocos2d小游戏---忍者吃西瓜全部内容,希望文章能够帮你解决cocos2d小游戏---忍者吃西瓜所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1066779.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存