#include "HelloWorld.h"

USING_NS_CC;

CCScene* MyHelloWorld::scene()
{
// 'scene' is an autorelease object
CCScene *scene = CCScene::create(); // 'layer' is an autorelease object
MyHelloWorld *layer = MyHelloWorld::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 MyHelloWorld::init()
{
//////////////////////////////
// 1. super init first
if ( !CCLayer::init() )
{
return false;
} initdefalut(); initmap(); return true;
} void MyHelloWorld::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
} void MyHelloWorld::initmap()
{
this->scheduleUpdate();
map=CCTMXTiledMap::create("my.tmx");
CCTMXObjectGroup* obj1= map->objectGroupNamed("object1");
wall=map->layerNamed("wall");
floor=map->layerNamed("floor");
meta=map->layerNamed("meta");
CCDictionary* dict=obj1->objectNamed("sprite");
float x=dict->valueForKey("x")->floatValue();
float y=dict->valueForKey("y")->floatValue();
float width=dict->valueForKey("width")->floatValue();
float height=dict->valueForKey("height")->floatValue();
int i=dict->valueForKey("key")->intValue();
sprite=CCSprite::create("Player.png");
sprite->retain();
sprite->setAnchorPoint(ccp(0,0));
sprite->setPosition(ccp(x,y));
this->addChild(map,10,1); map->addChild(sprite,0);
CCLOG("%d",map->getChildren()->count());
CCLOG("%d",floor->getZOrder());CCLOG("%d",wall->getZOrder());
CCLOG("%d",meta->getZOrder());
CCLOG("%d",sprite->getZOrder());
CCLOG("%f.%f,%d",x,y,i);
CCLOG("%f.%f,%d",width,height,i); } void MyHelloWorld::initdefalut()
{
CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin(); /////////////////////////////
// 2. add a menu item with "X" image, which is clicked to quit the program
// you may modify it. // add a "close" icon to exit the progress. it's an autorelease object
CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
"CloseNormal.png",
"CloseSelected.png",
this,
menu_selector(MyHelloWorld::menuCloseCallback)); pCloseItem->setPosition(ccp(origin.x + visibleSize.width - pCloseItem->getContentSize().width/2 ,
origin.y + pCloseItem->getContentSize().height/2)); // create menu, it's an autorelease object
CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);
pMenu->setPosition(CCPointZero);
this->addChild(pMenu, 20); /////////////////////////////
// 3. add your codes below... // add a label shows "Hello World"
// create and initialize a label CCLabelTTF* pLabel = CCLabelTTF::create("Hello World", "", 24); // position the label on the center of the screen
pLabel->setPosition(ccp(origin.x + visibleSize.width/2,
origin.y + visibleSize.height - pLabel->getContentSize().height)); // add the label as a child to this layer
this->addChild(pLabel, 1); // add "HelloWorld" splash screen"
CCSprite* pSprite = CCSprite::create("HelloWorld.png"); // position the sprite on the center of the screen
pSprite->setPosition(ccp(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y)); // add the sprite as a child to this layer
this->addChild(pSprite, 0);
} void MyHelloWorld::onEnter()
{
CCLayer::onEnter();
CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this,0,false);
} bool MyHelloWorld::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent)
{
CCPoint pointtemp= pTouch->getLocation(); //返回的是相当屏的本地坐标
CCPoint touchPoint= this->convertTouchToNodeSpace(pTouch); //返回的是相对layer
CCLOG("pointtemp:%f,%f",pointtemp.x,pointtemp.y);
CCLOG("touchPoint:%f,%f",touchPoint.x,touchPoint.y); //
////图块的惟一标识
//
//CCPoint mapTieldPoint=getTileCoordinate(point);
//CCLOG("mapTieldPoint:%f,%f",mapTieldPoint.x,mapTieldPoint.y);
//CCSize contentSize=map->getContentSize();
//CCLOG("contentSize:%f,%f",contentSize.width,contentSize.height);
//unsigned int tieldGid=floor->tileGIDAt(mapTieldPoint);
//if(tieldGid!=0){
// CCDictionary* dict=wall->propertiesForGID(tieldGid);
// if(dict!=NULL){
// /*int keyvalue=dict->valueForKey("key")->intValue();
// CCLOG("keyvalue%d",keyvalue);
// if(keyvalue==100){*/
// return true;
// //}
// }
//}
CCPoint playerPoint = sprite->getPosition();
CCLOG("playerPoint:%f,%f",playerPoint.x,playerPoint.y);
CCPoint diff = ccpSub(touchPoint, playerPoint);
if (abs(diff.x) > abs(diff.y))
{
if (diff.x > 0)
{
playerPoint.x += map->getTileSize().width;
}
else
{
playerPoint.x -= map->getTileSize().width;
}
}
else
{
if (diff.y > 0 )
{
playerPoint.y += map->getTileSize().height;
}
else
{
playerPoint.y -= map->getTileSize().height;
}
}
setPlayerPosition(playerPoint);
setViewpointCenter(playerPoint); return true;
} MyHelloWorld::~MyHelloWorld()
{
sprite->release();
} void MyHelloWorld::update(float)
{
/*CCLOG("update");
float x,y;
sprite->getPosition(&x,&y);
CCLOG("%f,%f",x,y);*/
} /************************************************************************/
/* 參数是精灵的位置 */
/************************************************************************/
void MyHelloWorld::setViewpointCenter(CCPoint& position) { CCSize winSize = CCDirector::sharedDirector()->getWinSize(); int x = MAX(position.x, winSize.width / 2);
int y = MAX(position.y, winSize.height / 2);
x = MIN(x, map->getMapSize().width * map->getTileSize().width - winSize.width / 2);
y = MIN(y, map->getMapSize().height *map->getTileSize().height- winSize.height/2);
CCPoint actualPosition = ccp(x, y); CCPoint centerOfView = ccp(winSize.width/2, winSize.height/2);
CCPoint viewPoint = ccpSub(centerOfView, actualPosition);
CCLOG("viewPoint:%f,%f",viewPoint.x,viewPoint.y);
this->setPosition(viewPoint); /*CCSize winSize = CCDirector::sharedDirector()->getWinSize();
int x = MAX(position.x, winSize.width / 2);
int y = MAX(position.y, winSize.height / 2); x = MIN(x, map->getTileSize().width * map->getMapSize().height - winSize.width / 2);
y = MIN(y, map->getTileSize().height * map->getMapSize().height - winSize.height / 2);
CCPoint acutalPoint = ccp(x, y);
CCPoint centerOfView = ccp(winSize.width / 2, winSize.height / 2);
CCPoint ViewOfPoint = ccpSub(centerOfView, acutalPoint);
this->setPosition(ViewOfPoint);*/ } CCPoint MyHelloWorld::getTileCoordinate(CCPoint& point){
float width=map->getTileSize().width;
float height=map->getTileSize().height;
CCLOG("getContentSize:%f",map->getContentSize().height);
CCPoint re=ccp((int)(point.x/width),(int)((map->getContentSize().height-point.y)/height));
return re;
} /************************************************************************/
/* position 目标点 相对于layer的 */
/************************************************************************/
void MyHelloWorld::setPlayerPosition(cocos2d::CCPoint position)
{ CCPoint tileCoord2 = this->tileCoordForGid(position);
CCPoint tileCoord = this->getTileCoordinate(position);
CCLOG("tileCoord:%f,%f",tileCoord.x,tileCoord.y);
CCLOG("tileCoord2:%f,%f",tileCoord2.x,tileCoord2.y);
int theGid = meta->tileGIDAt(tileCoord); if (theGid)
{
CCLOG("gid:%d",theGid);
CCDictionary* properties = map->propertiesForGID(theGid); if (properties)
{
CCSprite* tempSprite=meta->tileAt(tileCoord);
CCLOG("%d,",tempSprite->getZOrder());
CCLOG("%d,",sprite->getZOrder());
//sprite->setZOrder(0);
//const CCString* str = properties->valueForKey("key");
//CCLOG("STR:%d",str->intValue());
//if (str && str->compare("200") == 0)
////if (str !=NULL)
//{
// return;
//} /*const CCString* tempstr = properties->valueForKey("Eat");
if (tempstr && tempstr->compare("true") == 0)
{
_numCollected++;
_hud->numCollectedChanged(_numCollected);
_meta ->removeTileAt(tileCoord);
_foodLayer->removeTileAt(tileCoord); }*/ } }
//else{sprite->setZOrder(11);}
sprite->setPosition(position);
} /************************************************************************/
/返回的要转成int */
/************************************************************************/
CCPoint MyHelloWorld::tileCoordForGid(CCPoint position)
{
int x = position.x / map->getTileSize().width;
CCLOG("tiled:%f",map->getMapSize().height * map->getTileSize().height);
int y = ((map->getMapSize().height * map->getTileSize().height) - position.y) / map->getTileSize().height ;
return ccp(x, y); }

cocos2dx游戏 地图的更多相关文章

  1. [转]eoe社区cocos2d-x游戏引擎知识大汇总

    [eoeAndroid 社区]特意为大家汇总了cocos2d-x知识贴,分量十足,纯正干或.从基础教程到游戏应用的开发,我们不让知识流失,我们要做知识的搬运工还有加工 师.希望大家能够一起的学习,和大 ...

  2. Cocos2dx游戏开发系列笔记13:一个横版拳击游戏Demo完结篇

    懒骨头(http://blog.csdn.net/iamlazybone QQ:124774397 ) 写下这些东西的同时 旁边放了两部电影 周星驰的<还魂夜> 甄子丹的<特殊身份& ...

  3. 【Cocos2d-x游戏引擎开发笔记(25)】XML解析

    原创文章,转载请注明出处:http://blog.csdn.net/zhy_cheng/article/details/9128819 XML是一种非常重要的文件格式,由于C++对XML的支持非常完善 ...

  4. 15款Cocos2d-x游戏源码

    (1)用cocos2d-x开发的中国象棋游戏源码 使用Cocos2d-X2.2.3开发的一款中国象棋游戏,游戏中可以实现.新局面.悔棋.游戏音乐.胜利后会显示游戏结果. 源码下载:http://www ...

  5. 【COCOS2DX-LUA 脚本开发之一】在Cocos2dX游戏中使用Lua脚本进行游戏开发(基础篇)并介绍脚本在游戏中详细用途!

    [COCOS2DX-LUA 脚本开发之一]在Cocos2dX游戏中使用Lua脚本进行游戏开发(基础篇)并介绍脚本在游戏中详细用途! 分类: [Cocos2dx Lua 脚本开发 ] 2012-04-1 ...

  6. 转载:Cocos2D-x 游戏接入 Windows 设备所需做的六件事

    原文地址:http://msopentech.com/zh-hans/blog/2014/05/09/cocos2d-x-%E6%B8%B8%E6%88%8F%E6%8E%A5%E5%85%A5-wi ...

  7. cocos2d-x游戏是怎么跑起来的

    虽然cocos2d-x v3.0 alpha版已经出来了,也改进了不少,有兴趣的可以去尝尝鲜.因为后面可能还会配合cocoStudio写一下博客,而现在v1.0.0.0版本需要配合cocos2d-x ...

  8. cocos2d-x游戏开发实战原创视频讲座系列1之2048游戏开发

     cocos2d-x游戏开发实战原创视频讲座系列1之2048游戏开发 的产生 视持续更新中.... 视频存放地址例如以下:http://ipd.pps.tv/user/1058663622     ...

  9. Cocos2d-x游戏中默认的AndroidManifest.xml的解析

    直接上代码说明: <?xml version="1.0" encoding="utf-8"? > <!-- xmlns:android=&qu ...

随机推荐

  1. kotlin扩展函数

    kotlin 扩展函数 https://www.kotlincn.net/docs/reference/extensions.html 扩展函数在android中的应用 fun AppCompatAc ...

  2. APIO2018练习赛伪题解

    传送门:https://pcms.university.innopolis.ru/statements/org/apio/2018/practice/statements.pdf 主要就在于后面三道构 ...

  3. [BZOJ 1806] Miners 矿工配餐

    Link: BZOJ 1806 传送门 Solution: 为了使状态包含每个节点前所有必须的信息: 设$dp[i][a1][a2][b1][b2]$为配送到第$i$个,一厂前两个为$a1,a2$,二 ...

  4. 【bzoj4720】【noip2016】【换座位】期望dp+Floyd

    [pixiv] https://www.pixiv.net/member_illust.php?mode=medium&illust_id=62370736 wa...已经快一年了,重新来做这 ...

  5. [JZOJ3106]锻炼

    题目大意: 给你一个$n\times m(n,m\leq 50)$的网格图,其中有一个四连通的障碍物.给定起点和终点,每次你可以走到和当前位置八连通的一个方格内,问绕障碍物一圈最短要走几格? 思路: ...

  6. 安裝jpeg-6b png error错误解决方法

    安裝jpeg-6b png error错误解决方法 默认安裝jpeg-6b shell> wget ftp://ftp.uu.net/graphics/jpeg/jpegsrc.v6b.tar. ...

  7. IOS调用WCF提供的服务方法,但是方法的参数是WCF那边自定义的对象,这样有办法调用么,如果可以IOS应该怎么传参呢?请问有了解的么,

    最近做一个项目后端使用WCF接收Android手机拍照并带其它参数保存到服务器里:刚好把最近学习的WCF利用上,本以为是个比较简单的功能应该很好实现,没想到其中碰到不少问题,在网上搜索很久一直没有想到 ...

  8. Android 高版本API方法在低版本系统上的兼容性处理

    Android 版本更替,新的版本带来新的特性,新的方法. 新的方法带来许多便利,但无法在低版本系统上运行,如果兼容性处理不恰当,APP在低版本系统上,运行时将会crash. 本文以一个具体的例子说明 ...

  9. 在Android中解决内存溢出 – OutOfMemoryError

    原文链接:http://riggaroo.co.za/fixing-memory-leaks-in-android-outofmemoryerror/ 注:本文在原文基础上在如何判断内存是否泄露方面进 ...

  10. squid 访问日志记录

    squid日志记录在squid.conf 也可记录.并且squid日志可以不记录静态项 在squid.conf 加入 (1)access_log /var/log/squid/access.log  ...