Hi,
could someone let me know how this performs on a device please?
Also I adapted the code from another site (http://love2d.org/wiki/Tutorial:Efficient_Tile-based_Scrolling) and i’m a bit unsure about the maths I’ve used
In this version I have to add a column and row in the map array to allow for my offscreen tiles at the bottom and right edge. so if your map is 100x100 you need to make it 101x101 and use the end row/columns just as dummy values as they are never actually seen. This may be an issue with my modulus and flooring calculations
also if there’s a more optimal to code it, please make any suggestions
regards
j
http://www.sendspace.com/file/k7e9vk
[blockcode]
require(“sprite”)
display.setStatusBar( display.HiddenStatusBar )
local map – stores tiledata
local mapWidth, mapHeight – width and height in tiles
local mapX, mapY – view x,y in tiles. can be a fractional value like 3.25.
local oldMapX, oldMapY
local tilesDisplayWidth, tilesDisplayHeight – number of tiles to show
local tilesetImage
local tileSize = 32 – size of tiles in pixels
local tilesetSprite
local moveX = 0
local moveY = 0
local moveAmount=8/tileSize – in tile units, so to move 1px at a time set moveAmount to 1/tileSize
local _floor = math.floor
local _max = math.max
local _min = math.min
local _rnd = math.random
local _fmod = math.fmod
local function setupMap()
– note for a 100x100 map i need a 1 tile border at the right and bottom,
– this is for my offscreen tiles used for scrolling.
– although maybe fixed maths will avoid having to add this?
mapWidth = 101
mapHeight = 101
local x,y
map = {}
for x=1,mapWidth do
map[x] = {}
for y=1,mapHeight do
– pick a random tile for now
map[x][y] = _rnd(100)
–map[x][y]=8
– edges of map
if(x==1 or y==1 or x==mapWidth-1 or y==mapHeight-1) then map[x][y]=4 end
– this boundary tile won’t be seen
– it’s the right and bottom edge used for scrolling
– you can check it by uncommenting the xScale, yScale
– at the end of this file
if(x==mapWidth or y==mapHeight) then map[x][y]=2 end
end
end
end
local function setupMapView()
mapX = 1
mapY = 1
tilesDisplayWidth = 11 --(10 * 32 = 320, plus 1 spare column)
tilesDisplayHeight = 16 --(15 * 32 = 480, plus 1 spare row)
end
local function setupTileset()
spritemap = display.newGroup()
spriteSheet = sprite.newSpriteSheet(“tileset.png”, tileSize, tileSize)
spriteSet1 = sprite.newSpriteSet(spriteSheet, 1,100)
for x=1,tilesDisplayWidth,1 do
for y=1,tilesDisplayHeight,1 do
local tilesprite = sprite.newSprite( spriteSet1 )
tilesprite:setReferencePoint(display.TopLeftReferencePoint)
tilesprite.x = (x-1)*tileSize
tilesprite.y = (y-1)*tileSize
spritemap:insert(tilesprite)
end
end
end
local function updateTilesetBatch()
local i = 1
local fX = _floor(mapX)
local fY = _floor(mapY)
local f, x, y
for x=0, tilesDisplayWidth-1,1 do
for y=0, tilesDisplayHeight-1, 1 do
f = map[x+fX][y+fY]
spritemap[i].currentFrame = f
i = i + 1
end
end
end
– central function for moving the map
local function moveMap(dx, dy)
oldMapX = mapX
oldMapY = mapY
local needUpdate = false
if(dx~=0) then
mapX = _max(_min(mapX + dx, mapWidth - (tilesDisplayWidth-1)), 1)
spritemap.x = 0-((mapX * tileSize)%tileSize)
if(_floor(mapX) ~= _floor(oldMapX)) then needUpdate=true end
end
if(dy~=0) then
mapY = _max(_min(mapY + dy, mapHeight - (tilesDisplayHeight-1)), 1)
spritemap.y = 0-((mapY * tileSize)%tileSize)
if(_floor(mapY) ~= _floor(oldMapY)) then needUpdate=true end
end
if(needUpdate) then
updateTilesetBatch()
end
end
local function update(event)
– if move amount is set, move the map
if(moveX~=0 or moveY~=0) then
moveMap(moveX,moveY)
end
– if not moving (ie at edges) then set moveX,moveY
if(mapX==oldMapX) then moveX=0 end
if(mapY==oldMapY) then moveY=0 end
end
local function onTap(event)
moveX=0
moveY=0
– tap edges of screen to move
if(event.x < 100) then moveX=-moveAmount
elseif(event.x > 220) then moveX=moveAmount
end
if(event.y < 100) then moveY=-moveAmount
elseif(event.y > 380) then moveY=moveAmount
end
return true
end
local function load()
setupMap()
setupMapView()
setupTileset()
updateTilesetBatch()
collectgarbage(“collect”)
collectgarbage(“count”)
–spritemap.xScale =0.5
–spritemap.yScale=0.5
end
load()
Runtime:addEventListener(“enterFrame”,update)
Runtime:addEventListener(“tap”, onTap)
local fps = require(“fps”)
local performance = fps.PerformanceOutput.new();
performance.group.x, performance.group.y = display.contentWidth/2, 0;
performance.alpha = 0.6;
[/blockcode] [import]uid: 10744 topic_id: 4323 reply_id: 304323[/import]