cocos2d-x的A*寻路
如果你还不熟悉A*寻路,请先看下这篇文章http://blog.csdn.net/dssdss123/article/details/11494065
一、先介绍几个函数和结构:
1、virtual void draw()
这个函数跟与MFC上单文档里的OnDraw函数很像,这里只是少了dc,这个函数会一直被调用,无需刷新,也就是说,你无需像在MFC上一样调用Invalidate或者InvalidateRect
2、virtual void ccTouchBegan(CCTouch* pTouch, CCEvent* pEvent)
这个函数是下响应触摸的,当你点击屏幕时,就会进到这个函数。要使这个函数有效,你需要在init中调用
setTouchEnabled(true); // 允许该层响应触摸
CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, 0, false); // 注册单点触摸
在这个例子中,我们不需要多点触摸
3、ccColor4F结构
这个结构在ccDrawSolidRect函数中将会使用到,ccDrawSolidRect是画某种颜色的矩形,对应在MFC中,我们使用的是FillSolidRect。ccColor4F是RGBA的结构,RGB是三颜色,红绿蓝,最后一个alpha值,他表示这个颜色的透明度。为1是完全不透明,0时则完全透明。
二、实现
1、去掉coco自带的乱七八糟的显示
1)去掉帧频显示
在函数bool AppDelegate::applicationDidFinishLaunching()中
// turn on display FPS
//pDirector->setDisplayStats(true);
将pDirector->setDisplayStats(true),注释掉
2)去掉menu,Hello World文字标签
在函数void HelloWorld::init()中
// Add the menu to HelloWorld layer as a child layer.
//this->addChild(pMenu, 1);
// Add the label to HelloWorld layer as a child layer.
//this->addChild(pLabel, 1);
将menu和label注释掉
3)替换背景图,并置于最底层
// 3. Add add a splash screen, show the cocos2d splash image.
CCSprite* pSprite = CCSprite::create("map.jpg"); // 将原先的HelloWorld.png,替换为自己的图片,这里我换成map.jpg
CC_BREAK_IF(! pSprite);
2、初始化地图
声明结构表示格子掩码等一些信息,我们的例子中,只需掩码,所以结构如下: