[SOLVED] Question about Tilemap

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
Todespreis
Prole
Posts: 40
Joined: Sat Apr 01, 2023 9:30 pm

[SOLVED] Question about Tilemap

Post by Todespreis »

Hey there! I'm trying to implement sort of a tilemap. I tried to understand the tutorial on https://sheepolution.com/learn/book/18 and i have some questions to it. So he says: "We use the variable names row and tile to make it more clear what is going on. We loop through the table tilemap, and each value is a row. We loop through the row and each value is a tile."
How do i declare row and tile? I tried it without declaraation, because i thought, it is in the ipairs function and i get an error at line 170 - i think, because the variables tile and row are not declared.

so here is my code:

Code: Select all

local player

local direction

local ground

local tilemap



function love.load()
    
    FrameDuration = 0.08
    FrameTimer = 0
    FrameIndex = 1
    FramesWalkingDown =    {
       love.graphics.newImage("/Character_Sprites/going_down_01.png"),
       love.graphics.newImage("/Character_Sprites/going_down_02.png"),
       love.graphics.newImage("/Character_Sprites/going_down_03.png"),
       love.graphics.newImage("/Character_Sprites/going_down_04.png"),
       love.graphics.newImage("/Character_Sprites/going_down_05.png"),
       love.graphics.newImage("/Character_Sprites/going_down_06.png")
    }
    FramesWalkingUp = {
       love.graphics.newImage("/Character_Sprites/going_up01.png"),
       love.graphics.newImage("/Character_Sprites/going_up02.png"),
       love.graphics.newImage("/Character_Sprites/going_up03.png"),
       love.graphics.newImage("/Character_Sprites/going_up04.png"),
       love.graphics.newImage("/Character_Sprites/going_up05.png"),
       love.graphics.newImage("/Character_Sprites/going_up06.png")
    }
    FramesWalkingRight = {
      love.graphics.newImage("/Character_Sprites/going_right01.png"),
      love.graphics.newImage("/Character_Sprites/going_right02.png"),
      love.graphics.newImage("/Character_Sprites/going_right03.png"),
      love.graphics.newImage("/Character_Sprites/going_right04.png"),
      love.graphics.newImage("/Character_Sprites/going_right05.png"),
      love.graphics.newImage("/Character_Sprites/going_right06.png")
    }

    FramesWalkingLeft = {
      love.graphics.newImage("/Character_Sprites/going_left01.png"),
      love.graphics.newImage("/Character_Sprites/going_left02.png"),
      love.graphics.newImage("/Character_Sprites/going_left03.png"),
      love.graphics.newImage("/Character_Sprites/going_left04.png"),
      love.graphics.newImage("/Character_Sprites/going_left05.png"),
      love.graphics.newImage("/Character_Sprites/going_left06.png")
    }
  

    FrameEnabledDown = FramesWalkingDown[FrameIndex]
    FrameEnabledUp = FramesWalkingUp[FrameIndex]
    FrameEnabledRight = FramesWalkingRight[FrameIndex]
    FrameEnabledLeft = FramesWalkingLeft[FrameIndex]


    tilemap = {
        
        {16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16},
        {16, 16, 16, 16, 16, 16, 11, 12, 13, 12, 13, 12, 13, 12, 13, 15},
        {16, 16, 16, 16, 16, 16, 21, 22, 23, 22, 23, 22, 23, 22, 23, 25},
        {16, 16, 16, 16, 16, 16, 31, 33, 32, 33, 32, 33, 32, 33, 32, 35},
        {16, 16, 16, 16, 16, 16, 31, 33, 32, 33, 32, 33, 32, 33, 32, 35},
        {16, 16, 16, 16, 16, 16, 31, 33, 32, 33, 32, 33, 32, 33, 32, 35},
        {16, 16, 16, 16, 16, 16, 31, 33, 32, 33, 32, 33, 32, 33, 32, 35},
        {11, 12, 13, 12, 13, 12, 25, 33, 32, 33, 32, 33, 32, 33, 32, 35},
        {21, 22, 23, 22, 23, 22, 25, 33, 32, 33, 32, 33, 32, 33, 32, 35},
        {31, 32, 33, 32, 33, 32, 33, 32, 33, 32, 32, 33, 32, 32, 33, 35},
        {31, 32, 33, 32, 33, 32, 33, 32, 33, 32, 32, 33, 32, 32, 33, 35},
        {31, 32, 33, 32, 33, 32, 33, 32, 33, 32, 32, 33, 32, 32, 33, 35},
        {51, 52, 53, 52, 53, 15, 32, 33, 32, 33, 32, 33, 11, 52, 53, 55},
        {16, 16, 16, 16, 16, 31, 32, 33, 32, 33, 32, 33, 35, 16, 16, 16},
        {16, 16, 16, 16, 16, 31, 32, 33, 32, 33, 32, 33, 35, 16, 16, 16},
        {16, 16, 16, 16, 16, 51, 52, 53, 54, 52, 53, 54, 55, 16, 16, 16}
        
        
    }

    tile_11 = love.graphics.newImage("/DungeonTiles01/11.png")
    tile_12 = love.graphics.newImage("/DungeonTiles01/12.png")
    tile_13 = love.graphics.newImage("/DungeonTiles01/13.png")

    width = 16

    height = 16

    player = {}
    player.x = 70
    player.y = 70

    player.image = love.graphics.newImage("/Character_Sprites/standing_down.png" )

    


end


Down={}

function love.joystickpressed( joystick, button )

  Down[button]=true

end

function love.joystickreleased( joystick, button )

  Down[button]=nil

end

love.update=function (dt)

    FrameTimer = FrameTimer + dt
    if FrameTimer >= FrameDuration then
        FrameTimer = 0
        FrameIndex = FrameIndex + 1
        
        if FrameIndex > #FramesWalkingDown then
            FrameIndex = 1
        end

        if FrameIndex > #FramesWalkingUp then
            FrameIndex = 1
        end

        if FrameIndex > #FramesWalkingLeft then
            FrameIndex = 1
        end

        if FrameIndex > #FramesWalkingRight then
            FrameIndex = 1
        end
        
        if Down[06] then 
            player.x = player.x + 4
            FrameEnabledRight = FramesWalkingRight[FrameIndex]
        end

        if Down[04] then 
            player.y = player.y + 4
            FrameEnabledDown = FramesWalkingDown[FrameIndex]
        end

        if Down[02] then 
            player.x = player.x - 4
            FrameEnabledLeft = FramesWalkingLeft[FrameIndex]
        end

        if Down[00] then 
            player.y = player.y - 4
            FrameEnabledUp = FramesWalkingUp[FrameIndex]
        end
    end

    

end



function love.draw()

     for i,row in ipairs(tilemap) do
        for j,tile in ipairs(row) do
            
            if tile = 11 then
             
                love.graphics.draw( tile_11, j * width, i * height)   
               
            end
            
            if tile = 12 then 

                love.graphics.draw( tile_12, j * width, i * height) 

            end

            if tile = 13 then

                love.graphics.draw( tile_13, j * width, i * height)

            end

        end
     end

    if Down[06] then
    
      love.graphics.draw( FrameEnabledRight, player.x, player.y  )
    
    elseif Down[04] then 
   
      love.graphics.draw( FrameEnabledDown, player.x, player.y  ) 
    
    elseif Down[02] then
    
      love.graphics.draw( FrameEnabledLeft, player.x, player.y  )
    
    elseif Down[00] then
      
      love.graphics.draw( FrameEnabledUp, player.x, player.y  )

    else 
            love.graphics.draw( player.image, player.x, player.y  )
    end
Last edited by Todespreis on Wed Mar 13, 2024 8:44 am, edited 1 time in total.
qwdqwqwffqw
Prole
Posts: 33
Joined: Sat Jan 25, 2020 4:11 pm

Re: Question about Tilemap

Post by qwdqwqwffqw »

I think error is because of using "=" instead of "==", in "if tile = 11 then"?
User avatar
dusoft
Party member
Posts: 634
Joined: Fri Nov 08, 2013 12:07 am
Location: Europe usually
Contact:

Re: Question about Tilemap

Post by dusoft »

That's correct, you are assigning instead of comparing in your love.draw function.
Todespreis
Prole
Posts: 40
Joined: Sat Apr 01, 2023 9:30 pm

Re: Question about Tilemap

Post by Todespreis »

Oh no, how embarrassing! Thank you so much, it works now ^^"
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 4 guests