STI Tutorial, Getting whole sprite sheet
Posted: Sun Jun 09, 2019 3:39 am
Good evening everyone,
I am following the STI Tutorial spawning a player on the map. I have finally got the player to spawn on the map but the problem is that it is all the players on the sprite sheet. I was figuring I need to put a quad and put in coordinates but not getting it to work.
I am getting the following error: main.lua:92: unexpected symbol near '.'
Any help would be greatly appreciated. Here is my code.
I am following the STI Tutorial spawning a player on the map. I have finally got the player to spawn on the map but the problem is that it is all the players on the sprite sheet. I was figuring I need to put a quad and put in coordinates but not getting it to work.
I am getting the following error: main.lua:92: unexpected symbol near '.'
Any help would be greatly appreciated. Here is my code.
Code: Select all
-- Include Simple Tiled Implementation into project
local sti = require "sti"
function love.load()
-- Load map file
map = sti("map.lua")
-- Create new dynamic data layer called "Sprites" as the 3rd layer
local layer = map:addCustomLayer("Sprites", 3)
-- Get player spawn object
local player
for k, object in pairs(map.objects) do
if object.name == "Player" then
player = object
break
end
end
-- Create player object
local sprite = love.graphics.newImage("sprite.png")
layer.player = {
sprite = sprite,
x = player.x,
y = player.y,
ox = sprite:getWidth() / 2,
oy = sprite:getHeight() / 1.35
}
-- Add controls to player
layer.update = function(self, dt)
-- 96 pixels per second
local speed = 96
-- Move player up
if love.keyboard.isDown("w") or love.keyboard.isDown("up") then
self.player.y = self.player.y - speed * dt
end
-- Move player down
if love.keyboard.isDown("s") or love.keyboard.isDown("down") then
self.player.y = self.player.y + speed * dt
end
-- Move player left
if love.keyboard.isDown("a") or love.keyboard.isDown("left") then
self.player.x = self.player.x - speed * dt
end
-- Move player right
if love.keyboard.isDown("d") or love.keyboard.isDown("right") then
self.player.x = self.player.x + speed * dt
end
end
-- Draw player
layer.draw = function(self)
love.graphics.draw(
self.player.sprite,
math.floor(self.player.x),
math.floor(self.player.y),
0,
1,
1,
self.player.ox,
self.player.oy
)
-- Temporarily draw a point at our location so we know
-- that our sprite is offset properly
love.graphics.setPointSize(5)
love.graphics.points(math.floor(self.player.x), math.floor(self.player.y))
end
-- Remove unneeded object layer
map:removeLayer("Spawn Point")
end
function love.update(dt)
-- Update world
map:update(dt)
end
function love.draw()
-- Scale world
local scale = 2
local screen_width = love.graphics.getWidth() / scale
local screen_height = love.graphics.getHeight() / scale
-- Add Quad to get the first player
local love.graphics.draw(sprite.player, 100, 100)
-- Translate world so that player is always centered
local player = map.layers["Sprites"].player
local tx = math.floor(player.x - screen_width / 2)
local ty = math.floor(player.y - screen_height / 2)
-- Transform world
love.graphics.scale(scale)
love.graphics.translate(-tx, -ty)
-- Draw world
map:draw()
end