好久没有写博客了,自从自己用了云笔记后,喜欢在云笔记里记录,但最近发现自己cocos2d-html5的文章还是比较少,所以把最近通过别人的笔记cocos2d的关于tiled地图使用的文章转成了ch5终于跑起来了。
参考原文:
http://www.raywenderlich.com/1163/how-to-make-a-tile-based-game-with-cocos2d
中文参考稿:
http://www.cnblogs.com/andyque/archive/2011/04/11/2012852.html
关于如何创建 tiled地图文件tmx 文件的内容,请参考以上的中文参考稿,已经写的比较清楚了。唯一不清楚的就是tmx文件里面要修改你的图片相对路径:
如:
<tileset firstgid="1" name="tmw_desert_spacing" tilewidth="32" tileheight="32" spacing="1" margin="1">
<image source="tmw_desert_spacing.png" trans="ff00ff" width="265" height="199"/>
</tileset>
本文采用的cocos2d-html5版本为v2.1.5
现把我的翻译脚本copy上来供大家参考:
var MyLayer = cc.Layer.extend({
isMouseDown: false,
helloImg: null,
helloLabel: null,
circle: null,
sprite: null,
size: null,
_tileMap: null,
player: null,
init: function () {
this._super();
// ask director the window size
this.size = cc.Director.getInstance().getWinSize();
// 加载tmx
var tileMap = cc.TMXTiledMap.create(tmx_001);
this._tileMap = tileMap;
this.addChild(this._tileMap, 1, 0);
// 获得对象层
var objectLayer = tileMap.getObjectGroup("Objects");
var array = objectLayer.getObjects();
var spawnPoint = array[0];
var objX = spawnPoint["x"];
var objY = spawnPoint["y"];
var width = spawnPoint["width"];
var height = spawnPoint["height"];
this.player = cc.Sprite.create(s_Player);
this.player.setPosition(cc.p(objX, objY));
this.addChild(this.player, 1, 0);
this.setViewpointCenter(this.player.getPosition());
// 添加事件监听
this.setTouchEnabled(true);
},
registerWithTouchDispatcher: function () {
// CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, 0, true);
cc.Director.getInstance().getTouchDispatcher().addTargetedDelegate(this, 0, true);
},
onTouchEnded: function (touch, event) {
var touchLocation = touch.getLocation();
touchLocation = this.convertToNodeSpace(touchLocation);
//获得精灵当前位置
var playerPos = this.player.getPosition();
console.log("touchLocation.x: " + touchLocation.x);
console.log("touchLocation.y: " + touchLocation.y);
console.log("playerPos.x :" + playerPos.x);
console.log("playerPos.y :" + playerPos.y);
/*
计算出touch点和精灵的位置之差。我们必须基于touch位置选择一个方向,因此,
首先,我们需要计算出是上下移动还是左右移动。然后,我们比较正负值,决定具体的方向。
*/
var diff = cc.pSub(touchLocation, playerPos);
if (Math.abs(diff.x) > Math.abs(diff.y)) {
if (diff.x > 0) {
playerPos.x += this._tileMap.getTileSize().width;
} else {
playerPos.x -= this._tileMap.getTileSize().width;
}
} else {
if (diff.y > 0) {
playerPos.y += this._tileMap.getTileSize().height;
} else {
playerPos.y -= this._tileMap.getTileSize().height;
}
}
if (playerPos.x <= (this._tileMap.getMapSize().width * this._tileMap.getTileSize().width) &&
playerPos.y <= (this._tileMap.getMapSize().height * this._tileMap.getTileSize().height) &&
playerPos.y >= 0 && playerPos.x >= 0) {
// 设置精灵位置
this.setPlayerPosition(playerPos);
}
this.setViewpointCenter(this.player.getPosition());
},
onTouchBegan: function (touches, event) {
// 返回true表明接受这个触摸
return true;
},
/**
* 设置玩家所在的视窗
* @param position
* @returns {*}
*/
setViewpointCenter: function (position) {
// 屏幕大小
var winSize = this.size;
var x = Math.max(position.x, winSize.width / 2);
var y = Math.max(position.y, winSize.height / 2);
x = Math.min(x, (this._tileMap.getMapSize().width * this._tileMap.getTileSize().width) - winSize.width / 2);
y = Math.min(y, (this._tileMap.getMapSize().height * this._tileMap.getTileSize().height) - winSize.height / 2);
//console.log("x2 is : " + x + " y2 is : " + y);
// 实际位置
var actualPosition = cc.p(x, y);
// 坐标中心
var centerOfView = cc.p(winSize.width / 2, winSize.height / 2);
//console.log("centerOfView x" + centerOfView.x + "y " + centerOfView.y);
// 计算两点间的差分。
var viewPoint = cc.pSub(centerOfView, actualPosition);
console.log("视窗 x :" + viewPoint.x + " 视窗 y :" + viewPoint.y);
// 设置当前层的视窗
this.setPosition(viewPoint);
},
setPlayerPosition: function (viewPoint) {
console.log("精灵位置 x " + viewPoint.x);
console.log("精灵位置 y " + viewPoint.y);
this.player.setPosition(viewPoint);
}
});
var MyScene = cc.Scene.extend({
onEnter: function () {
this._super();
var layer = new MyLayer();
this.addChild(layer);
layer.init();
}
});
如果有了这个文章,你还不能跑起来,那我只能发源码给你了,把邮箱发过来。
附件为源码和源码目录结构。
相关推荐
cocos2d [1] 是一个基于MIT协议的开源框架,用于构建游戏、应用程序和其他图形界面交互应用。可以让你在创建自己的多平台游戏时节省很多的时间。 Cocos2D也拥有几个主要版本,包括Cocos2D-iPhone、Cocos2D-X,以及被...
cocos2d是一个基于MIT协议的开源框架,用于构建游戏、应用程序和其他图形界面交互应用。可以让你在创建自己的多平台游戏时节省很多的时间。 Cocos2D也拥有几个主要版本,包括Cocos2D-iPhone、Cocos2D-X,以及被社区...
(译)如何使用cocos2d制作基于tilemap的游戏教程 第一部分.pdf (译)如何在cocos2d里面使用动画和spritesheet.pdf (译)如何用cocos2d制作iphone游戏:旋转炮塔.pdf (译)如何用cocos2d制作iphone游戏:更猛的怪物和...
原价值1280元的项目实战_游戏编程超级玛丽(基于Cocos2d-x技术实现)课程,本系列课程分为四个实战项目: 1、<打飞机>:在还原微信《打飞机》的基础上,增加了道具,选择飞机,敌人射击等等更丰富的玩法。 2、<宝石...
第1章 Cocos2D-X引擎的介绍 1 1.1 何为游戏引擎 1 1.1.1 游戏的核心—引擎 1 1.1.2 引擎的特点 2 1.1.3 知名的引擎介绍 4 1.1.4 引擎的分类 5 1.2 Cocos2D-X引擎的来历 8 1.3 引擎的版本 9 1.4 下载与安装 10 1.5 ...
在第一篇《如何制作一个基于Tile的游戏》基础上,增加碰撞和拾取功能,原文《Collisions and Collectables: How To Make a Tile-Based Game with Cocos2D Part 2》,在这里继续以Cocos2d-x进行实现。
突破教程游戏的Cocos2d-html实现。 玩游戏 所有其他实现均已存档: 概述 这是用Cocos2D HTML编写的示例突破游戏。 最新版本是2.2.2,并且将在发布3.0时更新或分支。 此版本可在台式机和移动浏览器中运行,但也...
因上传权限限制,所以分拆成3部分。 其余两部分 ... 项目名单 AccelEvents AngelPanda AngryPanda AnimationActions ...TileMap ...VerticalShootingGame-ch5 ...chapter1.xcworkspace ...chapter5.xcworkspace ...cocos2d-iphone
此文件为第3部分 项目名单 AccelEvents AngelPanda AngryPanda AnimationActions AudioSetting BasicActions BetaTesting BlockActions CDSoundTest CallbackActions Cocos2DShaderDemo CocosAudio CocosBuilderDemo...
(译)如何使用cocos2d制作基于tilemap的游戏教程 第一部分.pdf (译)如何在cocos2d里面使用动画和spritesheet.pdf (译)如何用cocos2d制作iphone游戏:旋转炮塔.pdf (译)如何用cocos2d制作iphone游戏:更猛的怪物和更多...
此文件为第2部分 项目名单 AccelEvents AngelPanda AngryPanda AnimationActions AudioSetting BasicActions BetaTesting BlockActions CDSoundTest CallbackActions Cocos2DShaderDemo CocosAudio CocosBuilderDemo...
第1章-介绍cocos2d 第2章-开始学习 第3章-基础知识 第4章-你的第一个游戏 第4章-你的第一个游戏(修正错误) 第5章-游戏构成要素 第6章-深入了解精灵(Sprite) 第7章-横向滚屏射击游戏 第8章-完成滚屏射击游戏-1 第9章-...
Making buliding tile map easy in game. (Not my code)
所受分类选择的是Android,但是在windows上也可以跑,ps:我用的是vs2012. 重要!!拿到直接编译可能有点问题,因为有...将这个函数放在 cocos2dx\tilemap_parallax_nodes\CCTileMapAtlas.cpp 中同时在.h中声明即可。
本文实践自 Ray Wenderlich 的文章《How To Make a Tile-Based Game with Cocos2D》,文中使用Cocos2D,我在这里使用Cocos2D-x 2.0.4进行学习和移植。http://blog.csdn.net/akof1314/article/details/8474232
官方文档游戏的cocos2d-x实现(部分)。 可见博客 http://blog.csdn.net/fansongy/article/details/8864561