您的位置:首页 > 产品设计 > UI/UE

quick中,让精灵顺着tilemap制作的地图路径移动

2014-12-01 16:58 337 查看
 




制作上述的地图,在弯道出添加对象。

 

增加地图类:

 

local tileMap=nil
local map=class("map",
function()
return display.newLayer()
end)

function map:getInstance()
if tileMap==nil then
tileMap=map.new()
end
return tileMap
end

function map:ctor()
self.tile=CCTMXTiledMap:create("gameMap.tmx")
self:addChild(self.tile)

self.bgLayer=self.tile:layerNamed("bg")
end

function map:getMapObjectGroup()
return self.tile:objectGroupNamed("obj")
end

function map:toTilePosition(param)
local p={}
p.x=math.floor(param.x/(self.tile:getTileSize().width))
p.y=math.floor((self.tile:getMapSize().height*self.tile:getTileSize().height-param.y)/self.tile:getTileSize().height)

return p
end
function map:toCocosPosition(param)
local p={}
p.x=math.floor(param.x*(self.tile:getTileSize().width)-25)
p.y=math.floor((self.tile:getMapSize().height-param.y)*self.tile:getTileSize().height-25)

return p
end
return map


 

在玩家类的ctor中增加:

--获取对象层中的点
self.pointArray=self.map:getMapObjectGroup():getObjects()
self.allPoint={}
for i=0,self.pointArray:count()-1 do

local valueX = self.pointArray:objectAtIndex(i):objectForKey("x"):intValue()
local valueY = self.pointArray:objectAtIndex(i):objectForKey("y"):intValue()

j=#(self.allPoint)+1
self.allPoint[j] = {}
self.allPoint[j].x=valueX
self.allPoint[j].y=valueY
end

--初始化索引
self.pointIndex=1
--初始化位置
self:setPosition(self.allPoint[1].x,self.allPoint[1].y)

self:entityMove()

 

 

以及增加一个移动的方法:

function entity:entityMove()
self.pointIndex=self.pointIndex+1
if self.pointIndex>#self.allPoint then return end

--两个点的距离
local moveTime=(self:getPositionX()-self.allPoint[self.pointIndex].x)*(self:getPositionX()-self.allPoint[self.pointIndex].x)+
(self:getPositionY()-self.allPoint[self.pointIndex].y)*(self:getPositionY()-self.allPoint[self.pointIndex].y)
moveTime=math.sqrt(moveTime)*0.01

--local x, y =self:getPosition()
--print(CCPoint(10,10):getDistance(ccp(x, y)))
local moveAction=transition.sequence({
CCMoveTo:create(moveTime,CCPoint(self.allPoint[self.pointIndex].x,self.allPoint[self.pointIndex].y)),
CCCallFunc:create(handler(self, self.entityMove))
})

self:runAction(moveAction)

end


 

 

 

实现最终的效果是:







 

 

还有哪里不明白的?

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐