cocos2dx TMX地图
发布时间:2020-12-14 16:56:17 所属栏目:百科 来源:网络整理
导读:local map = cc.TMXTiledMap:create( "map.tmx" ):addTo(self); --加载地图 local mapProperties = map:getProperties(); --获取地图信息 print (mapProperties. type ); local mapLayer = map:getLayer( "scene" ); --获取图层 local LayerProperties = map
local map = cc.TMXTiledMap:create("map.tmx"):addTo(self); --加载地图
local mapProperties = map:getProperties(); --获取地图信息
print(mapProperties.type);
local mapLayer = map:getLayer("scene"); --获取图层
local LayerProperties = mapLayer:getProperties(); --获取图层信息
print(LayerProperties.type);
local tilePro = map:getPropertiesForGID(1); --获取图块信息
print(tilePro.type);
local objectGroup = map:getObjectGroup("object"); --获取对象组
local playerObj = objectGroup:getObject("player"); --从对象组获取对象
print(playerObj.type);
local colorObj = objectGroup:getObject("color"); --获取对象
local colorObjX = math.floor(colorObj.x / 32);
local colorObjY = math.floor(colorObj.y / 32);
local colorObjW = math.floor(colorObj.width / 32);
local colorObjH = math.floor(colorObj.height / 32);
for x = colorObjX,colorObjX + colorObjW - 1 do
for y = colorObjY,colorObjY + colorObjH - 1 do
local sprite = mapLayer:getTileAt(cc.p(x,map:getMapSize().height - y - 1));
sprite:setColor(cc.c3b(255,0,0)); --修改地图颜色
end
end
local player = display.newSprite("c.png"):addTo(self);
player:pos(playerObj.x,playerObj.y); --在player处创建一个精灵
map:setTouchEnabled(true); --开启地图触摸
map:setTouchMode(cc.TOUCH_MODE_ONE_BY_ONE);
map:addNodeEventListener(cc.NODE_TOUCH_EVENT,function (event) --触摸事件回调函数
if event.name ~= "began" then return true end --只处理点击事件
local point = cc.p(0,0);
point.x = math.floor(event.x / 32); --
point.y = map:getMapSize().height - math.floor(event.y / 32) - 1; --将点击坐标转换成瓦片坐标
local tiled = mapLayer:getTileAt(point); --获取对应瓦片
local gid = mapLayer:getTileGIDAt(point); --获取瓦片GID
local p = map:getPropertiesForGID(gid); --通过GID获取信息
if type(p) == "table" and p.move == "true" then --判断是否可以移动
player:pos(tiled:getPosition());
end
end);
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |