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
实现最终的效果是:
还有哪里不明白的?
相关文章推荐
- TiledMapEditor创建地图,精灵在地图上下左右移动,尚未实现碰撞检测功能
- 【cocos3.x+tilemap】制作rpg小游戏(一)地图制作
- 带高度的isometric tile map 斜45度 2d 地图制作
- OpenStreetMap初探(八)——制作地图瓦片Kosmos及Maperitive使用
- 10. 如何使用cocos2d制作基于tile地图的游戏教程:第一部分
- 网站地图制作工具 Sitemap Creator 2.1
- <cocos2d-x for wp7>使用cocos2d-x制作基于Tile地图的游戏(一)
- 网站地图制作工具 Sitemap Creator 2.1
- 转 OpenStreetMap初探(八)——制作地图瓦片Kosmos及Maperitive使用
- <cocos2d-x for wp7>使用cocos2d-x制作基于Tile地图的游戏:不一样的战斗(回合制战斗)(四)
- <cocos2d-x for wp7>使用cocos2d-x制作基于Tile地图的游戏:不一样的战斗(回合制战斗)(四)
- 使用TileMap制作游戏,在cocos2d-x中使用(三)
- 如何使用google map api制作自己的地图(转载)
- 如何使用cocos2d制作基于tile地图的游戏教程:第一部分
- (译)如何使用cocos2d制作基于tile地图的游戏教程:第一部分
- 【Cocos2d-x】新手自学(八)自定义路径,随心所欲移动精灵(2.0.1版本)
- 2011/08/02 动手联系子龙山人的‘如何制作基于tile地图的游戏’
- Google Maps API 2.0解析(10-GMapType GTileLayer _GoogleMapMercSpec _KeyholeMapMercSpec地图类型)
- (译)如何使用cocos2d制作基于tile地图的游戏教程:第一部分
- <cocos2d-x for wp7>使用cocos2d-x制作基于Tile地图的游戏:碰撞检测和收集物品(二)