STI Tutorial, Getting whole sprite sheet

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
2lostsouls
Prole
Posts: 15
Joined: Sat May 25, 2019 9:33 pm

STI Tutorial, Getting whole sprite sheet

Post by 2lostsouls » 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.

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




User avatar
arampl
Party member
Posts: 248
Joined: Mon Oct 20, 2014 3:26 pm

Re: STI Tutorial, Getting whole sprite sheet

Post by arampl » Sun Jun 09, 2019 8:03 am

Don't use keyword "local" here:

Code: Select all

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

2lostsouls
Prole
Posts: 15
Joined: Sat May 25, 2019 9:33 pm

Re: STI Tutorial, Getting whole sprite sheet

Post by 2lostsouls » Sun Jun 09, 2019 6:44 pm

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.
Attachments
sprite.png
sprite.png (25.24 KiB) Viewed 1218 times

User avatar
Karai17
Party member
Posts: 879
Joined: Sun Sep 02, 2012 10:46 pm

Re: STI Tutorial, Getting whole sprite sheet

Post by Karai17 » Tue Jun 11, 2019 2:50 am

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
STI - An awesome Tiled library
LÖVE3D - A 3D library for LÖVE 0.10+

Dev Blog | GitHub | excessive ❤ moé

2lostsouls
Prole
Posts: 15
Joined: Sat May 25, 2019 9:33 pm

Re: STI Tutorial, Getting whole sprite sheet

Post by 2lostsouls » Tue Jun 11, 2019 3:43 pm

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.

User avatar
Karai17
Party member
Posts: 879
Joined: Sun Sep 02, 2012 10:46 pm

Re: STI Tutorial, Getting whole sprite sheet

Post by Karai17 » Tue Jun 11, 2019 4:09 pm

check the wiki to make sure you're using it correctly
STI - An awesome Tiled library
LÖVE3D - A 3D library for LÖVE 0.10+

Dev Blog | GitHub | excessive ❤ moé

2lostsouls
Prole
Posts: 15
Joined: Sat May 25, 2019 9:33 pm

Re: STI Tutorial, Getting whole sprite sheet

Post by 2lostsouls » Tue Jun 11, 2019 4:41 pm

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.

User avatar
pgimeno
Party member
Posts: 1690
Joined: Sun Oct 18, 2015 2:58 pm

Re: STI Tutorial, Getting whole sprite sheet

Post by pgimeno » Tue Jun 11, 2019 5:09 pm

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.

2lostsouls
Prole
Posts: 15
Joined: Sat May 25, 2019 9:33 pm

Re: STI Tutorial, Getting whole sprite sheet

Post by 2lostsouls » Fri Jun 14, 2019 2:02 am

I got it. Thank you for your help.

Post Reply

Who is online

Users browsing this forum: No registered users and 9 guests