//移动方向枚举类
var MoveDirection = cc.Enum({
NONE: 0,
UP: 1,
DOWN: 2,
LEFT: 3,
RIGHT: 4
}); var minTilesCount = 2;
var mapMoveStep = 1;
var minMoveValue = 50; cc.Class({
extends: cc.Component,
editor: {
requireComponent: cc.TiledMap
}, properties: {
_touchStartPos: {
default: null,
serializable: false,
},
_touching: {
default: false,
serializable: false,
}, _isMapLoaded : {
default: false,
serializable: false,
}, floorLayerName: {
default: 'floor'
}, barrierLayerName: {
default: 'barrier'
}, objectGroupName: {
default: 'players'
}, startObjectName: {
default:'SpawnPoint'
}, successObjectName: {
default:'SuccessPoint'
}
}, onLoad: function () {
console.log(" init") //获取英雄
this._player = this.node.getChildByName('player');
if (! this._isMapLoaded) {
this._player.active = false;
} //注册按键点击
var self = this;
cc.eventManager.addListener({
event: cc.EventListener.KEYBOARD,
onKeyPressed: function(keyCode, event) {
self._onKeyPressed(keyCode, event);
}
}, self); //注册触摸事件
this.node.on(cc.Node.EventType.TOUCH_START, function (event) {
self._touching = true;
self._touchStartPos = event.touch.getLocation(); //this._onTouchStart()
}, self); this.node.on(cc.Node.EventType.TOUCH_END, function (event) {
if (!self._touching) return; self._touching = false;
var touchPos = event.touch.getLocation();
var movedX = touchPos.x - self._touchStartPos.x;
var movedY = touchPos.y - self._touchStartPos.y;
var movedXValue = Math.abs(movedX);
var movedYValue = Math.abs(movedY); //过小
if (movedXValue < minMoveValue && movedYValue < minMoveValue) {
// touch moved not enough
return;
} //新建一个点
var newTile = cc.p(this._curTile.x, this._curTile.y);
var mapMoveDir = MoveDirection.NONE;
//x方向移动
if (movedXValue >= movedYValue)
{
// move to right or left
if (movedX > 0) {
newTile.x += 1;
mapMoveDir = MoveDirection.LEFT;
} else {
newTile.x -= 1;
mapMoveDir = MoveDirection.RIGHT;
}
}
//y方向移动
else
{
// move to up or down
if (movedY > 0) {
newTile.y -= 1;
mapMoveDir = MoveDirection.UP;
}
else
{
newTile.y += 1;
mapMoveDir = MoveDirection.DOWN;
}
}
this._tryMoveToNewTile(newTile, mapMoveDir);
}, self);
}, //我推测这个是回调函数
theMapLoaded: function(err) {
//有错误退出
if (err) return; console.log("map init") //初始化地图位置
this._initMapPos(); // 获取成功层
this._succeedLayer = this.node.getParent().getChildByName('succeedLayer');
this._succeedLayer.active = false; //初始化英雄 位置
this._tiledMap = this.node.getComponent('cc.TiledMap');
var objectGroup = this._tiledMap.getObjectGroup(this.objectGroupName);
if (!objectGroup) return; var startObj = objectGroup.getObject(this.startObjectName);
var endObj = objectGroup.getObject(this.successObjectName);
if (!startObj || !endObj) return; var startPos = cc.p(startObj.x, startObj.y);
var endPos = cc.p(endObj.x, endObj.y); this._layerFloor = this._tiledMap.getLayer(this.floorLayerName);
this._layerBarrier = this._tiledMap.getLayer(this.barrierLayerName);
if (!this._layerFloor || !this._layerBarrier) return; this._curTile = this._startTile = this._getTilePos(startPos);
this._endTile = this._getTilePos(endPos); if (this._player) {
this._updatePlayerPos();
this._player.active = true;
} this._isMapLoaded = true;
}, //讲地图设置为地步坐下端
_initMapPos: function() {
this.node.setPosition(cc.visibleRect.bottomLeft);
}, _getTilePos: function(posInPixel) {
var mapSize = this.node.getContentSize();
var tileSize = this._tiledMap.getTileSize();
var x = Math.floor(posInPixel.x / tileSize.width);
var y = Math.floor((mapSize.height - posInPixel.y) / tileSize.height); return cc.p(x, y);
}, _onKeyPressed: function(keyCode, event) {
if (!this._isMapLoaded || this._succeedLayer.active) return; var newTile = cc.p(this._curTile.x, this._curTile.y);
var mapMoveDir = MoveDirection.NONE;
switch(keyCode) {
case cc.KEY.up:
newTile.y -= 1;
mapMoveDir = MoveDirection.DOWN;
break;
case cc.KEY.down:
newTile.y += 1;
mapMoveDir = MoveDirection.UP;
break;
case cc.KEY.left:
newTile.x -= 1;
mapMoveDir = MoveDirection.RIGHT;
break;
case cc.KEY.right:
newTile.x += 1;
mapMoveDir = MoveDirection.LEFT;
break;
default:
return;
} this._tryMoveToNewTile(newTile, mapMoveDir);
}, _tryMoveToNewTile: function(newTile, mapMoveDir) {
var mapSize = this._tiledMap.getMapSize(); //超出边界
if (newTile.x < 0 || newTile.x >= mapSize.width) return;
if (newTile.y < 0 || newTile.y >= mapSize.height) return; //障碍
if (this._layerBarrier.getTileGIDAt(newTile)) {
cc.log('This way is blocked!');
return false;
} // update the player position
this._curTile = newTile;
this._updatePlayerPos(); // 必要的时候移动地图
this._tryMoveMap(mapMoveDir); // 检测是否成功
if (cc.pointEqualToPoint(this._curTile, this._endTile)) {
cc.log('succeed');
this._succeedLayer.active = true;
}
}, _updatePlayerPos: function() {
var pos = this._layerFloor.getPositionAt(this._curTile);
this._player.setPosition(pos);
}, _tryMoveMap: function(moveDir) {
// get necessary data
var mapContentSize = this.node.getContentSize();
var mapPos = this.node.getPosition();
var playerPos = this._player.getPosition();
var viewSize = cc.size(cc.visibleRect.width, cc.visibleRect.height);
var tileSize = this._tiledMap.getTileSize();
var minDisX = minTilesCount * tileSize.width;
var minDisY = minTilesCount * tileSize.height; var disX = playerPos.x + mapPos.x;
var disY = playerPos.y + mapPos.y;
var newPos;
switch (moveDir) {
case MoveDirection.UP:
if (disY < minDisY) {
newPos = cc.p(mapPos.x, mapPos.y + tileSize.height * mapMoveStep);
}
break;
case MoveDirection.DOWN:
if (viewSize.height - disY - tileSize.height < minDisY) {
newPos = cc.p(mapPos.x, mapPos.y - tileSize.height * mapMoveStep);
}
break;
case MoveDirection.LEFT:
if (viewSize.width - disX - tileSize.width < minDisX) {
newPos = cc.p(mapPos.x - tileSize.width * mapMoveStep, mapPos.y);
}
break;
case MoveDirection.RIGHT:
if (disX < minDisX) {
newPos = cc.p(mapPos.x + tileSize.width * mapMoveStep, mapPos.y);
}
break;
default:
return;
} if (newPos) {
// calculate the position range of map
var minX = viewSize.width - mapContentSize.width - cc.visibleRect.left;
var maxX = cc.visibleRect.left.x;
var minY = viewSize.height - mapContentSize.height - cc.visibleRect.bottom;
var maxY = cc.visibleRect.bottom.y; if (newPos.x < minX) newPos.x = minX;
if (newPos.x > maxX) newPos.x = maxX;
if (newPos.y < minY) newPos.y = minY;
if (newPos.y > maxY) newPos.y = maxY; if (!cc.pointEqualToPoint(newPos, mapPos)) {
cc.log('Move the map to new position: ', newPos);
this.node.setPosition(newPos);
}
}
}, restartGame: function() {
this._succeedLayer.active = false;
this._initMapPos();
this._curTile = this._startTile;
this._updatePlayerPos();
},
});

ccc tiledmap的更多相关文章

  1. ccc tiledmap 获取元素属性

    cc.Class({ extends: cc.Component, properties: { elementLable: { default: null, type : cc.Label }, ma ...

  2. C段旁注工具CCC.exe

    C段旁注工具CCC.exe可以进行C段的web站点批量查询 自动排除DNS错误的域名以及IP和当前服务器不符的域名 抓取bing上的所有URL,不光是域名信息,方便直接进入 自动生成html报告,方便 ...

  3. code vs1262 不要把球传我(组合数学) 2012年CCC加拿大高中生信息学奥赛

    1262 不要把球传我 2012年CCC加拿大高中生信息学奥赛  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 白银 Silver 题解  查看运行结果     题目描述 De ...

  4. cocos2dx使用TiledMap创建斜45度地图场景

    做游戏,场景是一个很重要的部分,如果缺少这一步,很难做出好的游戏,对于cocos2dx来说,有很多2D的地图编辑器可以用,效果都还可以,其中Tiled是支持的比较好的,它支持Tiled编辑出来的几种模 ...

  5. [CCC 1996 01]Deficient, Perfect, and Abundant

    CCC加拿大高中生信息学奥赛 其余来源 CODEVS[3312]——CCC 1996 01 Deficient, Perfect, and Abundant ——http://codevs.cn/pr ...

  6. cocos2dx A* + tiledMap

    本文转自:http://blog.csdn.net/w18767104183/article/category/1757765 前面一章讲了cocos2dx 中使用A星算法 这章中讲 A*结合tile ...

  7. awk匹配以aaa开头,以bbb结尾的内容,同时aaa和bbb之间还包含ccc

    如果是匹配以A开头,以B结尾的内容,同时A和B之间还包含C的这种怎么做?比如 [root@localhost ~]#cat file aaa grge ddd bbb aaa gege ccc bbb ...

  8. LOJ #2802. 「CCC 2018」平衡树(整除分块 + dp)

    题面 LOJ #2802. 「CCC 2018」平衡树 题面有点难看...请认真阅读理解题意. 转化后就是,给你一个数 \(N\) ,每次选择一个 \(k \in [2, N]\) 将 \(N\) 变 ...

  9. Pentaho BI server 中 CCC table Component 的使用小技巧

    我使用的版本 Pentaho BI Server 5.3.0.0.213 CDE/CDF/CDA/CCC 15.04.16 stable   Q: 如何设置表格中各种提示文字的语言(默认为英语)? C ...

随机推荐

  1. 无废话Android之android下junit测试框架配置、保存文件到手机内存、android下文件访问的权限、保存文件到SD卡、获取SD卡大小、使用SharedPreferences进行数据存储、使用Pull解析器操作XML文件、android下操作sqlite数据库和事务(2)

    1.android下junit测试框架配置 单元测试需要在手机中进行安装测试 (1).在清单文件中manifest节点下配置如下节点 <instrumentation android:name= ...

  2. MVC授权认证

    处于安全性考虑,MVC可以完成授权认证,授权认证的方式如下: 1.配置Config文件,设置登录页面: <authentication mode="Forms"> &l ...

  3. Python多版本共存之pyenv

    经常遇到这样的情况: 系统自带的Python是2.6,自己需要Python 2.7中的某些特性: 系统自带的Python是2.x,自己需要Python 3.x: 此时需要在系统中安装多个Python, ...

  4. JqueryDemoTools-用于整理jQueryDemo 分类: C# 公共资源 2014-12-02 16:50 224人阅读 评论(1) 收藏

    应用背景: 在学习js时,为了熟悉某个功能,或使用某个插件,往往需要写一个Demo来测试:一些好的Demo也可以整理积累下来,方便以后查阅: 写了一个编写jQuery Demo的辅助工具.界面很简单, ...

  5. WebRTC代码走读(十):rtp_rtcp模块分析,webrtcrtp_rtcp

    转自:http://www.bkjia.com/Androidjc/1020017.html 1. 对外提供的主要流程接口       收包的调用接口RtpReceiverImpl::Incoming ...

  6. servlet、genericservlet、httpservlet之间的区别

    转自:http://blog.csdn.net/rat9912345/article/details/5161789 当编写一个servlet时,必须直接或间接实现servlet接口,最可能实现的方法 ...

  7. 安装.net Framework 3.5 SP1非常慢的解决方案

    解决方案:1.msiexec /unregserver回车,在输入命令:msiexec /regserver msiexec /unregserver是停止installer服务,而msiexec / ...

  8. Asp.Net MVC中Controller与View之间传递的Model

    Controller --> View 的Model 与 提交表单后 View --> Controller 的Model 不是相同的对象,即:这两个Model为不同的指针,指向不同的地址 ...

  9. Xcode 缓存 帮助文档 隐藏文件夹显示方法

    删除Xcode的缓存路径是:/Users/用户名/Library/Developer/Xcode/DerivedData(默认情况下, 这是一个隐藏文件夹) 显示隐藏文件夹 指令如下:显示隐藏文件: ...

  10. CentOS配置本地yum源(使用镜像iso文件)

    本人在使用yum安装软件的时候,感觉最不爽的是网络不佳时,安装的速度特别慢.所以,个人就上网search了一下如何使用Linux的安装文件作为其yum源.经过几次尝试,已经可以成功的配置了.下面是详细 ...