Hi
Currently, I put treasure chests on the map, by adding objects in Tiled on the object layer. All those objects cause treasure chest sprites to be added when the map loads:
mte.addSprite(sprite, {
kind = “sprite”,
layer = value[“layer”],
locX = spriteposition[“x”],
locY = spriteposition[“y”],
levelWidth = 32,
levelHeight = 32
})
That part works, but how do I make the player not just walk over them?
Currently, I’m using the following detection code and altered the last part to look for a property of “solid” on all layers within a level.
–CREATE COLLISION DETECTION COORDINATES -----------------------------------------------
–[[
In this sample the player is a rectangle 32 pixels across and 32 pixels wide with an
offset of -16 in the Y direction.
The origin of a sprite is it’s center, but in this case, due to the -16 pixel offset, the
origin of the sprite as reckoned by MTE is 16 pixels from the bottom of the sprite, or
the center of the lower tile in the stack.
To detect platforms and obstacles around the player we must define points around the
player to be checked by our code. There are many ways to do this, but in this example the
points are defined in relation to the player sprite. The top1 and top2 points are along
the top of the player sprite. The sprite is 32 pixels high and the origin is 16 pixels from
it’s bottom, so the top is -16 pixels from the origin. The rest of the points are defined
in the same manner.
]]–
local offsets = {top1 = {-12, -16},
top2 = {12, -16},
bottom1 = {-12, 16},
bottom2 = {12, 16},
left1 = {-16, -12},
left2 = {-16, -32},
right1 = {16, -12},
right2 = {16, -32}
}
–[[
These are variables for storing the true coordinates of the collision detection
points in relation to the level rather than the player sprite. These values are
calculated by the detectObstacle function below, which is called in an enterFrame.
]]–
local sensors = {top1 = {},
top2 = {},
bottom1 = {},
bottom2 = {},
left1 = {},
left2 = {},
right1 = {},
right2 = {}
}
local detect = {}
–OBSTACLE DETECTION FUNCTION-----------------------------------------------------------
local detectObstacle = function()
--CALCULATE SENSOR POSITIONS
local playerPosX = player.getLevelPosX() + velX
local playerPosY = player.getLevelPosY() + velY
sensors.top1[1], sensors.top1[2] = playerPosX + offsets.top1[1], playerPosY + offsets.top1[2]
sensors.top2[1], sensors.top2[2] = playerPosX + offsets.top2[1], playerPosY + offsets.top2[2]
sensors.bottom1[1], sensors.bottom1[2] = playerPosX + offsets.bottom1[1], playerPosY + offsets.bottom1[2]
sensors.bottom2[1], sensors.bottom2[2] = playerPosX + offsets.bottom2[1], playerPosY + offsets.bottom2[2]
sensors.left1[1], sensors.left1[2] = playerPosX + offsets.left1[1], playerPosY + offsets.left1[2]
sensors.left2[1], sensors.left2[2] = playerPosX + offsets.left2[1], playerPosY + offsets.left2[2]
sensors.right1[1], sensors.right1[2] = playerPosX + offsets.right1[1], playerPosY + offsets.right1[2]
sensors.right2[1], sensors.right2[2] = playerPosX + offsets.right2[1], playerPosY + offsets.right2[2]
--CHECK FOR TILE PROPERTIES
detect.top1 = mte.getTileProperties({levelPosX = sensors.top1[1], levelPosY = sensors.top1[2], level = 1})
detect.top2 = mte.getTileProperties({levelPosX = sensors.top2[1], levelPosY = sensors.top2[2], level = 1})
detect.bottom1 = mte.getTileProperties({levelPosX = sensors.bottom1[1], levelPosY = sensors.bottom1[2], level = 1})
detect.bottom2 = mte.getTileProperties({levelPosX = sensors.bottom2[1], levelPosY = sensors.bottom2[2], level = 1})
detect.left1 = mte.getTileProperties({levelPosX = sensors.left1[1], levelPosY = sensors.left1[2], level = 1})
detect.left2 = mte.getTileProperties({levelPosX = sensors.left2[1], levelPosY = sensors.left2[2], level = 1})
detect.right1 = mte.getTileProperties({levelPosX = sensors.right1[1], levelPosY = sensors.right1[2], level = 1})
detect.right2 = mte.getTileProperties({levelPosX = sensors.right2[1], levelPosY = sensors.right2[2], level = 1})
end
…
–CHECK FOR NORTH COLLISION
for key,value in pairs(detect.top1) do
if(value.properties and value.properties.solid) then
local loc1X, loc1Y = mte.convert(“levelPosToLoc”, sensors.top1[1]), mte.convert(“levelPosToLoc”, nil, sensors.top1[2])
local pos1X, pos1Y = mte.convert(“locToLevelPos”, loc1X) - mte.worldScaleX * 0.5, mte.convert(“locToLevelPos”, nil, loc1Y) - mte.worldScaleY * 0.5
if velY < 0 then
tempVelY = velY + (pos1Y - sensors.top1[2] + mte.worldScaleY)
velY = 0
end
end
end
for key,value in pairs(detect.top2) do
if(value.properties and value.properties.solid) then
local loc1X, loc1Y = mte.convert(“levelPosToLoc”, sensors.top1[1]), mte.convert(“levelPosToLoc”, nil, sensors.top1[2])
local pos1X, pos1Y = mte.convert(“locToLevelPos”, loc1X) - mte.worldScaleX * 0.5, mte.convert(“locToLevelPos”, nil, loc1Y) - mte.worldScaleY * 0.5
if velY < 0 then
tempVelY = velY + (pos1Y - sensors.top1[2] + mte.worldScaleY)
velY = 0
end
end
end
…