把Tiled中做出的地图弄到项目中~~就是懒,为了以后直接复制写过来
来源:互联网 发布:move.js 不停的旋转 编辑:程序博客网 时间:2024/05/19 22:01
cocos2d::CCSprite* ninja;
cocos2d::CCTMXTiledMap* tileMap;
然后.cpp中加入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上面
2.设置人物的初始位置
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);
3.将主角移动到地图的中间
this->setMapPosForView(ccp(x, y));
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);
5.用:ccTouchEnded(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)来使物体移动
void HelloWorld::ccTouchEnded(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent){
CCPoint touchPos=pTouch->getLocation();
touchPos=tileMap->convertToNodeSpace(touchPos);
CCPoint origPos=ninjia->getPosition();
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;
ninja->setPosition(newPos);
tileMap->setMapForView(newPos);
- 把Tiled中做出的地图弄到项目中~~就是懒,为了以后直接复制写过来
- cocos2dx中tiled地图的使用
- PowerDesigner 把Comment写到name中 和把name写到Comment中 pd7以后版本可用
- PowerDesigner 把Comment写到name中 和把name写到Comment中 pd7以后版本可用
- PowerDesigner 把Comment写到name中 和把name写到Comment中 pd7以后版本可用
- PowerDesigner 把Comment写到name中 和把name写到Comment中 pd7以后版本可用
- PowerDesigner 把Comment写到name中 和把name写到Comment中 pd7以后版本可用
- PowerDesigner 把Comment写到name中 和把name写到Comment中 pd7以后版本可用
- PowerDesigner 把Comment写到name中 和把name写到Comment中 pd7以后版本可用
- PowerDesigner 把Comment写到name中 和 把name写到Comment中 pd7以后版本可用
- PowerDesigner 把Comment写到name中 和把name写到Comment中 pd7以后版本可用
- PowerDesigner 把Comment写到name中 和把name写到Comment中 pd7以后版本可用
- PowerDesigner 把Comment写到name中 和把name写到Comment中 pd7以后版本可用
- PowerDesigner 把Comment写到name中 和把name写到Comment中 pd7以后版本可用
- myeclipse中直接复制项目不能直接运行的解决方案
- 鼠标点击获取百度地图的经度与纬度(可直接嵌入到项目中)
- 把D:\\12.avi 复制到当前项目目录下的copy.avi中(文件流)
- PowerDesigner 把name写到Comment中 pd7以后版本可用
- 你是欢喜还是忧?锤子手机发布会观后感
- 泛泰A900 刷4.4专用中文TWRP2.7.1.1版 支持自动识别手机版本(全球首创)
- phpcms v9代码剖析一
- 堆 和 栈的 区别(经典)
- 收录:8款最佳的开源在线学习CMS系统
- 把Tiled中做出的地图弄到项目中~~就是懒,为了以后直接复制写过来
- 能上Q不能打开网页解决方法
- Leetcode 习题: Merge k Sorted Lists
- 推荐:一个HMM库
- 消除window系统电脑桌面快捷方式
- kindeditor上传文件时报找不到action的result问题
- 第八章实验作业
- 最大公约数与最小公倍数
- Linux内核regulator架构和编写