ccc tiledmap
//移动方向枚举类
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 enoughreturn;}//新建一个点var newTile = cc.p(this._curTile.x, this._curTile.y);var mapMoveDir = MoveDirection.NONE;//x方向移动if (movedXValue >= movedYValue){// move to right or leftif (movedX > 0) {newTile.x += 1;mapMoveDir = MoveDirection.LEFT;} else {newTile.x -= 1;mapMoveDir = MoveDirection.RIGHT;}}//y方向移动else{// move to up or downif (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 positionthis._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 datavar 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 mapvar 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();},
});
转载于:https://www.cnblogs.com/yufenghou/p/5438548.html
ccc tiledmap相关推荐
- aa bb ccc java,TinyTemplate(Velocity Plus版)即将火热推出~~~
原本是没有本身写一个模板引擎的计划的,由于按个人理解,一直认识这种"语言"级的引擎,难度是很是大的.总感受本身的水平不够,所以不敢有这个念头.直到大量使用Velocty的时候,碰到 ...
- 计算机竞赛CCC可以直接学吗,CCC 计算机竞赛到底有多牛!
加拿大计算机竞赛是什么?难度情况? 加拿大计算机竞赛是由加拿大滑铁卢大学主办的,每年举办一次,是一场面向中学生的计算机程序设计比赛,CCC竞赛的一个目的是为广大中学生朋友们提供一个机会来测试自己分析. ...
- ccc计算机比赛如何报名,整理:加拿大的CCC是什么,怎么报名?
原标题:整理:加拿大的CCC是什么,怎么报名? CCC计算机竞赛(全称 Canadian Computing Competition)由加拿大滑铁卢大学与清华大学联合举办面向高中生的CS计算机竞赛,是 ...
- awk匹配以aaa开头,以bbb结尾的内容,同时aaa和bbb之间还包含ccc
如果是匹配以A开头,以B结尾的内容,同时A和B之间还包含C的这种怎么做? 比如 [root@localhost ~]#cat file aaa grge ddd bbbaaa gege ccc bbb ...
- Cocos2d-xna : 横版战略游戏开发实验5 TiledMap实现关卡地图
Cocos2d-xna : 横版战略游戏开发实验5 TiledMap实现关卡地图 在前面的几篇中动手实验使用了CCSprite.CCScene.CCLayer.CCAction.CCMenu等coco ...
- java如何识别tiled地图_Egret学习-TiledMap使用
环境说明: 引擎版本:5.2.4 Egret Wing 4.1.6 2.新建一个游戏项目Tank 3.将上面包含tiled库的libsrc文件夹放置在 Egret 工程的父文件夹 /工作空间 ---- ...
- Pentaho BI server 中 CCC table Component 的使用小技巧
我使用的版本 Pentaho BI Server 5.3.0.0.213 CDE/CDF/CDA/CCC 15.04.16 stable Q: 如何设置表格中各种提示文字的语言(默认为英语)? CDE ...
- c语言数组取出特定字符串,C语言根据特定的符号分割字符串 如:字符串a,bbb,ccc-数组[a,bbb,ccc]...
//根据符号','分割字符串,例如:字符串"a,bbb,ccc"->数组[a,bbb,ccc] author:wangchangshuai jlu char** splitF ...
- 美格智能5G R16模组SRM825N顺利通过国内CCC、SRRC、CTA认证
5G网络技术的蓬勃发展,赋予了物联网产业更多的定义和内涵,也催生出了更多的数字应用场景.新一代5G无线通信模组作为物联网领域实现海量连接的载体,更是为千行百业的数字化转型贡献着积极力量. 近日,美格智 ...
- 转:瓦片地图TiledMap
标签:tiledMap 3.x cocos tmx tile 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://shahdza.bl ...
最新文章
- SQLAlchemy 使用经验
- Confluence 6 使用 Apache 和 mod_proxy 添加 SSL和其他
- 实现一个简单的web服务器
- 有助于改变你命运的5个处世潜规则
- H3C路由器Hub-Spoke网络结构D×××配置案例(试读连载一二)
- js传参中文格式不对乱码
- SpringBoot整合jersey
- Android 关闭屏幕方法
- 甘肃省天水市计算机培训班,甘肃天水秦州区文化馆举办首期天水传统菜培训班...
- 普华i-VirtualApp应用交付系统介绍
- 论文笔记+模型实现TransNets: Learning to Transform for Recommendation
- oracle asm 缺省用户,oracle asm自动存储使用及管理说明(下)
- Systemback使用精简教程
- 微信小程序图片等比缩放显示正中间
- 【边缘注意:深度多尺度特征】
- 乐信、趣店同源“异路”
- SpringBoot读取Resources下文件
- 不定积分——类似1/(1+e^x)的积分
- 使用Visio画矢量图 线条组合图形填充颜色
- 关于15种前端安全检测及危害