Page 1 of 1

STI Tutorial, Getting whole sprite sheet

Posted: Sun Jun 09, 2019 3:39 am
by 2lostsouls
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.

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




Re: STI Tutorial, Getting whole sprite sheet

Posted: Sun Jun 09, 2019 8:03 am
by arampl
Don't use keyword "local" here:

Code: Select all

...
local love.graphics.draw(sprite.player, 100, 100)
...

Re: STI Tutorial, Getting whole sprite sheet

Posted: Sun Jun 09, 2019 6:44 pm
by 2lostsouls
Thank you. Yes, I did not think that was correct. I commented that out. The problem I am getting is that I am getting the whole sprite sheet on my map. Was not sure how to code to just get the top right sprite. I was trying to load a quad and use the x, y value to get it. The attachment shows what it looks like right now. Thank you.

Re: STI Tutorial, Getting whole sprite sheet

Posted: Tue Jun 11, 2019 2:50 am
by Karai17
you are drawing the whole texture, you want to send the quad with your texture.

Code: Select all

local q = love.graphics.newQuad(0, 0, 16, 16)
love.graphics.draw(sprite.player, q, 100, 100)
there is a library called anim8 you might want to look into, it's pretty good at helping with animations

Re: STI Tutorial, Getting whole sprite sheet

Posted: Tue Jun 11, 2019 3:43 pm
by 2lostsouls
Thank you for your help. But I am getting the following error when I run the newQuad:

bad argument #5 to 'newQuad' (number expected, got not vaules)

I took the local off, but still same error. Googled it but could not really find the answer. Found a lot of #2 errors. Thank you.

Re: STI Tutorial, Getting whole sprite sheet

Posted: Tue Jun 11, 2019 4:09 pm
by Karai17
check the wiki to make sure you're using it correctly

Re: STI Tutorial, Getting whole sprite sheet

Posted: Tue Jun 11, 2019 4:41 pm
by 2lostsouls
Thank you I did. The newQuad should go into Function love.load() and the love.graphics.draw should go into function.love.draw() but not working for me. Still messing with it. Thank you.

Re: STI Tutorial, Getting whole sprite sheet

Posted: Tue Jun 11, 2019 5:09 pm
by pgimeno
Hint: Don't take this literally:

Code: Select all

local q = love.graphics.newQuad(0, 0, 16, 16)
Instead, check the wiki for what parameters it expects.

Re: STI Tutorial, Getting whole sprite sheet

Posted: Fri Jun 14, 2019 2:02 am
by 2lostsouls
I got it. Thank you for your help.